Skip to content
Hands-on IT IT Drills

Day 10: Environments, OIDC and Updates

Goal: Create an environment produktion with approval and a secret of its own, fetch an OIDC token from the run and read its claims, and let Dependabot keep the dependencies up to date. · Duration: approx. 75 min · Prerequisite: Day 9

Briefing

  • An environment is a target such as produktion. It can require an approval, keep secrets for itself only and define which branches may deploy to it. A job selects it with environment:.
  • OIDC: With id-token: write, a job fetches a signed JWT from GitHub. It states who the job is: repository, branch, environment. Clouds and vaults exchange it for short-lived credentials – without a stored secret.
  • Dependabot looks for new versions of Go modules, actions and base images and opens pull requests for them. Configured in .github/dependabot.yml.
 deploy (environment: produktion)
   ─► waits ─► approval ─► secret DEPLOY_TOKEN
                        └► OIDC token: sub = repo:…:environment:produktion

Drill 1: An environment with approval

The approval should come from you. For that, the API needs your numeric ID:

cd ~/gha-lab/notes-actions
gh api user --jq .id

Expected (your number differs):

251214518

The rules as JSON – replace the number with yours:

cat > ~/gha-lab/env-produktion.json <<'EOF'
{
  "reviewers": [ { "type": "User", "id": 251214518 } ],
  "prevent_self_review": false,
  "deployment_branch_policy": {
    "protected_branches": true,
    "custom_branch_policies": false
  }
}
EOF
gh api -X PUT repos/{owner}/{repo}/environments/produktion \
  --input ~/gha-lab/env-produktion.json --jq '.name, [.protection_rules[].type]'

Expected:

produktion
["required_reviewers","branch_policy"]
  • reviewers: who may approve – people or teams.
  • prevent_self_review: false: whoever triggered the run may approve it themselves. Alone in the repository there is no other way; in a team you set it to true.
  • protected_branches: true: only protected branches may deploy to this environment – for you that is main, via the ruleset from Day 4.

A secret just for this environment:

gh secret set DEPLOY_TOKEN --env produktion --body lab-deploy-token-123
gh secret list --env produktion

Expected:

NAME          UPDATED
DEPLOY_TOKEN  less than a minute ago

gh never shows the value – even you can only overwrite a secret, not read it.

Save your progress? Sign up for free

Drill 2: A deployment that waits

cat > .github/workflows/deploy.yml <<'EOF'
name: deploy
on: workflow_dispatch

permissions:
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-24.04
    environment: produktion
    permissions:
      contents: read
      id-token: write
    steps:
      - name: Secret nutzen
        env:
          DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
        run: |
          echo "Token: $DEPLOY_TOKEN"
          echo "Länge: ${#DEPLOY_TOKEN}"
      - name: OIDC-Token
        run: >-
          curl -fsS -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN"
          "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=lab"
          | jq -r .value
          | jq -R 'split(".")[1] | @base64d | fromjson
          | {sub, aud, ref, environment, job_workflow_ref}'
EOF
git add .github
git commit -qm "deploy: Environment produktion"
git push -q
gh workflow run deploy.yml
  • environment: produktion binds the job to the environment: it waits for the approval and afterwards sees its secrets.
  • With id-token: write, the runner sets two variables: ACTIONS_ID_TOKEN_REQUEST_URL and ACTIONS_ID_TOKEN_REQUEST_TOKEN. With them, curl requests a token for the audience (audience) lab.
  • The second jq splits the JWT and decodes only the middle part, the claims. The token itself does not end up in the log.

With the ID from gh run list --workflow deploy.yml --limit 1:

gh run view <ID>

Expected (shortened):

* main deploy · 41626025600
Triggered via workflow_dispatch less than a minute ago

JOBS
* deploy (ID 125883462374)

* – the run is not finished. gh run list --workflow deploy.yml shows the status waiting: the job is waiting for approval. You can approve in the browser – or via the API. First the ID of the environment:

gh api repos/{owner}/{repo}/actions/runs/<ID>/pending_deployments \
  --jq '.[].environment | .name, .id'

Expected (the ID differs):

produktion
26123220715
gh api -X POST repos/{owner}/{repo}/actions/runs/<ID>/pending_deployments \
  -F 'environment_ids[]=26123220715' -f state=approved -f comment='Lab-Freigabe' \
  --jq '.[].environment'

Expected:

produktion
  • -F passes the number as a number, -f as text. environment_ids[] is a list – here with one entry.
  • state=rejected would cancel the run.

As soon as the run is finished:

gh run view <ID> --log | grep -E 'Z (Token|Länge):'

Expected (timestamps shortened):

deploy  Secret nutzen   ... Token: ***
deploy  Secret nutzen   ... Länge: 20

GitHub replaces every secret in the log with ***. The length shows: the value was there.

Save your progress? Sign up for free

Drill 3: What the OIDC token contains

gh run view <ID> --log | grep -A6 'Z {$' | cut -f3 | cut -c30-

