Securing Deploy Credentials with GitHub OIDC

Most static sites deploy with a token stored as a CI secret: an AWS access key, a Cloudflare API token, a Netlify personal access token. It sits in the repository settings for years, it is available to every job that references it, and anyone who obtains it — through a leaked log, a compromised dependency, a malicious workflow change — can publish anything to the site until someone notices and rotates it. For a static site, where the deploy token is effectively the key to every page, that is the single most valuable secret the organisation holds for it.

OpenID Connect (OIDC) removes the stored secret. Each workflow run requests a signed identity token from GitHub that says which repository, branch, environment and workflow it is; the cloud provider checks that identity against a trust policy and returns credentials valid for minutes. Nothing long-lived exists to leak. This guide sets that up for AWS, and for hosts that only accept API tokens, shows how to get most of the same protection. It is part of Security Headers and Hardening for Static Sites.

Prerequisites

  • A static site deployed from GitHub Actions.
  • Admin access to the repository (for environments and branch protection) and to the hosting account.
  • A deploy that can run as its own job, separate from install and build.

Step 1: Split Build and Deploy

Before touching credentials, separate the job that runs third-party code from the job that holds the credential. The build job installs dependencies and builds; it uploads the output as an artifact and has no deploy permissions. The deploy job downloads the artifact and publishes it; it installs nothing.

permissions: { contents: read }

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci --ignore-scripts && npm rebuild sharp && npm run build
      - uses: actions/upload-artifact@v4
        with: { name: site, path: dist/ }

  deploy:
    needs: build
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    environment: production          # protected environment, required reviewers optional
    permissions: { id-token: write, contents: read }
    steps:
      - uses: actions/download-artifact@v4
        with: { name: site, path: dist/ }
      # credential steps below

id-token: write is granted only to the deploy job, so only it can request an OIDC token. A compromised package in the build job cannot mint credentials because its job has no permission to ask.

OIDC credential exchange during a deploy The deploy job asks GitHub for an identity token stating repository, branch main and environment production. It presents that token to the cloud provider's security token service. The provider checks the token against a trust policy that only accepts this repository's production environment, and returns credentials valid for 15 minutes. The job uploads the site, and the credentials expire shortly afterwards. Identity in, short-lived credential out deploy job id-token: write GitHub OIDC signs repo, ref, env provider STS check trust policy credential expires in 15 min JWT trust policy accepts only: repo:acme/docs:environment:production forks, other branches, other repos: rejected
The only thing stored anywhere is the trust policy, which grants nothing on its own.

Step 2: AWS (S3 and CloudFront)

Create an IAM OIDC identity provider for token.actions.githubusercontent.com once per account, then a role whose trust policy accepts only your repository's production environment:

{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": { "Federated": "arn:aws:iam::123456789012:oidc-provider/token.actions.githubusercontent.com" },
    "Action": "sts:AssumeRoleWithWebIdentity",
    "Condition": {
      "StringEquals": {
        "token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
        "token.actions.githubusercontent.com:sub": "repo:acme/docs:environment:production"
      }
    }
  }]
}

Attach a permissions policy that allows only s3:PutObject, s3:DeleteObject and s3:ListBucket on the site bucket and cloudfront:CreateInvalidation on its distribution. Then, in the deploy job:

- uses: aws-actions/configure-aws-credentials@v4
  with:
    role-to-assume: arn:aws:iam::123456789012:role/docs-deploy
    aws-region: eu-west-1
    role-duration-seconds: 900
- run: aws s3 sync dist/ s3://docs-example-com/ --delete
- run: aws cloudfront create-invalidation --distribution-id E2ABC123 --paths '/*'

The full S3 deploy is described in Deploying a Static Site to S3 and CloudFront.

Step 3: Hosts Without Native OIDC

Cloudflare Pages and Workers, Netlify and Vercel deploy with API tokens rather than federated identity. You can still get most of the benefit:

  • Scope the token narrowly. A Cloudflare API token can be limited to "Cloudflare Pages: Edit" (or "Workers Scripts: Edit") on one account and nothing else. It cannot touch DNS, other zones or billing.
  • Store it in a protected environment, not a repository secret. GitHub environment secrets are only available to jobs that declare environment: production, and the environment can require the main branch and optional reviewer approval. A workflow edited in a pull request cannot read it.
  • Keep it out of the build job. Only the deploy job references the environment.
  • Rotate on a schedule. Set an expiry on the token (Cloudflare supports this at creation) and rotate quarterly.
  • Broker for the strictest setups. A small Worker can accept a GitHub OIDC token, verify its signature and claims against GitHub's published keys, and use a securely stored master token to create a short-lived, single-purpose token for this run. Most teams find the environment-scoped token sufficient.
