Skip to content

Instantly share code, notes, and snippets.

@mathieu-coingt
Last active November 13, 2023 02:00
Show Gist options
  • Save mathieu-coingt/e7c50575d83721c9792facdeabd9fc0b to your computer and use it in GitHub Desktop.
Save mathieu-coingt/e7c50575d83721c9792facdeabd9fc0b to your computer and use it in GitHub Desktop.
Github Actions Workflow for Symfony project
# .github/worflow/main.yaml
name: CI/CD production
on:
push:
branches: [ production ]
jobs:
build:
runs-on: ubuntu-latest
steps:
# Mandatory : fetch the current repository
- name: Checkout repository
uses: actions/checkout@v2
# To be faster, use cache system for the NPM
- name: Cache NPM (node_modules)
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
# Define the right Node.js's environment
- name: Environment for NPM
uses: actions/setup-node@v2
with:
node-version: '14'
# NPM run build
- name: NPM build
run: |
node -v
npm ci --cache .npm --unsafe-perm --prefer-offline
npm run build
# Upload artifacts (= builded files) to download them in the next job
- name: NPM artifacts
uses: actions/upload-artifact@v2
with:
name: npm-build
retention-days: 1
path: public/build/
deploy:
# Need the "build" job to be complete before started it
needs: build
runs-on: ubuntu-latest
steps:
# Mandatory : fetch the current repository
- name: Checkout repository
uses: actions/checkout@v2
# To be faster, use cache system for the Composer
- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer (vendor)
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
# Define the right PHP environment
# https://github.com/shivammathur/setup-php (community)
- name: Environment for PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
#extensions: mbstring, xml, ctype, iconv, intl, pdo, pdo_mysql, dom, filter, gd, iconv, json, mbstring, pdo
tools: composer:v2
coverage: none
env:
update: true
# Ensure that composer.json is valid
- name: Validate composer.json and composer.lock
run: composer validate
# Log the PHP version in use, and copy the "prod" env file to the "local" env file
- name: PHP utils
run: |
php -v
cp .env.prod .env.local
# Install composer dependencies, and dump env
- name: Composer install & dump-env
run: |
composer install --no-dev --no-progress --prefer-dist -a
composer dump-env prod
# Download artifacts created in the previous job
- name: Download NPM artifacts
uses: actions/download-artifact@v2
with:
name: npm-build
path: public/build
# Define the right Node.js environment
- name: Environment for deploy
uses: actions/setup-node@v2
with:
node-version: '14'
# Deploy repository onto the host provided in Github Secrets, including downloaded artifacts and files created (like vendor folder)
# Deploy is running thanks to "Deployator" NPM dependency (https://github.com/la-haute-societe/deployator)
# See "deployment-config.js" to get the config in use
- name: Deploy on prod
env:
SSH_HOST: ${{ secrets.SSH_HOST }}
SSH_LOGIN: ${{ secrets.SSH_LOGIN }}
SSH_PWD: ${{ secrets.SSH_PWD }}
SSH_PATH: ${{ secrets.SSH_PATH }}
run: |
npx --cache .npm deployator deploy --config deployment-config.js --environment=production
/*
* NOTICE OF LICENCE
* Le code source de ce fichier est propriété de Mathieu Coingt
*
* Contact : Mathieu Coingt <mathieu@coingt.dev>
* Website : https://coingt.dev
* Date : 11/09/2021 18:30
*/
"use strict";
function afterDeployProd(context) {
return [
`rm -rf ${context.release.path}/translations`,
`mkdir -p ${context.options.deployPath}/${context.options.sharedFolder}/translations`,
`ln -s ${context.options.deployPath}/${context.options.sharedFolder}/translations ${context.release.path}/translations`,
`php ${context.release.path}/bin/console d:m:m -n`,
`php ${context.release.path}/bin/console c:c`,
];
}
module.exports = function (options) {
return {
common: {
mode: 'synchronize',
currentReleaseLink: 'current',
localPath: process.cwd(), //Use "process.cwd()" if the local path needs to be "./". Don't leave it blank
share: {
'uploads': 'public/uploads',
'media-cache': 'public/media',
'log': 'var/log',
},
exclude: [
'.ddev/**',
'.ddev',
'.git/**',
'.git',
'.github/**',
'.github',
'.npm/**',
'.npm',
'assets/**',
'assets',
'public/media/**',
'public/media',
'public/uploads/**',
'public/uploads',
'tests/**',
'tests',
'var/**',
'var',
],
create: [
'var',
'var/log'
],
},
environments: {
production: {
host: process.env.SSH_HOST,
username: process.env.SSH_LOGIN,
password: process.env.SSH_PWD,
port: 22,
deployPath: process.env.SSH_PATH,
onAfterDeploy: afterDeployProd,
},
}
};
};
# config/packages/prod/liip_imagine.yaml
liip_imagine:
# Indispensable pour autoriser l'accès aux fichiers hors du répertoire root
loaders:
default:
filesystem:
locator: filesystem_insecure
{
"license": "UNLICENSED",
"private": true,
"scripts": {
"dev-server": "encore dev-server",
"dev": "encore dev",
"watch": "encore dev --watch",
"build": "encore production --progress"
},
"devDependencies": {
....
"deployator": "^2.0.0",
},
"dependencies": {
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment