GitHub Actions in depth

ExplainedCI/CDDevOps, Hands OnBy Cloud Hands On

Triggers, runners, contexts, permissions, matrices, caching, reusable workflows and OIDC: the parts you use once past the first workflow.

A workflow runs on events: push, pull_request, a schedule, a manual workflow_dispatch with inputs, a tag, or a call from another workflow. Each job gets a fresh runner (GitHub-hosted, or your own self-hosted machine) and runs its steps in order, each either a shell command (run:) or an action (uses:).

Expressions in ${{ }} read contexts such as github (the event, branch and SHA), secrets, inputs, matrix and needs (outputs of earlier jobs). The GITHUB_TOKEN is created for each run; the permissions: block sets what it may do, and it's good practice to start from contents: read.

Beyond the basics: matrices multiply jobs, caches save downloads, artifacts pass files, concurrency stops overlapping runs, environments add approvals and their own secrets, reusable workflows and composite actions share YAML, and OIDC lets a job sign in to Azure, AWS or Google Cloud without a stored secret.

Cost. Free for public repositories. Private repositories get a monthly allowance of minutes and storage, then pay per minute.

Use it when

How it works

Events
What starts the run: push, pull_request, schedule, workflow_dispatch, workflow_call.
Jobs and runners
Each job on a fresh machine; needs joins them.
Contexts
github, secrets, inputs, matrix, needs, env.
Permissions
What the run's token can do; least privilege.
Reuse
Reusable workflows, composite actions, marketplace actions pinned by SHA.

Options

GitHub-hosted runners
Nothing to look after; clean every time.
Larger runners
More CPU and memory, paid per minute.
Self-hosted runners
Your network and hardware; you keep them patched and safe.

Example. SS Kaiser reviewed their workflows and found three using pull_request_target to run tests on fork pull requests with secrets available. They moved tests to pull_request and kept pull_request_target only for labelling, which needs no checkout.

Don't confuse it with

pull_request
Runs the PR's code with a read-only token and no secrets for forks.
pull_request_target
Runs the base branch's workflow with secrets; dangerous if it checks out PR code.

Words to know

Event
What triggers a workflow.
Context
A set of values expressions can read, such as github or secrets.
GITHUB_TOKEN
A token created for each run, limited by permissions:.
workflow_dispatch
A trigger for running a workflow by hand, with inputs.

Where to look

  1. Repository
  2. .github/workflows
  3. Actions tab

Practise it

Read it in the guide, next to the tasks and labs.

Open in DevOps, Hands On

Keep going