सामग्री पर जाएँ

लेसन 30 · CI/CD Patterns

Running Packer in GitHub Actions

Build images in GitHub Actions: setup-packer, init, validate and build jobs, OIDC to AWS without stored keys, matrix builds per source, caching plugins and pull request versus main behavior.

Building images from a laptop does not scale: results depend on one person's tools and credentials, and nobody can audit what happened. A CI system makes builds repeatable, reviewable and permission-controlled. This lesson uses GitHub Actions for examples, but every concept maps to GitLab CI, Jenkins or any other runner.

Workflows cannot execute on this site. The YAML is read-only; JavaScript stand-ins model the decisions a pipeline makes (which jobs run, how a matrix expands). The action versions shown are examples: check each action's page for the current release and pin to a commit or tag you trust.

Pipeline shape

flowchart LR
  PR["Pull request"] --> L["fmt + validate"]
  L --> T["Optional test build"]
  M["Merge to main"] --> B["Build images"]
  B --> V["Test instance"]
  V --> R["Publish / promote"]

Pull requests only need the cheap checks; merges to main build and publish.

A pull request workflow

YAML
name: packer-validate
on:
  pull_request:
    paths: ["images/**", ".github/workflows/packer-*.yml"]

jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: hashicorp/setup-packer@main
        with:
          version: "1.11.2"
      - run: packer fmt -check -diff -recursive images/
      - run: packer init images/
      - run: packer validate -var-file=images/ci.pkrvars.hcl images/

Pin the Packer version so local and CI behave the same. Use paths so unrelated changes do not trigger image jobs.

A build workflow with OIDC

Static cloud keys stored in repository secrets are long-lived and easy to leak. With OpenID Connect, the job receives a short-lived token from GitHub and exchanges it for temporary cloud credentials. For AWS:

YAML
name: packer-build
on:
  push:
    branches: [main]
    paths: ["images/**"]
  schedule:
    - cron: "0 3 * * 1"

permissions:
  id-token: write
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        source: ["amazon-ebs.web", "amazon-ebs.worker"]
    steps:
      - uses: actions/checkout@v4
      - uses: hashicorp/setup-packer@main
        with:
          version: "1.11.2"
      - uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::123456789012:role/packer-build
          aws-region: eu-central-1
      - run: packer init images/
      - run: >
          packer build -only="${{ matrix.source }}"
          -var "git_sha=${{ github.sha }}" images/
      - uses: actions/upload-artifact@v4
        with:
          name: manifest-${{ matrix.source }}
          path: packer-manifest.json

Notes:

  • permissions: id-token: write lets the job request the OIDC token. The IAM role's trust policy should be limited to your repository and branch.
  • The account id and role name above are placeholders.
  • matrix builds one source per job in parallel; -only selects it. fail-fast: false lets other images finish when one fails.
  • A schedule trigger rebuilds weekly to pick up OS patches (rebuild schedules).
  • Upload the manifest as an artifact so downstream jobs and humans can find the image ids.

Practical concerns

  • Concurrency. Two simultaneous builds of the same image name collide. Add concurrency: group: packer-${{ github.ref }}.
  • Timeouts. Set timeout-minutes so a hung SSH wait does not burn runner minutes.
  • Plugin caching. packer init downloads plugins each run; cache ~/.config/packer/plugins keyed on the lock file hash if it matters.
  • Secrets from forks. Pull requests from forks do not get secrets, so keep PR jobs credential-free (validate only).
  • Environments and approvals. Use a GitHub environment with required reviewers for the job that publishes to production.
  • Self-hosted runners are needed for on-premises hypervisor builds. Keep them ephemeral and isolated.
  • Cleanup on cancel. If a job is cancelled, the builder instance may remain; tag builder resources (run_tags) and run a periodic janitor job that deletes old ones.
  • Logs. Avoid PACKER_LOG=1 on public repositories; it can expose details.

Try it: decide which jobs run

JS Event router (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Try it: expand a matrix

JS Matrix expansion (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Why prefer OIDC to stored access keys in CI?

    The job exchanges a short-lived token for temporary credentials.
  2. 2. What should a pull request from a fork run?

    Forks do not receive secrets and must not be able to use your cloud account.
  3. 3. Which flag selects one source from a template with several?

    packer build -only=amazon-ebs.web builds just that source.
  4. 4. Why add a scheduled trigger?

    Images age; periodic rebuilds keep them patched.

Summary

  • PRs run fmt, init, validate; merges and schedules build, test and publish.
  • Use OIDC for cloud credentials, a matrix with -only for parallel images and concurrency to avoid collisions.
  • Upload the manifest as an artifact and gate production publishing with environments.
  • Set timeouts, cache plugins and run a janitor for leaked builder resources.

Next: Image versioning, registries and promotion

अपनी प्रगति ट्रैक करें

मुफ़्त अकाउंट बनाएँ, लेसन पूरे के रूप में चिह्नित करें और किसी भी डिवाइस पर वहीं से शुरू करें जहाँ छोड़ा था।