Skip to content

Chapter 9 of 9

Best Practices and Optimization

Build secure, fast, observable, and maintainable GitHub Actions automation.

38 minutes 10 quick checksBy Subha Prasad
Lesson 9 of 9Course navigation

Lesson content

Read, practise, then check your understanding

Best Practices and Optimization

A production workflow should be correct before it is fast. Optimize the critical feedback path, reduce privileges and supply-chain risk, make failures diagnosable, and ensure deployment has a safe recovery path.

Core ideas

  • Declare default token permissions as read-only or empty, then grant narrow job-level permissions.
  • Concurrency groups prevent stale pull-request checks or overlapping deployments from wasting capacity.
  • Timeouts and cancellation keep hung builds from occupying runners.
  • Clear job names, step summaries, annotations, retained logs, and diagnostic artifacts reduce recovery time.

How it works

Measure queue duration, setup time, build time, cache effectiveness, and flaky failure rate. Parallelize independent jobs, cache only deterministic dependencies, and avoid installing tools already present when the pinned version is acceptable. Reliability gains usually outweigh micro-optimizations.

Configuration example

name: Production delivery
on:
  push:
    branches: [main]
permissions:
  contents: read
concurrency:
  group: deploy-production
  cancel-in-progress: false

jobs:
  deploy:
    timeout-minutes: 20
    environment: production
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
    steps:
      - run: ./scripts/deploy.sh

Read the example from top to bottom: the trigger creates a run, the job requests a runner, and each step receives only the context configured for it. Adapt names, versions, permissions, and commands to the repository rather than copying production credentials or policies blindly.

Production guidance

  • Pin action dependencies, review automated upgrades, and inventory workflow permissions.
  • Use branch protection and required checks for the validation contract.
  • Keep deployment idempotent, record the deployed revision, and rehearse rollback.

The chapter quiz follows this lesson and checks both the vocabulary and the operational decisions behind the configuration.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement correctly describes least-privilege permissions?
Which term matches this explanation: Granting GITHUB_TOKEN only the capabilities a job requires.
Which statement correctly describes concurrency control?
Which term matches this explanation: Grouping runs so stale or conflicting work can be cancelled or serialized.
Which statement correctly describes timeout-minutes?
Which term matches this explanation: A job limit that prevents stuck automation from consuming runners indefinitely.
Which statement correctly describes full-SHA pinning?
Which term matches this explanation: Referencing third-party action code by an immutable commit identifier.
Which statement correctly describes workflow observability?
Which term matches this explanation: Using clear names, summaries, logs, and artifacts to diagnose automation.

0 of 10 checks passed

Your progress is saved on this device.