2023-11-13_21-27-22 #316
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: Build and deploy | |
on: | |
push: | |
branches: [production, main] | |
pull_request: | |
branches: [main] | |
env: | |
ENVIRONMENT_NAME: "${{ github.event_name == 'push' && github.ref_name == 'production' && 'prod' || 'dev' }}" | |
DEPLOY_USER: build | |
DEPLOY_HOST: zhp-pl | |
PACKAGE_NAME: cbp-app-package.tar.gz | |
DEPLOY_PATH: /var/www/vhosts/zhp.pl/${{ github.ref_name == 'production' && 'cbp-prod.zhp.pl' || 'cbp-dev.zhp.pl' }} # until relase; then change to cbp.zhp.pl for sake of clarity | |
PHP_PATH: /opt/plesk/php/8.2/bin/php | |
jobs: | |
build: | |
name: Build and test | |
runs-on: ubuntu-latest | |
services: | |
mysql: | |
image: mysql:latest | |
env: | |
MYSQL_ALLOW_EMPTY_PASSWORD: yes | |
MYSQL_DATABASE: cbp_test | |
ports: | |
- 33306:3306 # external:internal | |
options: >- | |
--health-cmd="mysqladmin ping" | |
--health-interval=10s | |
--health-timeout=5s | |
--health-retries=3 | |
steps: | |
###### Prepare environment | |
- uses: actions/checkout@v3 | |
- name: Setup Node.js | |
uses: actions/setup-node@v1 | |
with: | |
node-version: '18.x' | |
- name: Login to Azure | |
uses: azure/login@v1 | |
with: | |
creds: ${{ secrets.AZURE_CREDENTIALS }} | |
- name: Install PHP versions | |
uses: shivammathur/setup-php@v2 | |
with: | |
php-version: '8.2' | |
########### Restore caches | |
- name: Cache node_modules directory | |
uses: actions/cache@v2 | |
id: node_modules-cache | |
with: | |
path: node_modules | |
key: ${{ runner.OS }}-build-${{ hashFiles('**/package.json') }}-${{ hashFiles('**/package-lock.json') }} | |
- name: Get Composer Cache Directory | |
id: composer-cache | |
run: | | |
echo "::set-output name=dir::$(composer config cache-files-dir)" | |
- name: Cache PHP composer cache | |
uses: actions/cache@v2 | |
with: | |
path: ${{ steps.composer-cache.outputs.dir }} | |
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} | |
restore-keys: | | |
${{ runner.os }}-composer- | |
- name: Cache PHP dependencies | |
uses: actions/cache@v2 | |
id: vendor-cache | |
with: | |
path: vendor | |
key: ${{ runner.OS }}-build-${{ hashFiles('**/composer.lock') }} | |
###### Validate bicep | |
- name: Validate Bicep | |
uses: azure/arm-deploy@v1 | |
with: | |
subscriptionId: ${{ vars.AZURE_SUBSCRIPTION_ID }} | |
resourceGroupName: zhp-cbp-${{ env.ENVIRONMENT_NAME }} | |
template: ./infrastructure/cbp.bicep | |
deploymentMode: Validate | |
parameters: environment=${{ env.ENVIRONMENT_NAME }} | |
###### Install dependencies | |
- name: Copy .env | |
run: php -r "file_exists('.env') || copy('.env.example', '.env');" | |
- name: Install Composer Dependencies | |
if: steps.vendor-cache.outputs.cache-hit != 'true' | |
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist | |
- name: Install NPM Dependencies | |
if: steps.node_modules-cache.outputs.cache-hit != 'true' | |
run: npm ci | |
###### Build | |
- name: Build frontend | |
run: npm run build | |
# - name: Generate key | |
# run: php artisan key:generate | |
##### Tests | |
# - name: Run Migrations | |
# env: | |
# DB_CONNECTION: mysql | |
# DB_DATABASE: cbp_test | |
# DB_PORT: 33306 | |
# DB_USER: root | |
# run: | | |
# chmod -R 777 storage bootstrap/cache | |
# php artisan migrate | |
# php artisan db:seed | |
# | |
# - name: Check code style | |
# run: vendor/bin/pint --config vendor/apsg/coding-standards/pint.json --test | |
# - name: Execute tests (Unit and Feature tests) via PestPHP | |
# env: | |
# DB_CONNECTION: mysql | |
# DB_DATABASE: cbp_test | |
# DB_PORT: 33306 | |
# DB_USER: root | |
# run: vendor/bin/pest --parallel | |
##### Prepare & Publish artifact | |
- name: Clear package cache/sessions after testing | |
run: rm -rf ./storage/framework/sessions/* ./storage/framework/views/* | |
- name: Compress package | |
run: tar --exclude="./tests" --exclude="./node_modules" --exclude="./vendor" --exclude=".git" --exclude="./.env" --exclude=".github" --exclude="${{ env.PACKAGE_NAME }}" --exclude="./storage/logs" --exclude="./storage/debugbar" -cvf ${{ env.PACKAGE_NAME }} . | |
- name: Publish build artifact | |
uses: actions/upload-artifact@v3 | |
with: | |
name: ${{ env.PACKAGE_NAME }} | |
path: ${{ env.PACKAGE_NAME }} | |
- name: Publish bicep artifact | |
uses: actions/upload-artifact@v3 | |
with: | |
name: cbp.bicep | |
path: ./infrastructure/cbp.bicep | |
deploy-site: | |
name: Deploy site | |
if: github.event_name == 'push' | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Configure SSH | |
run: ${{ secrets.ZHP_PL_SSH_CONFIG }} | |
- name: Download artifact | |
uses: actions/download-artifact@v3 | |
with: | |
name: ${{ env.PACKAGE_NAME }} | |
- name: Copy new version into server | |
run: scp -r ./$PACKAGE_NAME $DEPLOY_USER@$DEPLOY_HOST:/tmp | |
- name: Ensure deploy folder presence | |
run: ssh zhp-pl "mkdir --parents ${{ env.DEPLOY_PATH }}" | |
- name: Extract package to tmp | |
run: | | |
ssh zhp-pl "mkdir -p /tmp/new_cbp_${ENVIRONMENT_NAME}" | |
ssh zhp-pl "tar --no-overwrite-dir -m -xvf /tmp/$PACKAGE_NAME -C /tmp/new_cbp_${ENVIRONMENT_NAME}" | |
- name: Remove old and move new package | |
run: ssh zhp-pl "rm -rf $DEPLOY_PATH/* && rsync -arv /tmp/new_cbp_${ENVIRONMENT_NAME}/ $DEPLOY_PATH/" | |
- name: Install composer | |
run: ssh zhp-pl "cd $DEPLOY_PATH && $PHP_PATH composer.phar install" | |
- name: Decrypt env | |
run: ssh zhp-pl "cd $DEPLOY_PATH && $PHP_PATH artisan env:decrypt --env=$ENVIRONMENT_NAME --key=${{ secrets.DECRYPT_KEY }} --force --filename=.env" | |
- name: Run db migrations | |
run: ssh zhp-pl "cd $DEPLOY_PATH && $PHP_PATH artisan migrate --force --seed" | |
- name: Clear cache | |
run: ssh zhp-pl "cd $DEPLOY_PATH && $PHP_PATH artisan cache:clear" | |
- name: Optimize | |
run: ssh zhp-pl "cd $DEPLOY_PATH && $PHP_PATH artisan optimize" | |
# - name: Final | |
# run: ssh zhp-pl "cd $DEPLOY_PATH && $PHP_PATH artisan db:seed --class=TagImagesSeeder" | |
deploy-azure: | |
name: Deploy Azure storage | |
if: github.event_name == 'push' | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Login to Azure | |
uses: azure/login@v1 | |
with: | |
creds: ${{ secrets.AZURE_CREDENTIALS }} | |
- name: Download bicep artifact | |
uses: actions/download-artifact@v3 | |
with: | |
name: cbp.bicep | |
- name: Deploy Bicep | |
uses: azure/arm-deploy@v1 | |
with: | |
subscriptionId: ${{ vars.AZURE_SUBSCRIPTION_ID }} | |
resourceGroupName: zhp-cbp-${{ env.ENVIRONMENT_NAME }} | |
template: cbp.bicep | |
parameters: environment=${{ env.ENVIRONMENT_NAME }} |