GitHub Actions makes it easy to automate all your software workflows, including CI/CD. Workflows are defined using YAML files stored in the .github/workflows/ directory of your repository.
Basic Workflow Structure (.github/workflows/main.yml)
name: CI/CD Pipeline
# When does the workflow run?
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
workflow_dispatch: # Allows manual triggering from the GitHub UI
# What does the workflow do?
jobs:
build:
runs-on: ubuntu-latest # The runner environment
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm testCommon Triggers (on)
on:
push:
tags:
- 'v*.*.*' # Trigger on version tags (e.g., v1.0.0)
schedule:
- cron: '0 0 * * *' # Run daily at midnight UTC
repository_dispatch:
types: [custom_event] # Trigger via APIEnvironment Variables and Secrets
jobs:
deploy:
runs-on: ubuntu-latest
env:
NODE_ENV: production # Available to all steps in this job
steps:
- name: Deploy to Server
run: ./deploy.sh
env:
API_KEY: ${{ secrets.PROD_API_KEY }} # Accessing a GitHub SecretMatrix Builds
Run a job multiple times with different variable combinations (e.g., testing against multiple Node versions).
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14, 16, 18]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm test