Set least-privilege token permissions
Limit the default GITHUB_TOKEN scope.
permissions:
contents: readRestrict permissions at the workflow or job level unless write access is truly needed.
Secure GitHub Actions workflows with permissions, environments, secrets, masking, fork safety, and OpenID Connect for cloud auth.
Use least privilege and avoid unsafe secret handling.
Limit the default GITHUB_TOKEN scope.
permissions:
contents: readRestrict permissions at the workflow or job level unless write access is truly needed.
Elevate token permissions on a specific job.
jobs:
release:
permissions:
contents: writeUse per-job permissions so publish jobs can write while test jobs remain read-only.
- name: Login
env:
API_TOKEN: ${{ secrets.API_TOKEN }}
run: ./scripts/login.shSecrets are automatically masked in logs, but still avoid printing them.
Hide a generated or fetched secret from logs.
echo "::add-mask::$TOKEN"Mask runtime-generated credentials or sensitive values before any command could log them.
Require approval and environment-scoped secrets.
jobs:
deploy:
environment: productionEnvironments help gate deploys and scope secrets to staging, production, or other targets.
Use short-lived cloud auth and avoid dangerous pull_request setups.
Grant `id-token: write` when using cloud federation.
permissions:
id-token: write
contents: readOIDC lets workflows exchange a short-lived identity token for cloud credentials without storing static secrets.
Use the AWS credentials action without long-lived keys.
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-actions-deploy
aws-region: us-east-1Pair this with an IAM role trust policy that trusts GitHub's OIDC provider and claims.
- uses: google-github-actions/auth@v2
with:
workload_identity_provider: projects/123456789/locations/global/workloadIdentityPools/pool/providers/provider
service_account: deployer@example-project.iam.gserviceaccount.comThis avoids storing long-lived JSON keys in repository secrets.
Avoid unsafe patterns for public repo pull requests.
on:
pull_request:Be careful with workflows that run untrusted code from forks. Avoid combining privileged tokens or secrets with unreviewed pull request code paths.