โExplain the difference between a GitHub Actions repository secret, a variable, and the built-in GITHUB_TOKEN, and when to use each.โ
05 โ Secrets & environment variables in Actions
Category: Security ยท Priority: High ยท Step: 6
Never hardcode tokens. Store them as encrypted repository secrets and read them with
${{ secrets.NAME }}.
๐ฏ What is this task?
To push images or deploy, your workflow needs credentials โ but credentials must never live in the YAML file (it's public in your repo). GitHub gives you encrypted secrets: you store them once in the repo settings, and workflows read them at runtime without ever exposing the value in logs.
๐ค AI Prompt
"Explain the difference between a GitHub Actions repository secret, an environment variable, and the built-in
GITHUB_TOKEN. When should I use each?"
๐ Step-by-step
Step 1 โ Add a secret
Repo โ Settings โ Secrets and variables โ Actions โ New repository secret. Name it e.g. DOCKERHUB_TOKEN.
Step 2 โ Use it in a workflow
steps:
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
Three things that hold values:
| Source | Use for | Example |
|---|---|---|
secrets.X | Passwords, tokens (encrypted) | ${{ secrets.DOCKERHUB_TOKEN }} |
env: / vars.X | Non-secret config | ${{ vars.IMAGE_NAME }} |
GITHUB_TOKEN | Auto-generated, scoped to the repo | push to GHCR, comment on PRs |
GitHub automatically masks secret values in logs โ but never echo a secret on purpose.
โ Done when
- You added at least one repository secret
- A workflow reads it via
${{ secrets.NAME }} - You can explain why secrets aren't written in the YAML
๐ Quiz
Answer all 2 questions correctly to complete this lesson.