← Back to Home
βš™οΈ

Workflow Anatomy

Read and write every part of a workflow file with confidence.

⏱11 min readπŸ“šDevOps Fundamentals

Now let’s dissect a workflow file part by part. Once you can read each section, you can build almost any pipeline you need.


🎯 Learning Objectives

By the end of this lesson you will:


Triggers β€” the on Key

The on key decides what events start the workflow:

yaml
on:
push:
  branches: [main]           # only pushes to main
pull_request:
  branches: [main]           # PRs targeting main
schedule:
  - cron: "0 6 * * *"        # every day at 06:00 UTC
workflow_dispatch:           # a manual "Run" button
Trigger Fires when
push Commits are pushed
pull_request A PR is opened/updated
schedule On a cron schedule
workflow_dispatch You click β€œRun workflow” manually

πŸ’‘ Filter to what matters

Restrict triggers with branches, paths, or tags so workflows only run when relevant β€” this saves runner minutes and avoids noise.


Jobs and Steps

A workflow has one or more jobs; each job has ordered steps:

yaml
jobs:
test:
  runs-on: ubuntu-latest       # the runner OS
  steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
      with:
        node-version: 20
    - run: npm ci               # install
    - run: npm test             # test

By default, jobs run in parallel. Steps within a job run in order, sharing the same runner and filesystem.


Job Dependencies

Use needs to make one job wait for another β€” essential for build β†’ deploy ordering:

yaml
jobs:
build:
  runs-on: ubuntu-latest
  steps:
    - run: echo "building"

deploy:
  needs: build            # only runs if build succeeds
  runs-on: ubuntu-latest
  steps:
    - run: echo "deploying"

⚠ Jobs don't share files automatically

Each job runs on a fresh runner. To pass files (like build output) from one job to another, upload them as an artifact in the first job and download them in the next. Steps within the same job do share files.


Matrix Builds

A matrix runs the same job across multiple configurations in parallel β€” perfect for testing several language versions:

yaml
jobs:
test:
  runs-on: ubuntu-latest
  strategy:
    matrix:
      node: [18, 20, 22]
  steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node }}
    - run: npm test

This automatically creates three parallel jobs β€” one each for Node 18, 20, and 22.


Caching for Speed

Reinstalling dependencies every run is slow. Cache them to speed things up dramatically:

yaml
- uses: actions/setup-node@v4
with:
  node-version: 20
  cache: 'npm'          # cache the npm dependency store

πŸ’‘ Expressions and context

The double-brace expression syntax is how workflows read context values β€” the commit SHA, a matrix value, or a secret β€” and inject them into steps. You’ll see it used constantly in the pipelines ahead.


πŸ§ͺ Hands-on Lab

πŸ“

Build a Matrix Test Workflow

  1. Write a workflow triggered on push to main and on pull_request
  2. Add a test job with a matrix of two Node versions
  3. Add a deploy job that needs the test job

🧠 Knowledge Check

Knowledge Check

By default, how do multiple jobs in a workflow run?

Knowledge Check

What is a matrix build used for?


πŸ’Ό Interview Preparation

Interview Q&A

How do you pass build artifacts between jobs in GitHub Actions?


Summary

You can now read and write every part of a workflow: triggers, jobs, steps, needs dependencies, matrix builds, and caching. Next, you’ll assemble these into a complete build-and-test CI pipeline for a real project.

Up Next

Building a CI Pipeline

You've mastered this lesson. Continue your journey to becoming a DevOps Engineer.

Start Next Lesson→