deploy:
  environment: production
  steps:
    - uses: actions/download-artifact@v4
      with: { name: site, path: dist/ }
    - run: npx wrangler@3 deploy
      env:
        CLOUDFLARE_API_TOKEN: ${{ secrets.CF_PAGES_DEPLOY_TOKEN }}   # environment secret
        CLOUDFLARE_ACCOUNT_ID: ${{ vars.CF_ACCOUNT_ID }}
Credential options by host A ladder of three options from strongest to weakest. Native OIDC federation, available on AWS, Google Cloud and Azure, stores nothing. An OIDC broker issuing per-run host tokens stores one master token outside CI. An environment-scoped, narrowly permissioned API token stored as a protected environment secret is the practical minimum for Cloudflare, Netlify and Vercel. Use the strongest option your host supports native OIDC federation AWS · GCP · Azure — nothing stored in CI OIDC broker → per-run token any API-token host — master token lives outside CI scoped token in protected environment Cloudflare · Netlify · Vercel — practical minimum
Even the bottom rung is a large improvement on a broad, repository-wide token readable by every job.

Measured Impact

The documentation site's credential exposure before and after the change:

MeasureBeforeAfter
Long-lived deploy credentials stored in GitHub2 (AWS key, Cloudflare token)0 for AWS; 1 environment-scoped Cloudflare token
Jobs with access to deploy credentials3 (build, test, deploy)1 (deploy)
Steps that run third-party code with credentials present~900 package scripts + build0
Credential lifetime (AWS)indefinite (key age 26 months)15 minutes
Fork pull requests able to request credentialsnot testedrejected by trust policy
Deploy time change+4 s (artifact upload/download)
Credential exposure window Two timelines. Before, an AWS access key existed continuously for 26 months and was present in every job of every run. After, a credential exists for 15 minutes during each deploy job only, a few minutes a day in total. How long a stolen credential would have worked Stored key valid continuously · 26 months and counting OIDC 15-minute windows, deploy job only A leaked OIDC credential is useless minutes later; a leaked stored key works until someone notices
Rotation policies try to shrink the red bar; OIDC replaces it with the green slivers.

Testing the Trust Policy

A trust policy is security-critical configuration and should be tested like code. Two negative tests are enough to catch the common mistakes. Open a pull request from a fork that adds a step calling configure-aws-credentials with the production role: it must fail with an access-denied error from STS. Then push a branch other than main in the main repository with the same step: that must fail too, because its subject claim names the branch rather than the production environment. Record both results in the pull request that introduced the policy, and repeat them whenever the policy changes. A policy that has only ever been tested by succeeding has not really been tested.

Auditing Who Deployed What

Short-lived credentials also improve the audit trail. Each AWS session created through OIDC records the role session name, which configure-aws-credentials sets to include the workflow run ID; CloudTrail entries for every S3 write therefore link back to a specific GitHub Actions run, commit and actor. With a stored key, every deploy for two years had appeared in CloudTrail as the same anonymous IAM user. Add the run URL to the deploy's output — or to a deploy.json file written into the site itself — and any page in production can be traced to the exact workflow run that published it, which is the first question in any investigation described in Rolling Back a Bad Static Deploy in Under a Minute.

Pitfalls & Rollback

  • Trusting the whole repository. A sub condition of repo:acme/docs:* lets any branch or pull request workflow assume the role. Pin it to the environment or branch.
  • Granting id-token: write at workflow level. Give it only to the deploy job, or the build job can request tokens too.
  • Broad permissions on the role. The role should be able to write one bucket and invalidate one distribution, nothing else.
  • Forgetting preview deploys. Preview jobs need their own role or token with access only to preview targets, never production.
  • Rollback: keep the old secret disabled but present for one release cycle; re-enabling it restores the previous deploy path if the OIDC setup fails. Delete it once the new path has deployed cleanly for a week.

Conclusion

The deploy credential is the most powerful secret a static site has, and the easiest to eliminate. Splitting build from deploy removes third-party code from the credential's reach; OIDC removes the stored credential altogether where the host supports it; environment-scoped, narrowly permissioned tokens cover the hosts that do not. On this site that took two stored keys down to one tightly scoped token, cut AWS credential lifetime from 26 months to 15 minutes, and cost four seconds per deploy.

FAQ

What is OIDC in GitHub Actions?

GitHub can issue a signed identity token to a workflow run that states which repository, branch, environment and workflow it is. A cloud provider configured to trust GitHub exchanges that token for short-lived credentials, so no long-lived secret is stored anywhere.

Which hosts support OIDC deploys?

AWS, Google Cloud and Azure support it natively. For hosts that only accept API tokens, such as Cloudflare or Netlify, you can keep a narrowly scoped token in a protected environment or exchange the OIDC token through a small broker that issues short-lived host tokens.

How long do OIDC credentials last?

As long as you configure, typically 15 minutes to one hour. A deploy of a static site takes a minute or two, so the shortest duration the provider allows is usually enough.

What stops a pull request from a fork using the credential?

The trust policy. It should accept only tokens whose subject claim names your repository and the protected branch or environment, which fork pull requests cannot produce.