GitHub Actions
- React course
- Advanced
- Lesson 50/56
- 4 min
Learn to write a CI workflow for a React project.
GitHub Actions
GitHub Actions works by placing YAML files in the .github/workflows/ folder. Each file is a workflow.
A workflow contains jobs; each job runs on a runs-on virtual machine. Steps run a ready-made action with uses or a shell command with run.
Common actions: actions/checkout pulls code, actions/setup-node installs Node.
Cache and speed
Each workflow starts from scratch; without a cache npm ci re-downloads everything. The actions/setup-node action reuses the node_modules cache with the cache: 'npm' option.
Caching speeds up the workflow by minutes and lowers the bill. The key depends on the package-lock.json hash; when the lock changes the cache refreshes. In large projects this gap reaches several minutes and clearly reduces cost.
Practice Write the Build workflow
Write a build workflow. Use name: Build, on: push. Set up setup-node@v4 with node-version: '20' and cache: 'npm'. Run npm ci && npm run build.
Starter code
# Write the Build workflow hereSample solution
name: Build
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci && npm run buildQuestions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Fill in the blank: set up Node
- uses: actions/setup-node@v4
with:
node-version: '____'
- uses: actions/setup-node@v4
with:
node-version: '20'
Explanation: de-version: 20 is a common choice.
2 What does cache: 'npm' do in setup-node?
- Pins the Node version
- Reuses the npm dependency cache Correct answer
- Stores test results
- Skips building the code
Explanation: The cache speeds up dependency download.
3 Match Actions concepts
| Term | Meaning |
|---|---|
checkout |
Pulls the code |
setup-node |
Installs Node |
cache |
Reuses cache |
run |
Runs a shell command |