Expected (the numbers differ):

{
  "sub": "repo:<du>@251214518/notes-actions@1444038371:environment:produktion",
  "aud": "lab",
  "ref": "refs/heads/main",
  "environment": "produktion",
  "job_workflow_ref": "<du>/notes-actions/.github/workflows/deploy.yml@refs/heads/main"
}
  • cut -f3 takes the third column of the log (the output), cut -c30- cuts off the timestamp.
  • sub is the most important claim. A cloud does not trust “GitHub”, but exactly this value: “only jobs from <du>/notes-actions in the produktion environment”. A pull request from a fork gets a different sub and therefore no access.
  • After owner and repository come their immutable IDs (@251214518, @1444038371). If someone renames the repository or creates a new one with the same name, the sub no longer matches – a protection against hijacked names.
  • aud defines whom the token is intended for. A token for lab is useless with a different service.

In AWS, the condition of a role would look something like this:

"token.actions.githubusercontent.com:sub":
    "repo:<du>@251214518/notes-actions@1444038371:environment:produktion"
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"

No access key in the repository, no secret that expires or must be rotated.

Save your progress? Sign up for free

Drill 4: Dependabot

mkdir -p .github
cat > .github/dependabot.yml <<'EOF'
version: 2
updates:
  - package-ecosystem: gomod
    directory: /
    schedule:
      interval: weekly
  - package-ecosystem: github-actions
    directory: /
    schedule:
      interval: weekly
    groups:
      actions:
        patterns: ['*']
  - package-ecosystem: docker
    directory: /
    schedule:
      interval: weekly
EOF
git add .github/dependabot.yml
git commit -qm "dependabot: Go, Actions, Docker"
git push -q
  • One entry per ecosystem. github-actions finds all uses: – including those pinned by SHA with the comment # v7.0.1 from Day 9.
  • groups: combines all action updates into one pull request.
  • docker reads the FROM lines of the Dockerfile.

Dependabot runs for the first time shortly after the push. After a few minutes:

gh pr list --author app/dependabot

Expected (depending on the state of the dependencies):

Showing 1 of 1 pull request in <du>/notes-actions that matches your search

ID   TITLE                          BRANCH                   CREATED AT
#16  build(deps): bump github.c...  dependabot/go_module...  about 4 minutes ago

In full: build(deps): bump github.com/jackc/pgx/v5 from 5.11.0 to 5.12.0. If nothing shows up, everything is up to date. Whether Dependabot has run is shown by its runs:

gh run list --workflow 'Dependabot Updates' --limit 3 --json displayTitle \
  --jq '.[].displayTitle'

Expected (the numbers differ):

docker in /. - Update #1677437037
github_actions in /. - Update #1677437022
go_modules in /. - Update #1677437015

Dependabot Updates is a workflow that GitHub manages itself – it shows up in gh workflow list --all, but does not live in your repository. One run per ecosystem. For actions and Docker there was nothing new – you pinned the actions to the latest state on Day 9.

Dependabot pull requests go through ci like any other – the ruleset requires green checks before you merge them.

Save your progress? Sign up for free

Checkpoint

gh api repos/{owner}/{repo}/environments --jq '.environments[].name'

Expected:

produktion
gh run list --workflow deploy.yml --limit 1 --json conclusion --jq '.[0].conclusion'

Expected (after the challenge it shows failure):

success
gh api repos/{owner}/{repo}/contents/.github/dependabot.yml --jq .name

Expected:

dependabot.yml

Challenge

Reject a deployment: start deploy.yml once more and reject the approval with a comment. What result does the run have?

Hint: As in Drill 2, with state=rejected.

Solution

gh workflow run deploy.yml

With the new ID from gh run list --workflow deploy.yml --limit 1:

gh api -X POST repos/{owner}/{repo}/actions/runs/<ID>/pending_deployments \
  -F 'environment_ids[]=26123220715' -f state=rejected -f comment='Nicht heute' \
  --jq '.[].environment'
gh run view <ID> --json conclusion --jq .conclusion

Expected:

produktion
failure

The run ends as failure, the deploy job never started. In the browser, the comment “Nicht heute” is attached to the rejection.

Save your progress? Sign up for free

Enterprise Note

  • OIDC instead of secrets: AWS, Azure, Google Cloud, HashiCorp Vault and OpenBao accept GitHub OIDC tokens. The trust rule checks at least sub and aud – a rule on the owner only (repo:<org>/*) lets every repository of the organization in.
  • Customizing sub: Organizations can define which claims appear in sub (e.g. additionally job_workflow_ref). That way access can be bound to a specific reusable workflow (Day 12).
  • Environments as a gate: Approvals, wait timers (wait_timer) and branch rules are the place where a human consents before production – with an entry in the audit log.
  • Renovate instead of Dependabot: Renovate can do more – custom data sources, rules across many repositories, automerge after a test run. The sister book “Renovate hands-on” goes into depth.