on: β Defines the event that
triggers the workflow. Examples:
on: push β run when you push code.on: pull_request β run when PR is opened/updated.on: workflow_dispatch β manual trigger.jobs: β A workflow is made of one
or more jobs.
ubuntu-latest).needs:).steps: β Each job has steps (a
sequence of commands/actions).
uses: β Run a prebuilt GitHub
Action (from Marketplace). Example:
actions/checkout@v3.
run: β Run custom shell commands
(bash on Linux runners).
needs: β Makes jobs depend on other
jobs (controls order).
with: β Pass input values to
actions.
π Goal: Understand how workflows run.
π Workflow: .github/workflows/01-hello.yml
name: Hello World CI # Workflow name (shown in Actions tab)
on:
push: # Trigger event
branches: [ "main" ]
jobs:
hello: # Job ID
runs-on: ubuntu-latest # Runner machine
steps:
- name: Checkout repo
uses: actions/checkout@v3 # Action to fetch code
- name: Print message
run: echo "π Hello CI/CD World!" # Run shell commandβ Push β check Actions tab β see logs.
π Goal: Run scripts/tests.
π Workflow: .github/workflows/02-tests.yml
name: Run Tests
on: [push] # Short syntax (same as "on: push")
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18 # Specify Node.js version
- run: npm install
- run: npm run testβ
Add a simple npm run test script in
package.json.
π Goal: Test across multiple versions.
π .github/workflows/03-matrix.yml
name: Matrix Build
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy: # Define test matrix
matrix:
node: [18, 20] # Node.js versions
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm run testβ Runs tests on Node.js 16, 18, 20 simultaneously.
π Goal: Create real CI pipeline.
π .github/workflows/04-pipeline.yml
name: CI Pipeline
on: [push]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm install
- run: npm run lint
test:
runs-on: ubuntu-latest
needs: lint # Run only after lint succeeds
steps:
- uses: actions/checkout@v3
- run: npm install
- run: npm run test
build:
runs-on: ubuntu-latest
needs: test # Run only after test succeeds
steps:
- uses: actions/checkout@v3
- run: npm install
- run: npm run buildβ Enforces Lint β Test β Build order.
π Goal: Optimize workflow speed.
π .github/workflows/05-cache.yml
name: CI with Cache
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Cache node_modules
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- run: npm install
- run: npm run testβ Dependencies wonβt reinstall every time.
π Goal: Continuous Deployment.
π .github/workflows/06-deploy.yml
name: Deploy to GitHub Pages
on:
push:
branches: [ "main" ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18
- run: npm install
- run: npm run build
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./distβ Every push β deploys site to GitHub Pages.
β‘ Once you complete all 6 stages, youβll fully master GitHub Actions CI/CD basics + deployment.