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

लेसन 29 · CI/CD and Operations

Pulumi in CI/CD Pipelines: GitHub Actions, OIDC and Safe Deploys

Automate Pulumi deployments: preview on pull requests, up on merge, per-environment stacks, OIDC credentials instead of long-lived keys, caching dependencies and handling failures.

Running pulumi up from a laptop is fine for learning and dangerous for a team: it depends on one person's credentials, local files and memory. A pipeline makes deployments repeatable, reviewed and logged. The usual shape is simple: preview on every pull request, deploy when it merges, with a gate before production.

The pipelines below need GitHub, a cloud account and the Pulumi CLI, so they are read-only. The runnable stand-ins model the decision logic (which command runs for which event) and the OIDC trust check that replaces stored cloud keys.

The standard flow

flowchart LR
  PR["Pull request"] --> Prev["pulumi preview (dev stack)"]
  Prev --> Comment["Preview posted as PR comment"]
  Comment --> Review["Code review + checks"]
  Review --> Merge["Merge to main"]
  Merge --> UpDev["pulumi up: dev"]
  UpDev --> UpStg["pulumi up: staging"]
  UpStg --> Gate["Manual approval"]
  Gate --> UpProd["pulumi up: prod"]

Reviewers see exactly what a change would do before they approve it.

A GitHub Actions workflow

The official pulumi/actions action installs and runs the CLI for you:

YAML
name: infra
on:
  pull_request:
    paths: ["infra/**"]
  push:
    branches: [main]
    paths: ["infra/**"]

permissions:
  id-token: write       # OIDC to the cloud and Pulumi Cloud
  contents: read
  pull-requests: write  # let the action comment on the PR

concurrency:
  group: infra-${{ github.ref }}
  cancel-in-progress: false   # never cancel a running deploy

jobs:
  preview:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: 22, cache: npm, cache-dependency-path: infra/package-lock.json }
      - run: npm ci
        working-directory: infra
      - uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::123456789012:role/pulumi-preview
          aws-region: eu-central-1
      - uses: pulumi/actions@v6
        with:
          command: preview
          stack-name: acme/shop/dev
          work-dir: infra
          comment-on-pr: true
        env:
          PULUMI_ACCESS_TOKEN: ${{ secrets.PULUMI_ACCESS_TOKEN }}

  deploy:
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    environment: production        # approval rules live on this environment
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: 22, cache: npm, cache-dependency-path: infra/package-lock.json }
      - run: npm ci
        working-directory: infra
      - uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::123456789012:role/pulumi-deploy
          aws-region: eu-central-1
      - uses: pulumi/actions@v6
        with:
          command: up
          stack-name: acme/shop/prod
          work-dir: infra
        env:
          PULUMI_ACCESS_TOKEN: ${{ secrets.PULUMI_ACCESS_TOKEN }}

Pin actions to a version (or a commit SHA for stricter supply-chain control) and check the current major version in the action's repository before copying; this example is illustrative. Key ideas:

  • Separate roles for preview and deploy. The preview role can be read-only (plus whatever refresh needs). Only the main-branch workflow can assume the deploy role.
  • concurrency stops two deploys of the same stack from racing; do not cancel a running deployment half way.
  • environment: production in GitHub lets you require reviewers before the job runs.
  • paths filters avoid running infrastructure jobs for unrelated changes.

No long-lived cloud keys: OIDC

Storing AWS_ACCESS_KEY_ID in CI secrets is risky: keys leak, rotate rarely and work from anywhere. With OIDC, the CI provider issues a short-lived signed token that states who is running (repository, branch, environment). The cloud's identity provider trusts the issuer and a role's trust policy says which tokens may assume which role. The job gets credentials that expire in an hour.

An AWS trust policy for the deploy role says, in words: "the GitHub OIDC provider may assume this role if the audience is sts.amazonaws.com and the subject is repo:acme/infra:environment:production." Do the same on Azure (federated credentials) and Google Cloud (workload identity federation). Pulumi Cloud also supports OIDC for authentication, so even the access token can go away.

Node.js Stand-in: does this OIDC token match the trust policy?

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

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The second and third cases matter: a pull request must not be able to assume the production role, and a fork must not match at all. Writing the trust policy tightly (exact sub, not a wildcard) is the most valuable security setting in the pipeline.

Stand-in: which command for which event?

Pipelines encode a small decision table: event, branch and target determine whether to preview, deploy or do nothing, and whether approval is needed. Modeling it as data makes it easy to review.

Node.js Stand-in: pipeline decision table

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

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Practical advice

  • One stack per environment, with the same program and different config. Promote a change by merging once and deploying down the line.
  • Preview in the PR, but remember it can be stale. Another change may merge first; the deploy job re-runs the full update, so keep up interactive-free (--yes is implied by the action) and fail on policy violations.
  • Secrets: the secrets provider key (passphrase or KMS) must be available in CI. Prefer a KMS role over a passphrase variable.
  • Cache dependencies (npm, pip, Go modules) for speed, never the .pulumi folder.
  • Forked pull requests get no secrets in most CI systems. Run previews for forks only after a maintainer approves, or use a separate low-privilege path.
  • Failure handling: if an update fails halfway, resources already created stay. Fix the cause and run again, because up continues where state left off. If a lock is left from a cancelled job, the next run says so; use pulumi cancel after checking nobody is running an update.
  • Keep deploy logs. pulumi up --json or the Pulumi Cloud update history shows who deployed what and when.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What is the standard Pulumi pull request workflow?

    Preview shows reviewers the effect of a change; the merge triggers the real update.
  2. 2. What is the main benefit of OIDC over stored cloud access keys?

    No long-lived secret exists to leak; credentials expire and only matching repository, branch or environment claims can obtain them.
  3. 3. Why set concurrency with cancel-in-progress false for the deploy job?

    Cancelling an update halfway can leave locks and partial changes; queueing is safer.
  4. 4. A trust policy uses a wildcard subject like repo:acme/:. What is the risk?

    Broad subjects widen who can obtain credentials. Use exact subjects, ideally tied to a protected environment.

Summary

  • Preview on pull requests, deploy on merge, and add approval before production.
  • Use pulumi/actions (or the CLI) with one stack per environment and separate preview and deploy roles.
  • Replace stored cloud keys with OIDC and tight trust policies.
  • Serialize deploys with concurrency, cache dependencies and never cancel a running update.
  • Keep failure handling simple: fix, rerun up, and use pulumi cancel only when sure no update is running.

Next: Previews, Pull Requests and Review Practices

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

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