Key Concepts Learned
on:
push:
branches: [ master, feature/github-actions-practice ]
pull_request:
branches: [ master ]
workflow_dispatch:push: Runs when code is pushed to specified branchespull_request: Runs when PR is created/updatedworkflow_dispatch: Allows manual triggering from GitHub UI
jobs:
build:
runs-on: ubuntu-latest- Each workflow can have multiple jobs
- Jobs run in parallel by default
runs-onspecifies the runner (Ubuntu, Windows, macOS)
Two types of steps:
Action Steps (uses):
- name: Checkout code
uses: actions/checkout@v4Command Steps (run):
- name: Install dependencies
run: npm cinpm install |
npm ci |
|---|---|
| Updates package-lock.json | Requires package-lock.json |
| Installs latest compatible versions | Installs exact versions |
| Slower | Faster |
| Good for development |
Perfect for CI/CD
Q1: What is the difference between npm install and npm ci?
Answer:
npm ci(clean install) is designed for CI/CD environments- It's faster because it skips certain user-facing features
- It requires
package-lock.jsonand installs exact versions - It removes
node_modulesbefore installing (clean slate) npm installcan modifypackage-lock.json, whilenpm cinever does
Q2: Why do we need actions/checkout@v4?
Answer:
- GitHub Actions runners start with an empty workspace
actions/checkout@v4clones your repository code into the runner- Without it, your code wouldn't be available for testing/building
- The
@v4specifies the version of the action to use
Q3: What does runs-on: ubuntu-latest mean?
Answer:
- Specifies which operating system to run the job on
ubuntu-latestuses the latest Ubuntu Linux version (currently 22.04)- Other options:
windows-latest,macos-latest - GitHub provides free runners for public repositories
Q4: What is workflow_dispatch and when would you use it?
Answer:
- Allows manual triggering of workflows from GitHub UI
- Useful for:
- On-demand deployments
- Manual testing
- Workflows that shouldn't run automatically
- Workflows with user inputs
- Can accept input parameters for customization
Answer: Steps execute sequentially within a job:
- Checkout code from repository
- Setup Node.js environment (version 22)
- Install dependencies using npm ci
- Run tests (fails if tests fail)
- Build project (only runs if tests pass)
If any step fails, subsequent steps are skipped and the workflow fails.