# Secrets and variables in CI pipelines, GitHub Actions guide

> CI secrets are encrypted values (tokens, keys, passwords) stored in the CI system and injected into jobs at run time, while variables hold non-sensitive configuration in plain text.

URL: https://levelrail.com/learn/ci-cd/secrets-and-variables-in-ci-pipelines/
Updated: 2026-11-27

Pipelines need credentials: a registry token, an SSH key, a cloud login. They also need plain configuration like a hostname or a region. GitHub Actions has separate places for each, and using the wrong one is how credentials end up in logs and forks. This lesson covers the places, the scopes, and the few traps that cause real leaks. The idea of a [secret](https://levelrail.com/learn/glossary/#secret)) on a server is in [Environment variables and secrets](https://levelrail.com/learn/deploy-operate/environment-variables-and-secrets/)).

## How it works

GitHub stores secrets encrypted. When a job runs, it decrypts the ones the job may use and exposes them through the `secrets` context. Any value registered as a secret is masked in the log, replaced by asterisks, if it appears verbatim. Variables work the same way but are stored in plain text and read through the `vars` context.

```yaml
steps:
  - run: ./deploy.sh
    env:
      API_TOKEN: ${{ secrets.API_TOKEN }}
      TARGET_REGION: ${{ vars.TARGET_REGION }}
```

Pass a secret to a step through `env` on that step only. Do not set it at workflow level, where every step and every action can read it.

## Scopes: repository, environment, organization

- **Repository secrets and variables** are available to workflows in that repository.
- **Environment secrets and variables** belong to a named environment such as `production`. They are only given to jobs that declare `environment: production`, and you can require reviewers or restrict branches for that environment. Prefer these for deploy credentials.
- **Organization secrets and variables** are shared across selected repositories, so you set them once. Limit which repositories can see them.

The narrowest scope that works is the right one. A production SSH key as an environment secret with a reviewer means a random workflow cannot read it.

## Masking and never echoing

Masking is a safety net, not a plan. It matches the exact value, so a transformed value (base64, split into parts, URL-encoded) is not masked. Do not print secrets, do not run `env` or `set -x` in steps that hold them, and do not write them to files that you upload as artifacts. Build tools that print their configuration on error can leak a value without anyone asking them to. If a value is multi-line, such as a private key, register each sensitive line or avoid logging around it.

## GITHUB_TOKEN and permissions

Every run gets a short-lived `GITHUB_TOKEN` scoped to the repository. Its rights come from the `permissions` key. Set the minimum:

```yaml
permissions:
  contents: read
```

Add `packages: write` for pushing images, or `pull-requests: write` for commenting on pull requests, only in the jobs that need them. You rarely need a personal access token for things the built-in token can do. A narrow token also limits the damage from a compromised third-party action.

## OIDC for cloud deploys, in general terms

A stored cloud key lasts until someone rotates it. OpenID Connect (OIDC) replaces it. The workflow asks GitHub for a signed identity token that states which repository, branch and environment it runs for. The cloud provider is configured to trust that identity, and it exchanges the token for temporary credentials. Nothing long-lived sits in your secrets. To use it, a job needs `id-token: write` permission, and the cloud side needs a trust rule that names your repository. Most major clouds support this. The exact setup is in each provider's documentation.

## Secrets and pull requests from forks

For a `pull_request` run from a fork, secrets are not passed and the token is read-only. This is deliberate: the code in that run is the contributor's, and it could print anything it can read. Do not look for a way around it. Write tests that need no secrets, and keep deploys on branches in your own repository.

## The pull_request_target trap

`pull_request_target` runs the workflow file from your base branch with access to secrets and a write token, even for a fork. It exists for tasks like labelling a pull request. The danger is combining it with `actions/checkout` of the pull request's head and then running that code, such as `npm install` or a test script. The attacker's code now runs with your secrets. Rules that hold up:

- Do not check out or run untrusted PR code in a `pull_request_target` job.
- If you must process PR content, treat it as data, never as code.
- Keep that workflow's permissions as narrow as possible.

## Check it worked

Open Settings, then Secrets and variables, then Actions. You should see each credential in the narrowest scope that serves it, with deploy credentials in an environment. Open a recent run log and search for a known secret name: you should find `***` masks and no raw values. Confirm each workflow has a top-level `permissions` block. Finally, open a test pull request from a fork account, if you have one, and check that it passes without secrets.

## Edge cases and gotchas

- **Secrets are not versioned.** You cannot read one back in the UI. Keep the source of truth in a password manager so you can rotate it.
- **Variables are not private.** Anyone who can read the repository settings can see them. Never put a token in `vars`.
- **Structured secrets.** A JSON blob as one secret is not masked per field. Prefer one secret per value.
- **Third-party actions get your env.** An action you pin to `@v4` can read what you give it. Pass only what it needs, and review actions you did not write.
- **Re-run keeps old values.** A re-run may use the secrets at the time of the re-run, but cached or already-uploaded artifacts keep what they had. Check artifacts for secrets.
- **Public repositories.** Logs and artifacts are readable by anyone. Be stricter there.

## Troubleshooting

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Secret is empty in a step | Fork PR, wrong environment or typo in the name | Check the trigger, the `environment:` key and the name |
| `Context access might be invalid: SECRET_NAME` | Secret not defined in this scope | Add it to repository or environment secrets |
| `Resource not accessible by integration` | Token permission missing | Add the key under `permissions` |
| Value shows in the log unmasked | Value was transformed before printing | Revoke it, stop printing, avoid transforms |
| Environment secret not available | Job lacks `environment:` | Add `environment: production` to the job |
| `Not authorized to perform sts:AssumeRoleWithWebIdentity` | OIDC trust rule does not match the repo or branch | Fix the trust condition on the cloud side |
| Deploy runs from a fork PR | Wrong trigger | Use `workflow_dispatch` or push on your own branch |

## Quick reference

```yaml
permissions:
  contents: read
jobs:
  deploy:
    environment: production
    steps:
      - run: ./deploy.sh
        env:
          API_TOKEN: ${{ secrets.API_TOKEN }}
          REGION: ${{ vars.REGION }}
```

```text
leak response: revoke -> replace -> update the secret -> check where it was used
```

## Related lessons

- [Environment variables and secrets](https://levelrail.com/learn/deploy-operate/environment-variables-and-secrets/)): how secrets reach a running app.
- [Gitignore and keeping secrets out of Git](https://levelrail.com/learn/git/gitignore-and-keeping-secrets-out-of-git/)): the first line of defence.
- [Deploy over SSH from GitHub Actions](https://levelrail.com/learn/ci-cd/deploy-over-ssh-from-github-actions/)): a deploy key stored as an environment secret.
- [Run tests on every push and pull request](https://levelrail.com/learn/ci-cd/run-tests-on-every-push-and-pull-request/)): why tests must pass without secrets.
- [GitHub Actions workflow anatomy](https://levelrail.com/learn/ci-cd/github-actions-workflow-anatomy/)): where `permissions` and `env` sit.

For secrets on the server side, see the blog guide [Manage secrets in self-hosted Docker](https://levelrail.com/blog/manage-secrets-self-hosted-docker/)).

## Next

Continue with [Cache dependencies in GitHub Actions](https://levelrail.com/learn/ci-cd/cache-dependencies-in-github-actions/)).

## Frequently asked questions

### What is the difference between secrets and variables in GitHub Actions?

Secrets are encrypted and masked in logs, and you use them for credentials. Variables are plain configuration values, visible to anyone who can edit the settings, and you read them with the vars context.

### Are secrets available to pull requests from forks?

Not for the pull_request event. GitHub does not pass repository secrets to workflows triggered by a pull request from a fork, and the token is read-only.

### Why is pull_request_target dangerous?

It runs in the context of the base repository with access to secrets and a write token. If it checks out and runs code from the pull request, an attacker's code runs with those privileges.

### What should I do if a secret leaked?

Revoke it at the source first, create a new one, update the CI secret, and then look for where it was used. Deleting the log line or commit does not make the old value safe again.


## Sources

- [GitHub Docs, using secrets in GitHub Actions](https://docs.github.com/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions)
- [GitHub Docs, security hardening for GitHub Actions](https://docs.github.com/en/actions/security-for-github-actions/security-guides/security-hardening-for-github-actions)
- [GitHub Docs, OpenID Connect](https://docs.github.com/en/actions/security-for-github-actions/security-hardening-your-deployments/about-security-hardening-with-openid-connect)
