I had previously been using DeployHQ to deploy a couple of my website apps to my Linode server. This tool allowed my to run some Composer commands, then SSH into my server and sync changed files. I had always used the free version, and some time this year it started having GitHub sync issues so I all but stopped deploying updates to my apps.
The last couple of years I have been working with GitHub Actions as my CI/CD tooling for different projects (personal and business). So I thought it was time to create a deploy workflow.
File Structure
You need to follow the GitHub Action structure: .github/workflows/$name.yml
. In my case I used deploy.yml
.
Lets give it a name, and an “on” action.
name: "Deploy to Linode"
on:
push:
branches:
- master
workflow_dispatch:
The above workflow will show up in your GitHub repo’s Action tab, under workflows on the left.
The “on” action syntax in this case uses two events: push & workflow_dispatch.
push: This event will run when a push to a branch is triggered. In this case, merging into or pushing to master.
workflow_dispatch: This event is meant to be triggered manually from the Run Workflow dropdown and couple allow custom input fields (omitted in this example).
Jobs
The meat of this workflow is to trigger a job on select events. So we will create a single multi step job.
jobs:
tag:
name: Deploy
runs-on: ubuntu-latest
Steps
Now that we have our named job, we have to create the steps. Each step has a purpose, and some steps are required to run after another in a select order.
The following steps will do a few select actions:
- Checkout the current repo code.
- Make sure PHP is setup with additional (optional tools) using shivammathur/setup-php
- Run Composer install
- Run Composer update to delete development dependencies (
--no-dev
) before pushing to PROD. - SSH into my server using burnett01/rsync-deployments and rsync the current working directory code into the remote_path defined as a repository secret in GitHub.
- There are some great instructions in that repo. I also found this article to be helpful in creating a one off SSH key for these actions.
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.2"
coverage: none
tools: composer:v2
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Run composer install
run: |
composer install --no-interaction --optimize-autoloader
- name: Run composer update with --no-dev
run: |
composer update --no-dev --no-interaction --optimize-autoloader
- name: rsync deployment
uses: burnett01/[email protected]
with:
switches: -avz --exclude '.git' --exclude '.github' --exclude '.gitignore'
remote_path: ${{ secrets.DEPLOY_PATH }}
remote_host: ${{ secrets.DEPLOY_HOST }}
remote_user: ${{ secrets.DEPLOY_USER }}
remote_key: ${{ secrets.DEPLOY_KEY }}
Final thoughts
With this simple workflow, I can now auto-deploy my custom git tracked code to my personal, and business apps. See the full script on Gist.