I am experiencing unexpected behavior in my GitHub Actions workflow where a job is being triggered despite all conditions evaluating to false. Specifically, the docker job starts even when all outputs from the changes job are false, which should prevent any of the matrix entries from running. Additionally, I am unable to access ${{ matrix.image }} when all conditions are false. Below is the relevant part of my workflow:
Problem Details:
The docker job is starting even when all the outputs from the changes job are false.
When all conditions are false, it seems I cannot access ${{ matrix.image }}.
Questions:
What could be the reason that the docker job is being triggered despite all conditions evaluating to false? Is there a better way to structure the conditions to prevent the docker job from running when no changes are detected in the relevant paths?
jobs:
changes:
name: Detect changes
runs-on: ubuntu-latest
outputs:
library: ${{ steps.filter.outputs.library }}
workers: ${{ steps.filter.outputs.workers }}
collector: ${{ steps.filter.outputs.collector }}
api: ${{ steps.filter.outputs.api }}
webhook: ${{ steps.filter.outputs.webhook }}
all: ${{ steps.filter.outputs.changes }}
timeout-minutes: 30
steps:
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v1.4.7
- name:
uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
id: filter
with:
filters: |
library:
- 'package.json'
- 'package-lock.json'
- 'shared/**'
workers:
- 'workers/**'
- 'prisma/**'
collector:
- 'collector/**'
api:
- 'api/**'
webhook:
- 'webhook/**'
docker:
name: Build and push Docker Images
runs-on: ubuntu-latest
needs: changes
timeout-minutes: 30
strategy:
matrix:
image:
- collector
- workers
- api
- webhook
isCollector:
- ${{ needs.changes.outputs.collector == 'true' || needs.changes.outputs.library == 'true'}}
isWorker:
- ${{ needs.changes.outputs.workers == 'true' || needs.changes.outputs.library == 'true'}}
isApi:
- ${{ needs.changes.outputs.api == 'true' || needs.changes.outputs.library == 'true' }}
isWebhook:
- ${{ needs.changes.outputs.webhook == 'true' || needs.changes.outputs.library == 'true'}}
exclude:
- isCollector: false
image: collector
- isWorker: false
image: workers
- isApi: false
image: api
- isWebhook: false
image: webhook
2
Answers
Try this:
Alternatively you could try this:
When a dynamic matrix is empty, GitHub Actions runs a single job with an empty
matrix
context. This is done for a variety of reasons.We can prevent a job run with an empty matrix by adding a conditional to the job that verifies that at least one matrix record exists. For the
docker
job this conditional expression can be written like this: