# Secrets

> Give a job the secrets it needs, as environment variables or files, and nothing else.
> **Available** in repositories branchd reads pipeline files for. It is switching repositories on one at a time: until yours is, it keeps running its `.forge-ci.yaml` or `.xeonr-ci.yaml`. See [What is available](https://docs.branchd.run/branchd/pipelines#what-is-available).

```yaml title=".branchd/pipelines/ci.yaml"
# .branchd/pipelines/ci.yaml
# A job sees only the secrets it lists. publish gets NPM_TOKEN as $NPM_TOKEN;
# apply gets the secret KUBE_TOKEN under another name, $K8S_TOKEN.
name: CI
on:
  push:
    branches: [main]
jobs:
  publish:
    image: node:24
    secrets: [NPM_TOKEN]
    steps:
      - run: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
      - run: npm publish
  apply:
    needs: [publish]
    image: bitnami/kubectl:1.33
    secrets:
      KUBE_TOKEN: {target: K8S_TOKEN}
    steps:
      - run: kubectl --token "$K8S_TOKEN" apply -f k8s/
```

- **Set a secret** on the repository, or on a group so that every repository
  beneath it inherits it, in the repository's or group's pipeline settings
  on branchd. A secret's name is the variable a job reads (`DEPLOY_TOKEN`):
  letters, digits and `_`, not starting with a digit.
- **Which runs may read it** is set with the secret: _protected_ or _all_
  runs. A protected secret reaches only runs of a person's (or an agent's
  acting for a person) push to a protected branch or tag, and runs one of
  them starts by hand of a protected branch's current head. It never
  reaches a pull request, a schedule, a trigger, a bot's run or a re-run of
  an older commit. Use protected for anything that can deploy or publish.
- **Host only** is meant for signing material read only by a host job
  (`host: true`, run on a machine's own user, such as a Mac's Xcode signing
  job). It cannot be set yet. branchd refuses it until the pipelines
  service can hold a secret to host jobs alone. Until then, keep signing
  material protected and limited to the repositories that sign.

## Where to set a secret

Set a secret once, on the level that every repository needing it shares.
A repository gets the nearest setting of a name:

1. the repository's own;
2. each group above it, nearest first, out to the team's own (root) group;
3. the **company** that owns the team, if the team is in one;
4. for plain variables only, the branchd instance.

A secret on a company reaches every repository of every team the company
owns, so a credential several teams use (a package registry token, a store
upload key) lives there once instead of on each repository. A team that
leaves the company, or a repository moved to a team outside it, stops
receiving the company's secrets from its next run. branchd reads a team's
company from Xeonr Auth at most every five minutes. If Auth cannot be
reached, branchd uses its last answer for up to an hour; after that, runs
that need it wait for Auth to answer. Company settings are
changed by the company's owners and the organisation's administrators, the
people who also manage its runner pools, not by a team's admins. In the
console they are under **Company runners → your company → Pipelines**; with
the CLI:

```sh
printf '%s' "$NPM_TOKEN" | branchd secret set NPM_TOKEN --company xeonr --access all
branchd secret list --repo xeonr/forge/ui     # shows NPM_TOKEN as set on company xeonr
branchd variable set GOPROXY https://proxy.golang.org,direct --company xeonr
```

### Limit a secret to the repositories that need it

A secret on a group, team or company reaches every repository beneath it.
For anything that can sign, deploy or publish, limit it to the runs that
need it. Whoever may set the secret sets its limit with it:

- **Teams** (a company's secret only): only repositories of these teams.
- **One group**: only repositories under that group.
- **Repositories**: only these repositories.
- **Steps**: only these step packages are handed it (`platform/script` is
  every free-form job). This guards against accident, not against an
  attacker. A step that runs the repository's own code, such as
  `ci/image-build` or `ci/go-build`, or that has service containers, can
  read any secret it is handed, and any process in a job can read that
  job's secrets. A step limit narrows which steps get the secret, not what
  code can read it there.

Name teams, one group, or repositories, not a mix. A secret's conditions
must all hold, so it cannot say "this team or that repository".
Setting a secret again without a limit keeps its limit; `--no-limit`
clears it. The settings and `branchd secret list` show each secret's limit
and how many teams and repositories it reaches. A protected secret with
no limit says so plainly.

```sh
printf '%s' "$CSC_LINK" | branchd secret set CSC_LINK --company xeonr --access protected \
  --limit-repos pixelstream/tv,pixelstream/tv-new
```

`branchd secret set` reads the value from standard input only, never from an
argument. A repository's or group's pipeline settings list every secret it
inherits and where it is set, never the value.

- **A job sees only what it lists.** `secrets: [NPM_TOKEN]` grants that one,
  as an environment variable of the same name. `secrets:` as a map gives
  each one `{type, target}`: `target` is the variable it arrives in (default:
  the secret's name), and `type` is `env` (the default) or `file`, for a
  secret stored as a file, whose path then arrives in `target`.
- A secret is never in the pipeline file, in `plan` output or in a check.
  Keep it out of logs yourself: do not `echo` it or run with `set -x`.
- **Plain variables** (settings variables, not secret) are also set on a
  repository or group, and read as `${{ vars.NAME }}` or `$NAME`.

## A secret that is not set

- **A secret a job lists that is set nowhere** fails that job's attempt
  before its steps run (the run's other jobs go on), and the plan shows it
  as missing.
- **Optional secrets.** A step can declare a secret it can do without:
  `secrets: {SENTRY_TOKEN: {required: false}}` in a composite step's
  `step.yaml`, or a secret a typed step's package marks optional (such as
  `LANTERN_API_KEY` for `branchd/steps/deploy/gitops-flux`, which only attributes
  deploys).
  When it is set nowhere the job runs without it, and the plan shows it as
  skipped, not as missing. A `required: true` secret of a composite step
  that the job does not grant is refused when the run is compiled.
- **A job's optional grant.** `secrets: {NPM_TOKEN: {optional: true}}` on a
  job grants the secret when its policy lets it reach the run, and withholds
  it, the job running without it, when it is missing or its policy refuses
  it.
- **A reusable pipeline's** `on.call.secrets` are `{required, description}`:
  a caller must pass each required one, and may leave out the others.

What a step from another repository can do with a job's secrets is on the
[Security](https://docs.branchd.run/branchd/pipelines/security) page.

Source: https://docs.branchd.run/branchd/pipelines/secrets
