Skip to content

Instantly share code, notes, and snippets.

@jeromecoupe
Last active August 2, 2023 16:41
Show Gist options
  • Select an option

  • Save jeromecoupe/2a0314487546f6f11e695d7d774c9d3d to your computer and use it in GitHub Desktop.

Select an option

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

jeromecoupe commented Sep 9, 2020

Copy link
Copy Markdown
Author

I wanted to reproduce a simplified version of what services like Buddy.Works are doing w/ Github Actions
Still a WIP but already tested it successfully with a small custom Craft website.

Basic steps

  1. pull repository in container
  2. load SSH key to use with rsync
  3. use specific version of Node
  4. Install PHP dependencies via Composer
  5. Install Node dependencies
  6. Build assets using Gulp, Gulp is driving Webpack for JS in my case (you could run NPM scripts instead)
  7. Deploy with rsync
  8. Execute Craft commands on destination server

This blogpost by the fine people at Fortrabbit helped a lot: https://blog.fortrabbit.com/how-to-use-github-actions

@dingo-d

dingo-d commented Sep 11, 2020

Copy link
Copy Markdown
run: composer install --no-interaction --no-progress --no-suggest --optimize-autoloader

I usually do

run: composer install --no-ansi --no-dev --no-interaction --no-progress --no-scripts --optimize-autoloader

On production builds.

@jeromecoupe

jeromecoupe commented Sep 11, 2020

Copy link
Copy Markdown
Author

@dingo-d thank you for chiming in, not a PHP or composer expert here but I'll make sure I have a closer look at those options with regards to standard Craft builds. Thanks again!

@davidhellmann

Copy link
Copy Markdown

Interesting! I use buddy too for private stuff but it's to expensive. How long runs a build with GH Actions?

@jeromecoupe

Copy link
Copy Markdown
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

cbj4074 commented Nov 10, 2021

Copy link
Copy Markdown

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

jeromecoupe commented Nov 10, 2021

Copy link
Copy Markdown
Author

@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