GitHub Actions Secrets and Environments, Hands-On
Your pipeline will soon need a password, such as a token to push an image. Type it into the workflow file and everyone who can read the repository has it, for good. This lesson shows how GitHub Actions secrets keep such values out of your code, how environments decide when a job may use them, and where that protection stops.
Keep the value out of the file
A secret is a value a workflow must use but nobody should read: a password, an API key or a token, the generated password for machines from lesson 7. Lesson 2 said they belong in a protected store, never in the code; GitHub Actions has one built in.
The diagram compares two ways to hand a deploy script a token. Typed into the YAML, it reaches every clone and stays in Git history. Read from a secret, only its name is committed, and GitHub fills in the value when the job runs.
Where secrets live, and how a job reads them
GitHub keeps secrets at three levels. Repository secrets reach every job in one repository. Environment secrets belong to an environment, a named place you deploy to, such as staging, and reach only jobs that name it. Organisation secrets belong to an organisation, a GitHub account shared by a team or company, and reach the repositories its admins choose.
A step reads a secret with the expression ${{ secrets.NAME }}, usually through env:, so the shell sees an ordinary environment variable. Names use only letters, digits and underscores and must not start with GITHUB_. If two levels hold the same name, the innermost wins, as the diagram shows.
GitHub stores each value encrypted, and the runner replaces the value with *** wherever it appears in the log, which is called masking.
Lab step 1: two secrets and a masked log
The folder course-labs/ci-cd-and-devops-fundamentals/03-secrets-environments holds secrets.yml, a workflow with two jobs. Create the environment first: on github.com, open your fork's Settings, Environments, New environment, type staging and click Configure environment.
Next, store two dummy secrets with the GitHub CLI. --env puts the second one in staging. Your clone has two remotes, your fork and datsabk/course-labs, so --repo names the fork; replace your-name with your GitHub user name:
gh secret set DEMO_REPO_SECRET --body 'open-sesame' --repo your-name/course-labs
gh secret set DEMO_TOKEN --env staging --body 'hunter2' --repo your-name/course-labs
Each prints a line such as ✓ Set Actions secret DEMO_TOKEN for your-name/course-labs. If the second fails with failed to fetch public key and HTTP 404, the environment does not exist yet or the user name is wrong. --body leaves the value in your shell history, which is fine for a dummy; for a real secret, leave it out and gh asks for the value.
Then copy the workflow in and push it:
mkdir -p .github/workflows
cp ci-cd-and-devops-fundamentals/03-secrets-environments/secrets.yml .github/workflows/
git add .github/workflows/secrets.yml
git commit -m "add secrets workflow"
git push
The first job, repo-secret, echoes the repository secret. The second, staging, names the environment with environment: staging. That line is what unlocks the staging safe; without it, secrets.DEMO_TOKEN is an empty string:
staging:
needs: repo-secret
runs-on: ubuntu-latest
environment: staging
steps:
- name: Use the staging secret
env:
DEMO_TOKEN: ${{ secrets.DEMO_TOKEN }}
run: |
echo "The staging token is $DEMO_TOKEN"
echo "It has ${#DEMO_TOKEN} characters"
${#DEMO_TOKEN} is the shell's way to print a variable's length. In the Actions tab, open Secrets and environments, the run add secrets workflow, the staging job and its step Use the staging secret. You should see (trimmed):
Run echo "The staging token is $DEMO_TOKEN"
The staging token is ***
It has 7 characters
Click the Run line to unfold it: its env: part shows DEMO_TOKEN: *** too. The value reached the job, all seven characters of hunter2, yet the log never shows it, and repo-secret prints The repository secret is ***. If a line ends at is and the count is 0, the job could not see the secret: gh secret list --env staging --repo your-name/course-labs should list it.
Lab step 2: give GITHUB_TOKEN the least access
Every job also gets GITHUB_TOKEN, a token GitHub creates when the job starts and that expires when it finishes. Steps use it to call GitHub, for example to push an image. Without a permissions: key, it gets the repository's default, set under Settings, Actions, General, Workflow permissions.
That default is either read and write for most scopes (permissive) or read-only contents and packages (restricted). New personal repositories start restricted, but nothing in the file tells you which one applies. Least privilege, from lesson 2, says to grant only what the job needs, so secrets.yml writes it down:
permissions:
contents: read
At the top of a workflow, the block covers every job; under one job, only that job. Once you name any scope, every scope you leave out becomes none, and a job that must write asks for just that scope, such as packages: write. To check it, expand Set up job, then GITHUB_TOKEN Permissions. You should see Contents: read and Metadata: read, which every token keeps, and nothing with write.
Lab step 3: make staging wait for a reviewer
An environment can also have protection rules, checks that must pass before any job that names it starts. On a public repository, such as your fork, all three are free:
- Required reviewers: up to six people or teams; one approval lets the job go on.
- Wait timer: a delay of 1 to 43,200 minutes (30 days) before the job may start.
- Deployment branches and tags: no restriction, protected branches only, or the names you list.
The flowchart shows why this protects secrets: a job is sent to a runner, and with it the environment's secrets, only after every rule passes.
Open Settings, Environments, staging, tick Required reviewers, add your own user name and click Save protection rules. Then start a new run without changing a file:
git commit --allow-empty -m "ask for approval"
git push
You should see repo-secret pass at once while staging waits. Click Review deployments, tick staging and click Approve and deploy: the job runs and prints *** as before. Reject would fail it without the secret ever leaving GitHub. You may approve your own run unless the environment is set to prevent self-review.
If the job runs without waiting, the rule was not saved, or environment: names another environment. That typo is quiet: naming an environment that does not exist creates it, empty and unprotected.
Limits to know
Pull requests from forks get no secrets. When someone opens a pull request from their fork into your repository, its run receives no secrets except GITHUB_TOKEN, and that token is read-only. Otherwise anyone could open a pull request that prints your secrets. Pushes to your own fork, as in this lab, are not affected.
Masking is a safety net, not a vault. It hides the exact value, but a transformed copy, such as the secret in base64, prints in clear. Anyone with write access can also push a workflow that sends a secret elsewhere. So never print a real secret, give write access only to people you trust, and hide values you derive with ::add-mask::, a log command that masks one more value.
Check yourself
A teammate removes a hard-coded token from the workflow in a new commit. Is the token safe now?
No. Git history and every earlier clone still hold it, so treat it as leaked: revoke it, create a new one and store that as a secret.
repo-secret prints ***, but staging prints The staging token is and nothing after it. What is the likely cause?
The staging job cannot see an environment secret: its environment: line is missing or names another environment, or DEMO_TOKEN was never stored in staging. Repository secrets reach every job; environment secrets reach only jobs that name their environment.
A pull request from a stranger's fork adds a step that prints secrets.DEMO_TOKEN in base64. Why is your token still safe?
Runs triggered from a fork receive no secrets except a read-only GITHUB_TOKEN, so there is nothing to print. Masking cannot hide a base64 copy; the fork rule is what protects you.