deploy-trigger #778
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy | |
on: | |
repository_dispatch: | |
types: [manual-trigger, deploy-trigger] | |
jobs: | |
deploy: | |
name: Deploy | |
runs-on: ubuntu-latest | |
concurrency: | |
group: core-deploy | |
cancel-in-progress: false | |
steps: | |
- name: Checkout Codes | |
uses: actions/checkout@v3 | |
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/[email protected] | |
with: | |
APP_URL: https://www.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_TRAINING_ALERTS_CHANNEL_ID: ${{ secrets.DISCORD_TRAINING_ALERTS_CHANNEL_ID }} | |
# | |
# 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@v2 | |
name: Create GitHub Deployment | |
id: github_deployment | |
with: | |
token: ${{ github.token }} | |
environment_url: https://www.vatsim.uk | |
environment: production | |
ref: ${{ github.event.client_payload.ref }} | |
# | |
# BUILD DEPENDENCIES SETUP | |
# | |
- name: Setup Node | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '20' | |
- name: Configure PHP | |
uses: shivammathur/setup-php@v2 | |
with: | |
php-version: 8.2 | |
coverage: pcov | |
tools: composer: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@v3 | |
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@v3 | |
with: | |
path: vendor | |
key: ${{ runner.OS }}-build-${{ hashFiles('**/composer.lock') }} | |
# Install | |
- name: Install Composer Dependencies | |
run: | | |
composer install --no-interaction | |
php artisan package:discover | |
# | |
# NODE BUILD | |
# Install node_modules and run webpack | |
# | |
# Restore Caches | |
- name: Get Node Cache Directory | |
id: node-cache-dir-path | |
run: echo "::set-output name=dir::$(node cache dir)" | |
- name: Restore Node Cache | |
uses: actions/cache@v3 | |
id: node-cache | |
with: | |
path: ${{ steps.node-cache-dir-path.outputs.dir }} | |
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-node- | |
# Install node_modules | |
- name: Install Core assets | |
run: npm ci | |
# Run Webpack | |
- name: Compile Core assets | |
run: npm run build | |
# Not required for deployment | |
- name: Remove node_modules | |
run: 'rm -rf node_modules' | |
- name: Remove vendor for NPM build. | |
run: 'rm -rf vendor' | |
# | |
# 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 /usr/local/bin/composer install --no-interaction --no-dev | |
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/[email protected] | |
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: core | |
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://www.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://www.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 }} |