Write your first workflow
In this task you'll run a build and test on every push and pull request.
A GitHub Actions workflow is a YAML file in .github/workflows. It says when to run (on push, on pull request, on a schedule), on which runner (a fresh virtual machine) and which steps to run.
The first workflow every repository needs is the one that checks each change: it fails the PR if the code doesn't build or a test fails.
Example. SS Kaiser's shop repository runs a workflow on every pull request that lints the HTML and runs the unit tests; a red cross on the PR means it can't be merged.
Before you start
You need: A GitHub repository. Time: 20 minutes.
Where to look
- Repository
- .github/workflows/ci.yml
- Actions tab
Steps
- In your repository, create .github/workflows/ci.yml with the example in Commands.on: lists the triggers; jobs: the work.
- Commit it to a branch and open a pull request.The workflow runs and its result appears on the PR.
- Open the Actions tab, select the run and read each step's log.
- Make the test fail on purpose, push, and see the PR turn red. Then fix it.
Commands
name: CI
on:
push:
branches: [ main ]
pull_request:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Check the home page exists and has a title
run: grep -q "<h1>" index.html
Watch out for
- Cost
- Free for public repositories; private ones get a monthly allowance of minutes.
- Security
- Pin third-party actions to a version (or a commit SHA) and give the workflow only the permissions it needs.
- Common mistakes
- Running deploy steps on pull requests from forks, and not setting permissions (the default token can do more than you need).
Clean up
Nothing to clean up. Disable the workflow from the Actions tab if you don't want it to run.
Words to know
- Workflow
- A YAML file that defines automated jobs.
- Runner
- The machine a job runs on. GitHub-hosted runners are new and empty each time.
- Step
- One command or action inside a job.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On