GitHub Actions and CI/CD
6 learning objectives · 12 key terms · 8 commands · 3 diagrams · 5 quiz questions
GitHub Actions and CI/CD
Automation & SecurityWhat you'll be able to do
- Explain CI and CD and why automation changes a team’s release confidence
- Write a workflow file and know where it must live in the repository
- Use events, jobs, steps, runners and actions correctly
- Pass configuration and secrets to a workflow without leaking them
- Cache dependencies, upload artefacts and run matrix builds
- Trigger workflows on a schedule or by hand, and gate a pull request on checks
The idea, in plain language
Continuous integration means every change is automatically built and tested as soon as it is pushed, so integration problems surface in minutes rather than at release time. Continuous delivery extends that to producing a deployable artefact on every green build, and continuous deployment goes one step further and ships it. GitHub Actions is the automation engine that runs all of it. A workflow is a YAML file in .github/workflows/; it declares what triggers it, what jobs to run, and what steps each job performs.
The execution model is worth internalising. An event (a push, a pull request, a schedule, a manual dispatch) starts a workflow run. Each job in the workflow runs on its own fresh virtual machine — a runner — so jobs are isolated and can run in parallel unless you explicitly declare needs. Within a job, steps run in order on the same machine, which is why checking out the code and installing language runtimes are steps rather than jobs. Each step is either a shell command (run:) or a reusable action (uses:) from the marketplace or another repository.
Two habits separate workflows that help from workflows that annoy. First, keep them fast: cache dependency directories and only run what the change affects. Second, treat secrets properly — repository or environment secrets are injected as environment variables or step inputs and are masked in logs, but a secret printed by a malicious script cannot be unmasked, so never echo them and never expose secrets to workflows triggered by fork pull requests.
Key terms
- Workflow
- A YAML file in .github/workflows/ that defines one automated process.
- Event / trigger
- What starts a run — on: push, pull_request, schedule, workflow_dispatch, release and many more.
- Job
- A group of steps that run on one runner; jobs run in parallel by default.
- Step
- One run: shell command or one uses: action inside a job.
- Runner
- The fresh virtual machine a job executes on (ubuntu-latest, windows-latest, macos-latest, or self-hosted).
- Action
- A reusable unit of work, such as actions/checkout to clone the repository into the runner.
- Secret
- An encrypted value stored in repository security settings, injected at run time and masked in logs.
- Artefact
- A file produced by a job and kept for download or for a later job to consume.
- Cache
- Dependency data persisted between runs to cut install time.
- Matrix build
- Running the same job across a set of parameter combinations, such as three language versions.
- Required status check
- A workflow result that branch protection insists is green before merging.
- workflow_dispatch
- The trigger that adds a manual Run workflow button.
How it works, step by step
- 1Create the workflow directory
The path is fixed: .github/workflows/ at the repository root, with a .yml or .yaml extension. Any file there is picked up automatically — no registration step.
one-time setupbashmkdir -p .github/workflows
- 2Declare name and triggers
Restrict triggers to the branches you care about, otherwise every scratch branch starts a run and the queue becomes noise.
triggersyamlname: Run Tests on: push: branches: [main] pull_request: branches: [main] workflow_dispatch:
- 3Define a job and its runner
Each job gets an id, a human-readable name, and the machine type it runs on. jobs.<id>.runs-on is required.
jobyamljobs: test: name: Unit tests runs-on: ubuntu-latest
- 4Write the steps in the order they must happen
Check out the repository first, then install a runtime, then restore caches, then install dependencies, then run tests. Steps share the runner’s filesystem, so order matters.
stepsyamlsteps: - name: Checkout repository uses: actions/checkout@v4 - name: Set up runtime uses: actions/setup-node@v4 with: node-version: 20 cache: npm - name: Install dependencies run: npm ci - name: Run tests run: npm test
- 5Pass configuration and secrets
Use env: for plain configuration and secrets at the job or step level for anything sensitive. Reference a repository secret with the secrets context, and never echo it.
env and secretsyamlenv: NODE_ENV: test steps: - name: Publish coverage run: ./scripts/publish.sh env: COVERAGE_TOKEN: ${{ secrets.COVERAGE_TOKEN }}
- 6Keep runs fast with cache and matrix
Caching dependency directories turns a three-minute install into ten seconds. A matrix runs the same job across several versions at once, catching compatibility problems early.
matrixyamlstrategy: fail-fast: false matrix: node: [18, 20, 22]
- 7Keep results and gate the merge
Upload artefacts for anything you want to download later, and configure the workflow as a required status check so a red run blocks merging instead of being ignored.
artefactsyaml- name: Upload coverage report uses: actions/upload-artifact@v4 with: name: coverage path: coverage/
Commands worth memorising
Every block below is copy-ready — use the Copy button, then paste it into your terminal. Expected output is shown underneath so you know what “working” looks like.
gh workflow listRun Tests active 25183446201 test.yml Release active 25183446202 release.yml
Confirms GitHub has picked up the files in .github/workflows/ and whether they are enabled.
gh run list --limit 5
✓ main Run Tests 25183446201 1m12s ✕ feature/x Run Tests 25183446177 48s
The quickest way to see whether main is currently green.
gh run watch
✓ main Run Tests · 25183446201 ✓ Set up job ✓ Checkout repository ✓ Run tests
Follows the latest run for the current branch and exits non-zero on failure.
gh run view --log-failed
Prints the failing step log alone. Far faster than paging through a whole run.
gh run rerun 25183446177 --failed
Re-runs only the failed jobs, which is useful after a transient network error.
gh workflow run test.yml --ref feature/login
Requires workflow_dispatch in the workflow. Handy for testing workflow changes without an empty commit.
gh secret set COVERAGE_TOKEN✓ Set Actions secret COVERAGE_TOKEN for ada/app
Reads the value from a prompt or stdin, so it never appears in your shell history. Use gh secret list to audit names only.
gh api repos/:owner/:repo/actions/workflows/test.yml/runs --jq '.workflow_runs[0:10] | .[] | .conclusion'
"success" "failure" "success"
Useful for spotting flaky tests: a run of failures and successes on the same code is a signal.
Workflow diagrams
The whole cycle is typically a couple of minutes, and the result is attached to the commit and the pull request.
- A push or pull request event matches the workflow’s on: filters and starts a run.
- Each job gets a fresh runner — nothing is left over from the previous run.
- Steps run in order on that machine: checkout, runtime setup, cache restore, install, test.
- The overall result appears on the commit and on the pull request, and can be required before merging.
| Key | Meaning | Example |
|---|---|---|
| name | Label shown in the Actions tab | name: Run Tests |
| on | Events that trigger the workflow | on: push, pull_request, schedule, workflow_dispatch |
| jobs | One or more jobs, run in parallel unless needs is set | jobs: test:, build:, deploy: |
| steps | Ordered commands inside a job | - uses: actions/checkout@v4 / - run: npm test |
| env / secrets | Configuration and encrypted values | env: NODE_ENV / secrets: COVERAGE_TOKEN |
Five keys cover the vast majority of real workflows.
- on: decides when; jobs: decides what; steps: decides how.
- Jobs run in parallel by default — add needs: when one must wait for another.
- Secrets are referenced through the secrets context and are masked in the logs.
Jobs are isolated, so build and deploy each start from a fresh machine and pass artefacts between them.
- The test job runs on every push and pull request and is the gate for everything after it.
- The build job declares needs: test, so it only starts when tests pass — and it starts on a fresh runner, so it downloads the test job’s artefacts rather than reusing files.
- The deploy job can require approval through an environment, which is how continuous delivery is kept safe.
- A failure anywhere in the chain leaves the later jobs skipped and the commit marked red.
Worked example
A complete, beginner-friendly workflow: it tests on every push and pull request, caches dependencies, and uploads a coverage report.
name: Run Tests on: push: branches: [main] pull_request: branches: [main] workflow_dispatch: jobs: test: name: Test on Node ${{ matrix.node }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node: [18, 20, 22] steps: - name: Checkout repository uses: actions/checkout@v4 - name: Set up runtime uses: actions/setup-node@v4 with: node-version: ${{ matrix.node }} cache: npm - name: Install dependencies run: npm ci - name: Run tests run: npm test -- --coverage - name: Upload coverage report if: matrix.node == 20 uses: actions/upload-artifact@v4 with: name: coverage path: coverage/
Actions tab shows 3 parallel jobs (Node 18, 20, 22). A green tick appears on the commit and on the pull request; a failure blocks a protected main branch.
Where you'll use it
Common mistakes
- Printing secrets in logs — masked values can be revealed by transforms and encoded output.
- Running workflows on every branch, so the queue fills with runs nobody wants.
- Forgetting actions/checkout, then wondering why the workspace is empty.
- Hard-coding dependency versions in the workflow instead of using the lockfile, so CI and production drift apart.
- Exposing secrets to pull requests from forks, which is how credentials leak from public repositories.
- Trusting third-party actions without pinning them to a version or commit SHA.
- Letting a required check be flaky and then disabling it — a disabled check is worse than none.
Best practices
- Keep CI under about ten minutes; cache dependencies and run only what changed.
- Pin actions to a major version at minimum, ideally a commit SHA, especially for third-party actions.
- Make the test job a required status check on protected branches.
- Store every credential in repository or environment secrets — never in the workflow file.
- Use environments with required reviewers for production deploys.
- Fail fast, but let matrix jobs finish (fail-fast: false) so you see the whole picture.
- Upload logs and artefacts on failure so debugging does not require a re-run.
Hands-on exercise
Add CI to one of your repositories
A green check on every push, then make it gate a merge.
- Create .github/workflows/ci.yml with a name, an on: block for push and pull_request to main, and a single job on ubuntu-latest.
- Add steps to checkout the repository, set up your language runtime, install dependencies and run your tests or linter.
- Push the workflow on a branch, open a pull request, and watch the run start in the Actions tab. Fix whatever fails first.
- Add cache support (for example, cache: npm on setup-node) and compare the run times before and after.
- Add a second job that depends on the first with needs:, and give it something useful to do such as building the project.
- Configure branch protection on main with the test job as a required status check, then deliberately break a test and confirm the merge button is blocked.
💡 Hint: You can re-run a failed workflow from the Actions tab, and workflow_dispatch adds a manual button so you do not need to push a commit to test changes to the workflow itself.
Done when: Every push to a branch in a pull request starts a workflow, the check appears on the pull request, and a failing test blocks the merge.
Checkpoint quiz
0 of 5 answered · 0 correct
- Multiple choiceQ1
Where must a workflow file live?
- Command outputQ2
What does this job configuration do?
strategy: matrix: node: [18, 20, 22] - TroubleshootingQ3
Your workflow fails immediately with "no such file or directory: package.json". The repository definitely contains it. What is the most likely cause?
- True or falseQ4
True or false: secrets are safe to use in a workflow triggered by a pull request from a fork.
- Multiple choiceQ5
What is the benefit of caching dependencies in a workflow?
Module summary
- CI tests every change automatically; CD adds artefact production and deployment.
- Workflows live in .github/workflows/ and are defined by on:, jobs:, steps:, env: and secrets:.
- Jobs run on fresh runners and in parallel unless you chain them with needs:.
- Checkout, set up the runtime, restore the cache, install, then test — in that order.
- Secrets belong in repository or environment settings, never in the YAML, never echoed, never exposed to fork pull requests.
- Cache, matrix and required status checks are what make CI fast enough and firm enough to trust.
- Environments with required reviewers are how production deploys stay deliberate.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Put the GitHub course to work: open one of the company-wise repositories and follow the workflow you just learned, branch to release.