Your First Workflow
Build a real CI workflow — checkout, setup, install, test, lint, and build in one file
Skills taught: cicd.first-workflow, cicd.caching, cicd.artifacts, cicd.step-outputs
A complete CI workflow — step by step
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
# Cancel in-progress runs on new push (saves CI minutes)
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
ci:
name: Test, Lint & Build
runs-on: ubuntu-latest
steps:
# Step 1: Get the code
- name: Checkout
uses: actions/checkout@v4
# Step 2: Install Node.js with npm caching
- name: Setup Node.js 20
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm' # automatically caches ~/.npm between runs
# Step 3: Install dependencies (ci is faster + reproducible from lock file)
- name: Install dependencies
run: npm ci
# Step 4: TypeScript type check
- name: Type check
run: npx tsc --noEmit
# Step 5: ESLint
- name: Lint
run: npm run lint
# Step 6: Unit tests with coverage
- name: Test
run: npm test -- --coverage
# Step 7: Upload coverage report as artifact (viewable in GitHub UI)
- name: Upload coverage
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: coverage/
retention-days: 7
# Step 8: Build (only if all above pass)
- name: Build
run: npm run build
# Step 9: Upload build output as artifact
- name: Upload build
uses: actions/upload-artifact@v4
with:
name: dist
path: dist/
retention-days: 1 # short — used by next deploy job
Caching — the most impactful optimization
steps:
- uses: actions/checkout@v4
# actions/cache saves and restores a directory between runs
# Cache key: if package-lock.json changes, the cache is invalidated
- name: Cache node_modules
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
# With Turbo (monorepo):
- name: Cache Turborepo
uses: actions/cache@v4
with:
path: .turbo
key: ${{ runner.os }}-turbo-${{ github.sha }}
restore-keys: |
${{ runner.os }}-turbo-
- run: npm ci
- run: npm run build # turbo uses .turbo cache — only rebuilds what changed
Working with step outputs and environment
steps:
# Set an environment variable for all subsequent steps
- name: Set build vars
run: |
echo "BUILD_DATE=$(date -u +%Y-%m-%d)" >> $GITHUB_ENV
echo "IMAGE_TAG=${{ github.sha }}" >> $GITHUB_ENV
# Now $IMAGE_TAG is available as a normal shell variable
- name: Build Docker image
run: docker build -t myapp:$IMAGE_TAG .
# Step outputs (pass data to later steps in same job)
- name: Get version
id: version # give the step an id to reference its output
run: echo "version=$(cat package.json | jq -r .version)" >> $GITHUB_OUTPUT
- name: Print version
run: echo "Version is ${{ steps.version.outputs.version }}"
Tip: Use concurrency to cancel stale runs. When you push 5 commits quickly, you don't want 5 CI runs to complete — only the latest matters. The concurrency block cancels any in-progress run for the same branch when a new one starts. This can cut your CI bill by 60% on fast-moving branches.
Frequently Asked Questions
How do I view workflow run results?
Go to your repository on GitHub → Actions tab → click a workflow run → click a job → expand each step to see output and logs. You can also see the real-time stream while a workflow is running. Artifacts appear at the bottom of the workflow run summary page.