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.
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 themainbranch 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 }}
Measured Impact
The documentation site's credential exposure before and after the change:
| Measure | Before | After |
|---|---|---|
| Long-lived deploy credentials stored in GitHub | 2 (AWS key, Cloudflare token) | 0 for AWS; 1 environment-scoped Cloudflare token |
| Jobs with access to deploy credentials | 3 (build, test, deploy) | 1 (deploy) |
| Steps that run third-party code with credentials present | ~900 package scripts + build | 0 |
| Credential lifetime (AWS) | indefinite (key age 26 months) | 15 minutes |
| Fork pull requests able to request credentials | not tested | rejected by trust policy |
| Deploy time change | — | +4 s (artifact upload/download) |
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
subcondition ofrepo:acme/docs:*lets any branch or pull request workflow assume the role. Pin it to the environment or branch. - Granting
id-token: writeat 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.
Related
- Parent: Security Headers and Hardening for Static Sites — pipeline and edge hardening together.
- Auditing npm Dependencies in SSG Pipelines — why the build job must not hold credentials.
- Deploying to Multiple Environments from One Workflow — environment-per-target setups.
- Deploying a Static Site to S3 and CloudFront — the AWS side of this deploy.
- Automating Preview Deploy Pipelines with GitHub Actions — scoping preview credentials separately.