AI Hints
Home›CI/CD with GitHub Actions Tutorial›Secrets, Tokens and Outputs
CI/CD with GitHub Actions · Lesson 40 of 138

Secrets

Passwords stay hidden.

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

Deploying usually needs passwords, tokens, or keys. Those must never be written in a workflow file, because everyone who can read the repository would see them. GitHub stores them separately, as encrypted secrets.

Secrets
From the video: Secrets
  • Deploys need passwords
  • Never in the file
  • Encrypted secrets

Using a secret

You add a secret in the repository settings, under secrets and variables. In the workflow, secrets dot API key reads it. The safest way to use it is through an env value, so it reaches the command as an environment variable, not as text pasted into the script.

.github/workflows/deploy.ymlyaml
name: Deploy
on: push
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Use the API key
        env:
          API_KEY: ${{ secrets.API_KEY }}
        run: |
          echo "Key length: ${#API_KEY}"
          test -n "$API_KEY" && echo "Key is set"

Running with a secret

Our run steps helper reads secrets from variables starting with SECRET underscore, standing in for GitHub's secret store. The step sees the key, and prints only its length, never the value. On GitHub, if a secret's value ever appears in a log, it's automatically replaced with stars.

Terminal
SECRET_API_KEY=sk-test-12345 run-steps .github/workflows/deploy.yml
Workflow: Deploy  (event: push, branch: main)
Job: deploy
  ✓ Use the API key
      Key length: 13
      Key is set
  Job succeeded

Secret rules

Never print a secret, even partly. Give each secret the smallest permissions possible. Rotate secrets now and then, and straight away if one might have leaked. And remember that pull requests from forks don't get your secrets, which protects you from strangers' code.

keyvalue
Never printnot even part
Least privilegesmall permissions
Rotatechange them regularly
Forksget no secrets

Summary

Secrets are stored encrypted by GitHub, read with secrets dot name, and passed to commands through env. Next, the token GitHub gives every run.

Key takeaways
  • Settings → Secrets
  • ${{ secrets.NAME }}
  • Pass via env
Next up: GITHUB_TOKEN and Permissions — a key for every run