Opscraft
GitHub Actions · Lesson 3 of 10 · 3 min read

Jobs: dependencies, matrices, conditions and services

Dependencies and outputs

jobs:
  build:
    runs-on: ubuntu-latest
    outputs:
      tag: ${{ steps.meta.outputs.tag }}
    steps:
      - id: meta
        run: echo "tag=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
  deploy:
    needs: build                          # runs after build succeeds
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deploying ${{ needs.build.outputs.tag }}"

needs builds a DAG; if: always() or if: failure() on a dependent job runs it regardless or only on failure (e.g. notifications).

Enlarge diagrambuildoutputs: taglintneeds: buildtestneeds: builddeployneeds: [lint, test] · uses the tag
Figure 1. Jobs run in parallel unless needs links them. Here lint and test run side by side, and deploy waits for both and receives the image tag as an output.
Swipe sideways to see all of it, or tap to open it full screen.

Matrix builds

strategy:
  fail-fast: false              # let other combinations finish
  max-parallel: 4
  matrix:
    java: [17, 21]
    os: [ubuntu-latest, windows-latest]
    include:
      - java: 21
        os: ubuntu-latest
        coverage: true
    exclude:
      - java: 17
        os: windows-latest
Enlarge diagramtest (ubuntu, 17)test (ubuntu, 21)test (ubuntu, 25)test (windows, 17)test (windows, 21)test (windows, 25)java 17java 21java 25ubuntuwindowsfail-fast: false lets the other five finish when one fails.
Figure 2. A matrix turns one job definition into many: here 2 operating systems × 3 Java versions = 6 parallel jobs.
Swipe sideways to see all of it, or tap to open it full screen.

Conditions, timeouts and resilience

  • if: on jobs and steps: if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v').
  • timeout-minutes on jobs (default 360 minutes) — set it; hung jobs burn minutes.
  • continue-on-error: true for optional steps (e.g. an experimental linter).
  • Re-run failed jobs from the UI; there's no built-in step retry — use a retry action or a shell loop for flaky network calls.

Service containers and container jobs

jobs:
  integration:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:16
        env: { POSTGRES_PASSWORD: test }
        ports: ["5432:5432"]
        options: >-
          --health-cmd "pg_isready" --health-interval 5s --health-retries 10
    steps:
      - uses: actions/checkout@v4
      - run: ./gradlew integrationTest
        env: { DB_URL: "jdbc:postgresql://localhost:5432/postgres" }

container: runs the whole job inside a container image (consistent toolchain).

Enlarge diagramRUNNER VM (UBUNTU-LATEST)job stepscheckout · ./gradlew integrationTestservice: postgres:16health-cmd pg_isreadylocalhost:5432
Figure 3. Service containers: the job gets a real Postgres next to it for integration tests, started and health-checked before the steps run.
Swipe sideways to see all of it, or tap to open it full screen.