Project 2 โ€บ 05 Secrets & environment variablesGitHub โ†—
๐Ÿค– AI Prompt โ€” stuck? paste this

โ€œ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:

SourceUse forExample
secrets.XPasswords, tokens (encrypted)${{ secrets.DOCKERHUB_TOKEN }}
env: / vars.XNon-secret config${{ vars.IMAGE_NAME }}
GITHUB_TOKENAuto-generated, scoped to the repopush 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.

1. How do you reference a repository secret in a workflow?
2. Why should tokens never be written directly in the YAML?
โ† PreviousPass the quiz to completeNext โ†’