Skip to content

Instantly share code, notes, and snippets.

@jeromecoupe
Last active August 2, 2023 16:41
Show Gist options
  • Save jeromecoupe/2a0314487546f6f11e695d7d774c9d3d to your computer and use it in GitHub Desktop.
Save jeromecoupe/2a0314487546f6f11e695d7d774c9d3d to your computer and use it in GitHub Desktop.
Github actions: build and deploy Craft sites (WIP)
name: Craft CMS deployments
on:
push:
branches: [master]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# Pull repository into the current pipeline
- name: pull repository
uses: actions/checkout@v2
# Setup container with private SSH Key (used by rsync)
- name: Loads private SSH key
uses: webfactory/ssh-agent@v0.4.1
with:
ssh-private-key: ${{ secrets.SSH_KEY }}
# Use a specific version of Node
- name: Use Node.js 12.x
uses: actions/setup-node@v1
with:
node-version: "12.x"
# Install PHP dependencies
- name: Composer install
run: composer install --no-interaction --no-progress --no-suggest --optimize-autoloader
# Install NPM dependencies
- name: NPM install
run: npm ci
# Build assets using locally installed Gulp
- name: Build assets with Gulp
run: npx gulp build
# rsync
# exclude web/uploads is there to avoid deleting user uploaded files
# The StrictHostKeyChecking option avoids a failure when the client does not know the SSH host already
- name: deploy with rsync
run: |
rsync -azh --delete-after --exclude={'/web/uploads/','/node_modules/','/.git/','/.github/'} -e 'ssh -o StrictHostKeyChecking=no' ./ ${{ secrets.SSH_USER }}@${{ secrets.ssh_HOST }}:~/
# execute Craft commands on remote server
- name: Execute SSH commmands on remote server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.ssh_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
script: |
chmod a+x craft
php craft backup/db
php craft migrate/all
php craft project-config/apply
php craft clear-caches/all
@jeromecoupe
Copy link
Author

@davidhellmann Build times can vary but, looking at the last 20 builds on the live project I am testing this, they are hovering around 2 minutes, give or take.

@cbj4074
Copy link

cbj4074 commented Nov 10, 2021

Thanks for sharing this!

I'm not a Craft CMS user, but I stumbled upon this while attempting to troubleshoot a similar build process (PHP with composer and npm).

I'm not clear as to how composer appears to be available in your environment automatically, i.e., you simply run composer install ... without installing Composer anywhere explicitly.

Do you mind sharing how this works, exactly? Thank you!

@jeromecoupe
Copy link
Author

jeromecoupe commented Nov 10, 2021

@cbj4074 No problem. Glad it helped. The straightforward answer is that php and composer are included in the ubuntu-latest image used as the virtual environment by Github actions, as per the doc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment