Skip to content

Conditional Deployment

The default out-of-the-box deployment.yml will deploy every time you push to one of the three branches configured in the push section:

.github/workflows/deployment.yml
on:
push:
branches:
- main
- master
- develop

You can customize this based on your needs. For example, you may have a CI workflow that runs tests and you only want to deploy when the tests pass. You can do this by configuring the on section to your deployment.yml file.

.github/workflows/deployment.yml
on:
workflow_run:
workflows: ["CI"]
branches: [master, develop]
types:
- completed

Adding condition

In your jobs section you will also need to add an if

.github/workflows/deployment.yml
jobs:
build:
name: Build
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}

Swapping out References

Given that the deployment will now be triggered by another workflow instead of a push, the action will not have access to the correct ref and sha. We need to reference the ref and sha from the workflow that triggered the deployment.

You will also need to replace the following values:

  • From ${{ github.ref }} to ${{ github.event.workflow_run.head_branch }}.
  • From ${{ github.sha }} to ${{ github.event.workflow_run.head_sha }}.