Skip to content

Instantly share code, notes, and snippets.

@jasperf
Last active May 24, 2024 06:36
Show Gist options
  • Save jasperf/9ff917a851f45a79de6571399e4283b7 to your computer and use it in GitHub Desktop.
Save jasperf/9ff917a851f45a79de6571399e4283b7 to your computer and use it in GitHub Desktop.
Github workflow for Laravel Vue application and deployment on Ploi managed server

This workflow uses the on block to specify that it should be triggered on pushes to the staging branch and pull requests to the master branch.

The jobs block defines a single job called build-and-deploy. This job runs on an Ubuntu 20.04 (Focal) environment and consists of seven steps:

  1. Checkout code
  2. Install Composer dependencies
  3. Run Laravel Artisan commands
  4. Install npm dependencies
  5. Run Vue.js build script (prod mode)
  6. Deploy to environment using the appleboy/ssh-action action
  7. Set DEPLOY_TO_DOMAIN environment variable based on the branch being pushed or the pull request being merged.

The secrets block defines four separate secrets for staging and production environments:

  1. DEPLOY_TO_STAGING_KEY
  2. DEPLOY_TO_STAGING_HOST
  3. DEPLOY_TO_STAGING_USERNAME
  4. DEPLOY_TO_PRODUCTION_KEY
  5. DEPLOY_TO_PRODUCTION_HOST
  6. DEPLOY_TO_PRODUCTION_USERNAME

These secrets are used in the deployment script to determine which environment to deploy to based on the branch being pushed or the pull request being merged.

name: Build and Deploy
on:
push:
branches:
- staging
pull_request:
branches:
- master
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install Composer dependencies
run: composer install
- name: Run Laravel Artisan commands
run: |
php artisan optimize
php artisan route:cache
php artisan config:clear
php artisan view:clear
# php artisan db:seed --class=DatabaseSeeder
- name: Install npm dependencies
run: npm install
- name: Run Vue.js build script (prod mode)
run: |
npm run prod
- name: Deploy to environment
uses: appleboy/ssh-action@v0.2.3
with:
key: ${secrets.${DEPLOY_TO_KEY}}
host: ${secrets.${DEPLOY_TO_HOST}}
username: ${secrets.${DEPLOY_TO_USERNAME}}
script: |
# Use a dynamic value based on the environment being deployed to
domain=${env(DEPLOY_TO_DOMAIN)}
rm -rf ~/${domain}/*
cp -r ./* ~/${domain}/
chown -R ploi:ploi .
- name: Run php artisan publish:site-js
run: |
cd ~/${domain}/
php artisan publish:site-js
- name: Set DEPLOY_TO_DOMAIN environment variable
env:
DEPLOY_TO_DOMAIN: ${if: github.event_name == 'push' &&
github.ref_name == 'staging', 'staging. domain.com', 'production. domain.com'}
secrets:
DEPLOY_TO_STAGING_KEY:
usage: DEPLOY_TO_KEY
DEPLOY_TO_STAGING_HOST:
usage: DEPLOY_TO_HOST
DEPLOY_TO_STAGING_USERNAME:
usage: DEPLOY_TO_USERNAME
DEPLOY_TO_PRODUCTION_KEY:
usage: DEPLOY_TO_KEY
DEPLOY_TO_PRODUCTION_HOST:
usage: DEPLOY_TO_HOST
DEPLOY_TO_PRODUCTION_USERNAME:
usage: DEPLOY_TO_USERNAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment