- Welcome my DevOps blog./
- 🔰Posts/
- 🗂️My Trainings/
- VCS Trainings and Certifications/
- GitHub Actions - The Complete Guide/
- GitHub Actions: Controlling Workflow and Job Execution/
GitHub Actions: Controlling Workflow and Job Execution
Table of Contents
| External Resources » | ||
|---|---|---|
| GitHub Actions official Documentation | GitHub Actions Marketplace | GitHub.com |
Controlling Execution Flow #
Using conditions to control job execution #
jobs.<job_id>.if conditional to prevent a job from running unless a condition is met. You can use any supported context and expression to create a conditional. For more information on which contexts are supported in this key, see Contexts reference.Step execution #
Example:
📄 File: .github/workflows/06-01-execution-flow.yml
| |
Note: id: can be injected to any step to be later called by any context object.
Note: failure function (failure() &&) must be present in order to force GitHub Actions no to follow it’s default behaviour (to stop executions if step fails).
Special Conditional Functions:
failure()- Returnstruewhen any previous Step or Job failed.success()- Returnstruewhen none of the previous Step or Job failed.always()- Causing the step to always execute, even when cancelled.cancelled()- Returnstrueif the workflow has been cancelled.
Job execution #
Example:
📄 File: .github/workflows/06-01-execution-flow.yml
| |
Example: report job depends on lint and deploy jobs (needs: [lint, deploy]). It will still run if any of the jobs fails.

continue-on-error: true changes the behaviour and sets the failed job to successful, even if it failed. Can be used for jobs that are allowed to fail.
Check contexts for more information.
Hint:
steps.<step_id>.conclusion | string | The result of a completed step after continue-on-error is applied. Possible values are success, failure, cancelled, or skipped. When a continue-on-error step fails, the outcome is failure, but the final conclusion is success. |
steps.<step_id>.outcome | string | The result of a completed step before continue-on-error is applied. Possible values are success, failure, cancelled, or skipped. When a continue-on-error step fails, the outcome is failure, but the final conclusion is success. |
More info:
- Using conditions to control job execution: https://docs.github.com/en/actions/how-tos/write-workflows/choose-when-workflows-run/control-jobs-with-conditions
- Contexts: https://docs.github.com/en/actions/reference/workflows-and-actions/contexts
- Operators: https://docs.github.com/en/actions/reference/workflows-and-actions/expressions#operators
Matrix Strategies #
How it Works #
Within a workflow file, you define a strategy block within a job, which contains a matrix key. Inside the matrix, you specify variables as keys and a list of values for each variable. GitHub Actions then automatically generates a separate, parallel job run for every possible combination of these values.
Example:
This example tests a Node.js application on two different operating systems and two different Node.js versions:
jobs:
build-and-test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node_version: [14, 16]
steps:
- uses: [actions/checkout@v4](github.com)
- name: Use Node.js ${{ matrix.node_version }}
uses: [actions/setup-node@v4](github.com)
with:
node-version: ${{ matrix.node_version }}
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
This configuration creates four jobs:
os: ubuntu-latest,node_version: 14os: ubuntu-latest,node_version: 16os: windows-latest,node_version: 14os: windows-latest,node_version: 16
Inclusions and Exclusions in Matrix strategies #
include keyword adds specific configurations or properties, while the exclude keyword removes unwanted combinations from the default matrix permutations. This provides fine-grained control over which jobs run in the workflow.Example:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
node: [14, 16]
include:
# Add a specific job for Node 18 on Ubuntu
- os: ubuntu-latest
node: 18
# Add an extra property (e.g., 'test-suite') to the macos-latest/Node 16 job
- os: macos-latest
node: 16
test-suite: "extended"
More info: Running variations of jobs in a workflow
🔥Re-Using Workflows #
Reusable workflows are YAML-formatted files, very similar to any other workflow file. As with other workflow files, you locate reusable workflows in the .github/workflows directory of a repository. Subdirectories of the workflows directory are not supported.
on must include workflow_call (See below example).📄 File: cicd-gh-actions-course/.github/workflows/06-04-reusable-workflow.yml
| |
Using inputs and secrets in a reusable workflow #
You can define inputs and secrets, which can be passed from the caller workflow and then used within the called workflow. There are three stages to using an input or a secret in a reusable workflow.
In the reusable workflow, use the
inputsandsecretskeywords to define inputs or secrets that will be passed from a caller workflow.on: workflow_call: inputs: config-path: required: true type: string secrets: personal_access_token: required: trueFor details of the syntax for defining inputs and secrets, see
on.workflow_call.inputsandon.workflow_call.secrets.In the reusable workflow, reference the input or secret that you defined in the
onkey in the previous step.Note
If the secrets are inherited by using
secrets: inheritin the calling workflow, you can reference them even if they are not explicitly defined in theonkey. For more information, see Workflow syntax for GitHub Actions.jobs: reusable_workflow_job: runs-on: ubuntu-latest steps: - uses: actions/labeler@v6 with: repo-token: ${{ secrets.personal_access_token }} configuration-path: ${{ inputs.config-path }}In the example above,
personal_access_tokenis a secret that’s defined at the repository or organization level.Warning
Environment secrets cannot be passed from the caller workflow as
on.workflow_calldoes not support theenvironmentkeyword. If you includeenvironmentin the reusable workflow at the job level, the environment secret will be used, and not the secret passed from the caller workflow. For more information, see Managing environments for deployment and Workflow syntax for GitHub Actions.Pass the input or secret from the caller workflow.
To pass named inputs to a called workflow, use the
withkeyword in a job. Use thesecretskeyword to pass named secrets. For inputs, the data type of the input value must match the type specified in the called workflow (either boolean, number, or string).
| |
Workflows that call reusable workflows in the same organization or enterprise can use the inherit keyword to implicitly pass the secrets.
| |
More info: Reuse workflows
» Sources « #
- Using conditions to control job execution: https://docs.github.com/en/actions/how-tos/write-workflows/choose-when-workflows-run/control-jobs-with-conditions
- Contexts: https://docs.github.com/en/actions/reference/workflows-and-actions/contexts
- Operators: https://docs.github.com/en/actions/reference/workflows-and-actions/expressions#operators
Matrix strategies:
» Disclaimer « #
This series draws heavily from Maximilian Schwarzmüller’s GitHub Actions - The Complete Guide course on Udemy.
| About the instructor: | |
|---|---|
| 🌐 Website | 📺 YouTube |
| 🗃️ GitHub |
| My Repos for this section: | |
|---|---|
| cicd-gh-actions-course | Learnings from "GitHub Actions - The Complete Guide" on Udemy. |
