Skip to content

deploy-trigger

deploy-trigger #508

Workflow file for this run

name: Deploy
on:
repository_dispatch:
types: [manual-trigger, deploy-trigger]
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
concurrency:
group: ukcp-api-deploy
cancel-in-progress: false
steps:
- name: Checkout Codes
uses: actions/checkout@v2
with:
fetch-depth: 0
ref: ${{ github.event.client_payload.ref }}
- name: Extract short commit hash
run: |
echo "::set-env name=COMMIT::$(echo ${GITHUB_SHA} | cut -c1-7)"
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
- name: Extract tag
run: |
echo "::set-env name=TAG::$(git describe --tags --abbrev=0)"
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
#
# DEPLOYMENT PARAMETERS
#
- name: Set additional deployment variables
uses: allenevans/set-env@v2.0.0
with:
APP_URL: https://ukcp.vatsim.uk
APPLICATION_ROOT: ${{ secrets.APPLICATION_ROOT }}
RELEASE_DIRECTORY: ${{ secrets.APPLICATION_ROOT }}/releases/${{ env.COMMIT }}
SHARED_DIRECTORY: ${{ secrets.APPLICATION_ROOT }}/shared
PHP_PATH: /bin/php8.2
VERSIONS_TO_KEEP: 5
#
# DISCORD NOTIFICATION JOB START
#
- name: Discord Notification (Start)
uses: rjstone/discord-webhook-notify@v1
with:
severity: warn
description: ${{ format('Starting Deployment of **{0}**', github.repository) }}
details: >
${{ format(':rocket: Starting Deployment of commit `{0}` by :technologist: *{1}* to **Production** ({2})', env.COMMIT, github.actor, env.APP_URL) }}
footer: ${{ format('https://{0}/actions/runs/{1}', github.repository, github.run_id) }}
webhookUrl: ${{ secrets.ACTIONS_DISCORD_WEBHOOK }}
#
# GITHUB DEPLOYMENT JOB START
#
- uses: chrnorm/deployment-action@releases/v1
name: Create GitHub Deployment
id: github_deployment
with:
token: ${{ github.token }}
target_url: https://ukcp.vatsim.uk
environment: production
ref: ${{ github.event.client_payload.ref }}
#
# BUILD DEPENDENCIES SETUP
#
- name: Setup Yarn
uses: actions/setup-node@v1
with:
node-version: '20'
- name: Configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
tools: v2
# Add GitHub Auth to Composer
- name: Add Composer GitHub Token
run: composer config -g github-oauth.github.com ${{ secrets.GITHUB_TOKEN }}
# Restore Caches
- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Restore Composer Cache
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Restore Vendor From Cache
uses: actions/cache@v1
with:
path: vendor
key: ${{ runner.OS }}-build-${{ hashFiles('**/composer.lock') }}
- name: Install Composer Dependencies
run: composer install --prefer-dist --no-interaction --optimize-autoloader --no-suggest
#
# YARN BUILD
# Install node_modules and run webpack
#
# Restore Caches
- name: Get Yarn Cache Directory
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Restore Yarn Cache
uses: actions/cache@v1
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
# Install node_modules
- name: Install Assets
run: yarn
# Run Webpack
- name: Compile Assets
run: yarn run prod
# Not required for deployment
- name: Remove node_modules
run: 'rm -rf node_modules'
- name: Reduce Composer Dependencies To Production
run: composer install --no-interaction --no-dev --optimize-autoloader
#
# DEPLOYMENT
# Prepare remote environment and deploy application
#
- name: Deploy application
uses: appleboy/scp-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
port: ${{ secrets.SSH_PORT }}
source: "."
target: ${{ env.RELEASE_DIRECTORY }}
#
# REMOTE POST-DEPLOYMENT ACTIONS
# Conduct server-side post-deployment tasks and make application version live.
#
- name: (Remote) Setup .env & install composer dependencies
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER}}
port: ${{ secrets.SSH_PORT }}
key: ${{ secrets.SSH_KEY }}
script: |
# Ensure we're working from the current release
cd $RELEASE_DIRECTORY
# Symlink .env from root directory
ln -s $APPLICATION_ROOT/.env .env
# Install application dependencies
$PHP_PATH artisan package:discover
$PHP_PATH artisan filament:upgrade
$PHP_PATH artisan optimize
$PHP_PATH artisan event:cache
envs: RELEASE_DIRECTORY,APPLICATION_ROOT,TAG,PHP_PATH
- name: (Remote) Update symbolic links
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER}}
port: ${{ secrets.SSH_PORT }}
key: ${{ secrets.SSH_KEY }}
script: |
if [ ! -d "$SHARED_DIRECTORY/storage" ]; then
mkdir -p $SHARED_DIRECTORY/storage
mv $RELEASE_DIRECTORY/storage/* $SHARED_DIRECTORY/storage/
chmod -R 775 $SHARED_DIRECTORY/storage
fi
rm -rf $RELEASE_DIRECTORY/storage
ln -s $SHARED_DIRECTORY/storage $RELEASE_DIRECTORY/storage
# Update the current link to point to this release
ln -sfn $RELEASE_DIRECTORY $APPLICATION_ROOT/current
envs: RELEASE_DIRECTORY,SHARED_DIRECTORY,APPLICATION_ROOT
- name: Trigger Forge Deployment
uses: jbrooksuk/laravel-forge-action@v1.0.2
with:
trigger_url: ${{ secrets.FORGE_DEPLOY_WEBHOOK }}
#
# SENTRY
# Create a Sentry release
#
- name: Create Sentry release
uses: getsentry/action-release@v1
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
SENTRY_ORG: vatsim-uk
SENTRY_PROJECT: ukcp-api
with:
environment: production
version: ${{ github.sha }}
#
# HOUSEKEEPING
# Perform post-deployment housekeeping actions (release history)
#
- name: Housekeeping
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER}}
port: ${{ secrets.SSH_PORT }}
key: ${{ secrets.SSH_KEY }}
script: |
# Ensure we're only keeping the desired number of versions in history
# Releases are extracted by an array ordered by directory
# creation date.
releases=($(ls -tU $APPLICATION_ROOT/releases))
number_of_releases=${#releases[@]}
if [ "$number_of_releases" -gt "$VERSIONS_TO_KEEP " ]; then
for i in $(seq 0 `expr $number_of_releases - $VERSIONS_TO_KEEP - 1`);
do
echo "Removing: ${releases[$i]}"
# rm -rf $APPLICATION_ROOT/releases/${releases[$i]}
done
fi
envs: APPLICATION_ROOT,VERSIONS_TO_KEEP
#
# GITHUB DEPLOYMENT JOB END
#
- name: Update Deployment Status (Failed)
if: failure()
uses: chrnorm/deployment-status@releases/v1
with:
token: ${{ github.token }}
target_url: https://ukcp.vatsim.uk
state: "failure"
deployment_id: ${{ steps.github_deployment.outputs.deployment_id }}
- name: Update Deployment Status (Success)
if: success()
uses: chrnorm/deployment-status@releases/v1
with:
token: ${{ github.token }}
target_url: https://ukcp.vatsim.uk
state: "success"
deployment_id: ${{ steps.github_deployment.outputs.deployment_id }}
#
# DISCORD NOTIFICATIONS JOB END
#
- name: Discord Notification (Failed)
if: failure()
uses: rjstone/discord-webhook-notify@v1
with:
severity: error
description: ${{ format('Deployment **FAILED** of **{0}**', github.repository) }}
details: >
${{ format(':fire: Deployment **FAILED** for commit `{0}` by :technologist: *{1}* to **Production** ({2})', env.COMMIT, github.actor, env.APP_URL) }}
footer: ${{ format('https://github.com/{0}/actions/runs/{1}', github.repository, github.run_id) }}
webhookUrl: ${{ secrets.ACTIONS_DISCORD_WEBHOOK }}
- name: Discord Notification (Success)
if: success()
uses: rjstone/discord-webhook-notify@v1
with:
severity: info
description: ${{ format('Deployment **SUCCEEDED** of **{0}**', github.repository) }}
details: >
${{ format(':white_check_mark: Deployment **SUCCEEDED** for commit `{0}` by :technologist: *{1}* to **Production** ({2})', env.COMMIT, github.actor, env.APP_URL) }}
footer: ${{ format('https://github.com/{0}/actions/runs/{1}', github.repository, github.run_id) }}
webhookUrl: ${{ secrets.ACTIONS_DISCORD_WEBHOOK }}