Skip to main content
This lesson demonstrates how GitHub Actions resolves secret precedence across organization, repository, and environment scopes. Environments (for example, production, staging, or DemoEnvironment) let you enforce deployment protection rules (required reviewers, wait timers) and also host scoped secrets and variables. A secret name can exist at all three levels; this guide shows which value GitHub Actions will expose to your workflow when the same secret name appears in multiple scopes.

Create and configure an environment

Create an environment in your repository settings and configure protection rules such as required reviewers and wait timers. For this demonstration we created an environment named DemoEnvironment, set a 15-minute wait timer, and required a reviewer (ourselves). We also enabled the option to allow administrators to bypass protection rules.
A screenshot of the GitHub repository settings page for "Environments / Configure DemoEnvironment" showing deployment protection rules. It displays options for required reviewers, a 15-minute wait timer, and a green "Save protection rules" button, with the left-hand settings navigation visible.

Add an environment-level secret

You can scope secrets specifically to an environment. For this demo we added an environment secret named DEMO_OVERRIDE_SECRET with the value EnvironmentValue. Environment secrets are only available to jobs that declare that exact environment.

Add an organization-level secret

At the organization level (Organization → Settings → Actions → Secrets and variables → Actions → New organization secret) you can create org-wide secrets. We defined an organization secret named DEMO_OVERRIDE_SECRET with the value OrganizationValue. Organization secrets may be restricted to specific repositories or made available org-wide.
A dark-themed screenshot of a GitHub organization settings page where an Actions secret is being added, showing a "Value" textbox containing "OrganizationValue" and a "Repository access" dropdown with "Private and internal repositories" selected. The left sidebar lists organization settings like Billing, Repository, Codespaces, Actions, and Webhooks.

Add a repository-level secret

At the repository level (Settings → Secrets and variables → Actions) we added a repository secret named DEMO_OVERRIDE_SECRET with the value RepositoryValue. When a repository has a secret with the same name as an organization secret, the GitHub UI indicates that the repository secret overrides the organization secret. That override is only relevant when the job is not associated with an environment that defines the same secret name.
A dark-themed screenshot of a GitHub repository Settings page showing the "Secrets and variables" → "Actions" section, listing environment, repository, and organization secrets (e.g., DEMO_OVERRIDE_SECRET) and related management buttons.

Secret precedence — key points

  • Environment secrets take highest precedence for jobs associated with that environment.
  • If a job does not declare an environment, repository-level secrets override organization-level secrets when names conflict.
  • Unique secrets defined only at one level remain available from that level.
  • Environment protection rules (approvals, wait timers) can prevent a job from running until requirements are met; admins can be allowed to bypass protection rules.
Precedence summary table
Reference secrets in workflow YAML using the secrets context. Example: env: TEST_SECRET: ${{ secrets.DEMO_OVERRIDE_SECRET }}. Put the expression inside a code block or inline code (as shown) so MDX does not interpret the curly braces.
Avoid printing secret values in logs. Although GitHub masks secrets in logs, echoing secrets or writing them to files can unintentionally expose sensitive data. Use them only where necessary and handle output carefully.

Demo workflow: test precedence

This single workflow demonstrates both scenarios:
  • TEST 1: Job associated with DemoEnvironment — environment-level secret should win.
  • TEST 2: Job with no environment — repository-level secret should win over organization-level secret.
Save this file as .github/workflows/secrets-priority.yml in your repository.

Run the workflow and observe the behavior

After pushing this workflow or manually dispatching it:
  • The repo-vs-org job will run immediately and should print that the repository-level secret won (because RepositoryValue overrides OrganizationValue for the same secret name when no environment is declared).
  • The env-vs-repo-vs-org job will be blocked by environment protection rules (approval and wait timer). The Actions UI will show the job waiting for deployment approval and the timer.
A dark-themed GitHub Actions workflow run page showing the "secrets-priority.yml" workflow with a pending deployment to "DemoEnvironment" and one job marked waiting. The left sidebar shows run details and jobs like "repo-vs-org" and "env-vs-repo-vs-org."

Example run output (repo vs org)

When the job that does not use an environment runs, expected output:
After approving the environment deployment (or bypassing the wait timer as an allowed administrator), the environment-associated job should run and report:

Summary

  • Environment secrets have highest priority when a job is associated with that environment and the same secret name exists at multiple levels.
  • If a job does not reference an environment, repository secrets override organization secrets when names conflict.
  • Environment protection rules (approvals, wait timers) can delay or block runs until requirements are satisfied; administrators may be allowed to bypass protections.
  • Use the secrets context (${{ secrets.YOUR_SECRET_NAME }}) in workflow YAML to access scoped secrets. Keep secret values out of logs and persistent artifacts.

Watch Video