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.
permissions:
contents: readRestrict permissions at the workflow or job level unless write access is truly needed.
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.
echo "::add-mask::$TOKEN"Mask runtime-generated credentials or sensitive values before any command could log them.
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.
permissions:
id-token: write
contents: readOIDC lets workflows exchange a short-lived identity token for cloud credentials without storing static secrets.
- 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.
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.