AI Hints
Home›CI/CD with GitHub Actions Tutorial›Environments and Approvals
CI/CD with GitHub Actions · Lesson 87 of 138

Environment Secrets

Different keys per stage.

⏱ 2 min read▶ Video at 1:40:48🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Staging and production should never share passwords. If staging leaks, production must stay safe. Environment secrets let each environment have its own value for the same secret name.

Environment Secrets
From the video: Environment Secrets
  • Never share keys
  • Staging leaks, prod is safe
  • Same name, different value

Same name, two values

The job uses the staging environment, and reads secrets dot deploy token. If staging and production both define deploy token, the job gets the one for its own environment. And the workflow code doesn't change at all.

.github/workflows/deploy.ymlyaml
name: Deploy
on: push
jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: staging
    steps:
      - name: Deploy
        env:
          TOKEN: ${{ secrets.DEPLOY_TOKEN }}
        run: |
          echo "Using token $TOKEN"
          echo "Token length: ${#TOKEN}"

Masking

Our helper reads secrets from variables, so we pass a fake staging token. The log shows three stars instead of the token, just like GitHub, which hides secret values in logs. Its length still prints, because only the exact value is hidden. So never print a secret, or transform it.

Terminal
SECRET_DEPLOY_TOKEN=stg-abc123 run-steps .github/workflows/deploy.yml | grep -A3 "✓"
  ✓ Deploy
      Using token ***
      Token length: 10
  Job succeeded

Summary

Environment secrets give each stage its own keys, under the same name. Logs mask secret values, but don't print them anyway. Next, strategies for deploying safely.

Key takeaways
  • Per-environment secrets
  • Same name
  • Masked in logs
Next up: Deployment Strategies — how to switch versions