Dev tools
GitHub Actions: A Practical Guide to CI/CD
GitHub Actions step by step: workflows, jobs and runners, a first CI workflow, caching, matrices, secrets, deployments, security hardening and costs.
GitHub Actions is GitHub's built-in automation platform. You describe workflows in YAML files inside your repository, and GitHub runs them on virtual machines when something happens: a push, a pull request, a release, a schedule or a manual click. Most teams use it for CI/CD: running tests and linters on every pull request, building artifacts and container images, and deploying to staging and production.
Because it lives next to the code, there's no separate CI server to set up, and thousands of reusable actions cover common tasks from checking out code to deploying to every major cloud. That convenience comes with responsibilities: workflows run with access to your code and secrets, so they need the same care as production code. This GitHub Actions tutorial explains the core concepts, builds a first CI workflow step by step, adds caching, test matrices and deployments, and covers secrets, security hardening, costs and debugging.
GitHub Actions at a Glance
| Concept | What it is |
|---|---|
| Workflow | A YAML file in .github/workflows/ describing automation |
| Event (trigger) | What starts a workflow: push, pull_request, schedule, workflow_dispatch, release and many more |
| Job | A set of steps that runs on one runner; jobs run in parallel unless they depend on each other |
| Step | A single shell command (run) or a reusable action (uses) |
| Action | A reusable unit, such as actions/checkout, published in a repository or the Marketplace |
| Runner | The machine that executes a job: GitHub-hosted (Linux, Windows, macOS) or self-hosted |
| Secret | An encrypted value, such as an API key, available to workflows |
| Environment | A deployment target (for example production) with its own secrets and protection rules |
| Artifact | Files saved from a run, such as build outputs or test reports |
| Cost | Standard GitHub-hosted runners are free for public repositories; private repositories use the plan's included usage, then pay as you go |
What Is GitHub Actions?
What is GitHub Actions in plain terms? It's an event-driven automation service built into GitHub. GitHub added CI/CD capabilities to Actions in 2019, and it has since become one of the most widely used CI/CD systems, especially for open-source projects, which run on standard hosted runners for free.
The model is simple: when an event happens, run these jobs on these machines. A workflow can do almost anything you can script: run tests, build Docker images, publish packages, label issues, update documentation, send notifications or deploy infrastructure.
A GitHub Actions Tutorial: Your First CI Workflow
This step-by-step example adds continuous integration to a Node.js project. The same pattern works for any language.
Step 1: Create the workflow file
In your repository, create .github/workflows/ci.yml:
name: CI
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run lint
- run: npm testStep 2: Understand what it does
onruns the workflow for pushes tomainand for every pull request.permissions: contents: readlimits what the automaticGITHUB_TOKENcan do, a security best practice.runs-on: ubuntu-latestpicks a GitHub-hosted Linux runner.actions/checkoutfetches your code;actions/setup-nodeinstalls Node.js and caches npm downloads.- The
runsteps install dependencies exactly from the lock file, lint and test.
Step 3: Commit and watch it run
Push the file. Open the Actions tab in your repository to see the run, with live logs for every step. On pull requests, the result appears as a check, and you can make it required in the branch protection rules or rulesets, so code can't merge until CI passes.
Step 4: Add a status badge (optional)
The workflow page offers a badge you can paste into your README, showing whether the main branch is passing.
A Fuller GitHub Actions Workflow
As projects grow, a GitHub Actions workflow usually picks up a few more features.
Test matrices
Test several versions or operating systems in parallel:
jobs:
test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node: [20, 22]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci
- run: npm testThat creates four jobs, one per combination. fail-fast: false lets the others finish even if one fails, so you see every broken combination at once.
Caching
Downloading dependencies on every run wastes minutes. Many setup actions have built-in caching (as with cache: npm above); for anything else, actions/cache stores and restores folders keyed on a hash of your lock file.
Job dependencies and artifacts
Jobs run in parallel by default. Use needs to order them and artifacts to pass files between them:
build:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run build
- uses: actions/upload-artifact@v4
with:
name: dist
path: dist/Avoiding duplicate runs
Add a concurrency group so a new push cancels the previous, now outdated run on the same branch:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: trueDeploying With GitHub Actions
For CD, add a deploy job that runs only on the main branch and targets an environment:
deploy:
needs: build
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production
permissions:
contents: read
id-token: write
steps:
- uses: actions/download-artifact@v4
with:
name: dist
- run: ./scripts/deploy.shEnvironments add control:
- Required reviewers: a person must approve before the job runs.
- Wait timers and branch restrictions: only certain branches can deploy.
- Environment secrets: production credentials are only available to jobs targeting production.
The id-token: write permission enables OpenID Connect (OIDC). Instead of storing long-lived cloud keys as secrets, the workflow requests a short-lived token that your cloud provider (AWS, Azure, Google Cloud and others) trusts for this specific repository and branch. It's the recommended way to deploy to the cloud from Actions.
GitHub Actions Secrets
GitHub Actions secrets store sensitive values such as API keys and passwords, encrypted at rest and masked in logs. They can be set at three levels:
| Level | Scope | Typical use |
|---|---|---|
| Repository | All workflows in one repository | Tokens for a single project |
| Environment | Jobs targeting that environment | Production and staging credentials |
| Organisation | Selected repositories in an organisation | Shared tokens, such as a package registry |
Use them in a workflow as ${{ secrets.NAME }}, usually passed into a step's environment. Rules that matter:
- Prefer OIDC over stored cloud credentials where possible.
- Secrets aren't passed to workflows triggered by pull requests from forks, which protects public repositories. Be very careful with the
pull_request_targettrigger, which does run with secrets in the context of the base repository. - Never echo secrets or write them to files that become artifacts.
- Rotate secrets periodically and after any suspected exposure.
- Keep `GITHUB_TOKEN` permissions minimal with the
permissionskey, as in the examples above.
Security Hardening
Workflows run code with access to your repository and secrets, which makes them a target. A real example: in March 2025, the popular third-party action tj-actions/changed-files was compromised. Attackers repointed its version tags to malicious code that printed CI secrets into workflow logs, exposing secrets in public repositories that used it. The incident was tracked as CVE-2025-30066 and added to CISA's Known Exploited Vulnerabilities catalogue.
The lessons, as a checklist:
- Pin third-party actions to a full commit SHA, not a tag such as
@v45, because tags can be moved. Add a comment with the version for readability, and let a dependency bot propose updates. - Prefer actions from GitHub and well-known publishers, and review what others do before using them.
- Set `permissions` explicitly at the workflow or job level, starting from read-only.
- Use OIDC for cloud access instead of long-lived keys.
- Protect environments with required reviewers for production.
- Treat user-controlled input as untrusted: don't interpolate issue titles, branch names or pull request text directly into
runscripts. - Restrict self-hosted runners to private repositories you control; never let public pull requests run on them.
Our guide to software supply chain security covers the wider picture.
Runners and Costs
| Runner type | What it is | Cost model |
|---|---|---|
| Standard GitHub-hosted | Fresh Linux, Windows or macOS VM per job | Free in public repositories; private repositories draw on included usage, then are billed per minute |
| Larger GitHub-hosted | More CPU and memory, GPU options | Billed, including for public repositories |
| Self-hosted | Your own machines or cloud instances | You pay for the infrastructure; useful for special hardware or networks |
On GitHub Actions cost, minute rates differ by operating system, with macOS and Windows costing more than Linux, so keep most jobs on Linux. Check GitHub's current billing documentation for your plan's included usage and rates.
To cut usage: cache dependencies, cancel outdated runs with concurrency, use path filters so documentation changes don't run full test suites, and in a monorepo, run only the jobs affected by a change. Our comparison of monorepo vs polyrepo covers affected-only builds.
Reusable Workflows and Composite Actions
When several repositories need the same pipeline, don't copy it:
- Reusable workflows (
on: workflow_call) let one workflow call another, centralising a standard CI or deploy pipeline. - Composite actions bundle several steps into one action you can
uses:anywhere.
Both reduce drift and make security fixes a one-place change.
Debugging Failing Workflows
- Read the step logs in the Actions tab; expand the failing step.
- Re-run with debug logging enabled from the run page for more detailed output.
- Check the event: many surprises come from triggers (
pull_requestvspush) and branch filters. - Validate YAML: indentation errors are the most common cause of workflows not starting.
- Reproduce locally with community tools such as
act, which runs workflows in containers, keeping in mind it isn't identical to GitHub's runners.
GitHub Actions vs Other CI Tools
| GitHub Actions | GitLab CI/CD | CircleCI | Jenkins | |
|---|---|---|---|---|
| Where it lives | Built into GitHub | Built into GitLab | Separate SaaS | Self-hosted server |
| Configuration | YAML per workflow | .gitlab-ci.yml | YAML | Groovy pipelines or UI |
| Reuse | Marketplace actions, reusable workflows | Templates, components | Orbs | Plugins, shared libraries |
| Best for | Projects already on GitHub | Projects on GitLab | Teams wanting a dedicated CI service | Full control, on-premises needs |
If you're weighing a move away from GitHub entirely, our guide to GitHub alternatives compares the options and their CI.
Common Mistakes
- Broad default token permissions instead of explicit read-only.
- Third-party actions pinned to tags rather than commit SHAs.
- Secrets in logs from debug
echostatements. - No caching, making every run slow and expensive.
- Running everything on macOS or Windows when Linux would do.
- Using `pull_request_target` to run untrusted code with secrets.
FAQ
Is GitHub Actions free?
Standard GitHub-hosted runners are free for public repositories. Private repositories use the plan's included usage, then pay as you go; self-hosted runners don't consume GitHub-hosted minutes.
Where do GitHub Actions workflows live?
In YAML files under .github/workflows/ in your repository.
What is the difference between a workflow, a job and a step?
A workflow is the whole automation file. It contains jobs, each running on its own runner. Each job is a sequence of steps, which are commands or actions.
Can GitHub Actions deploy to AWS, Azure or Google Cloud?
Yes. The recommended approach uses OpenID Connect so the workflow gets short-lived cloud credentials without storing long-lived keys.
Can I run GitHub Actions on my own servers?
Yes, with self-hosted runners. Use them for special hardware or private networks, and avoid exposing them to untrusted public pull requests.