Skip to content

Cleaning up some bugs: #138

Cleaning up some bugs:

Cleaning up some bugs: #138

Workflow file for this run

name: Deploy
# Trigger the workflow on push and
# pull request events on the production branch
on:
push:
branches:
- main
# Authenticate to the server via ssh
# and run our deployment script
jobs:
create-deployment-artifacts:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Configure PHP 8.2
uses: shivammathur/setup-php@master
with:
php-version: 8.2
extensions: mbstring, ctype, fileinfo, openssl, PDO, bcmath, json, tokenizer, xml, pgsql
- name: Install Composer dependencies
run: composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader
- name: Clone .env before compiling
env:
LARAVEL_ENV: ${{ secrets.LARAVEL_ENV }}
run: |
echo "Copying the contents to .env"
printf "%s" "${LARAVEL_ENV}" > ".env"
- name: Compile CSS and Javascript
run: |
npm install
npm run build
- name: Create deployment artifact
env:
GITHUB_SHA: ${{github.sha}}
run: tar -czf "${GITHUB_SHA}".tar.gz --exclude=*.git --exclude=node_modules *
- name: Store artifact for distribution
uses: actions/upload-artifact@v3
with:
name: app-build
path: ${{ github.sha }}.tar.gz
prepare-release-on-server:
runs-on: ubuntu-latest
needs: create-deployment-artifacts
steps:
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: app-build
- name: Copy files through SSH
uses: appleboy/scp-action@v0.1.6
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
port: ${{ secrets.PORT }}
key: ${{ secrets.SSHKEY }}
source: ${{github.sha}}.tar.gz
target: /home/blink-safety/artifacts
- name: Extract files and create directories
uses: appleboy/ssh-action@master
env:
GITHUB_SHA: ${{ github.sha }}
ARTIFACTS_DIR: /home/blink-safety/artifacts
BASE_DIR: /home/blink-safety
RELEASES_DIR: /home/blink-safety/releases
STORAGE_DIR: /home/blink-safety/storage
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
port: ${{ secrets.PORT }}
key: ${{ secrets.SSHKEY }}
envs: GITHUB_SHA,ARTIFACTS_DIR,BASE_DIR,RELEASES_DIR,STORAGE_DIR
script: |
echo "Create a releases directory and unzip the artifact tar to a new folder"
mkdir -p "${RELEASES_DIR}/${GITHUB_SHA}"
tar xzf "${ARTIFACTS_DIR}/${GITHUB_SHA}.tar.gz" -C "${RELEASES_DIR}/${GITHUB_SHA}"
echo "Remove the release storage folder"
rm -rf "${RELEASES_DIR}/${GITHUB_SHA}/storage"
echo "Making sure that all the necessary storage folders are created and with the right permissions"
mkdir -p ${STORAGE_DIR}/{app,public,framework,logs}
mkdir -p ${STORAGE_DIR}/framework/{cache,sessions,testing,views}
mkdir -p ${STORAGE_DIR}/framework/cache/data
chgrp -R www-data ${STORAGE_DIR} ${RELEASES_DIR}/${GITHUB_SHA}/bootstrap/cache
chmod -R ug+rwx ${STORAGE_DIR} ${RELEASES_DIR}/${GITHUB_SHA}/bootstrap/cache
echo "Linking storage path to the release"
ln -s -n -f ${STORAGE_DIR} ${RELEASES_DIR}/${GITHUB_SHA}
run-before-hooks:
name: 'Running setup before activation'
runs-on: 'ubuntu-latest'
needs: [create-deployment-artifacts,prepare-release-on-server]
steps:
- name: Run before hooks
uses: appleboy/ssh-action@master
env:
GITHUB_SHA: ${{ github.sha }}
RELEASES_DIR: /home/blink-safety/releases
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
port: ${{ secrets.PORT }}
key: ${{ secrets.SSHKEY }}
envs: GITHUB_SHA,RELEASES_DIR
script: |
cd ${RELEASES_DIR}/${GITHUB_SHA}
echo "Deployment started ..."
activate-release:
name: 'Activating the release'
runs-on: 'ubuntu-latest'
needs: [create-deployment-artifacts,prepare-release-on-server,run-before-hooks]
steps:
- name: Run before hooks
uses: appleboy/ssh-action@master
env:
GITHUB_SHA: ${{ github.sha }}
BASE_DIR: /home/blink-safety
RELEASES_DIR: /home/blink-safety/releases
ACTIVE_RELEASE_DIR: /home/blink-safety/current
LARAVEL_ENV: ${{ secrets.LARAVEL_ENV }}
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
port: ${{ secrets.PORT }}
key: ${{ secrets.SSHKEY }}
envs: GITHUB_SHA,BASE_DIR,RELEASES_DIR,ACTIVE_RELEASE_DIR,LARAVEL_ENV
script: |
echo "Copying the contents to .env"
printf "%s" "${LARAVEL_ENV}" > "${BASE_DIR}/.env"
ln -s -n -f ${BASE_DIR}/.env ${RELEASES_DIR}/${GITHUB_SHA}
echo "Running database migrations and seeding the database"
cd ${RELEASES_DIR}/${GITHUB_SHA}
php artisan migrate --force
php artisan db:seed --force
echo "Binding public storage"
php artisan storage:link
echo "Activating the new release"
ln -s -n -f ${RELEASES_DIR}/${GITHUB_SHA} ${ACTIVE_RELEASE_DIR}
echo "Reloading PHP8.2-FPM"
service php8.2-fpm reload
run-after-hooks:
name: 'Running hooks after setup'
runs-on: 'ubuntu-latest'
needs: [create-deployment-artifacts,prepare-release-on-server,run-before-hooks,activate-release]
steps:
- name: Run after hooks
uses: appleboy/ssh-action@master
env:
GITHUB_SHA: ${{ github.sha }}
ACTIVE_RELEASE_DIR: /home/blink-safety/current
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
port: ${{ secrets.PORT }}
key: ${{ secrets.SSHKEY }}
envs: GITHUB_SHA,ACTIVE_RELEASE_DIR
script: |
cd ${ACTIVE_RELEASE_DIR}
echo "Caching events"
php artisan event:cache
echo "Caching routes"
php artisan route:cache
echo "Caching views"
php artisan view:cache
echo "Update the events"
php artisan app:retrieve-st-petersburg-data
echo "Deployment finished ..."
clean-up:
name: 'Clean up the server'
runs-on: 'ubuntu-latest'
needs: [ create-deployment-artifacts,prepare-release-on-server,run-before-hooks,activate-release,run-after-hooks]
steps:
- name: Run clean up
uses: appleboy/ssh-action@master
env:
RELEASES_DIR: /home/blink-safety/releases
ARTIFACTS_DIR: /home/blink-safety/artifacts
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
port: ${{ secrets.PORT }}
key: ${{ secrets.SSHKEY }}
envs: RELEASES_DIR,ARTIFACTS_DIR
script: |
cd ${RELEASES_DIR} && ls -t -1 | tail -n +5 | xargs rm -Rf
cd ${ARTIFACTS_DIR} && ls -t -1 | tail -n +5 | xargs rm -Rf