# Build secrets

A build sometimes needs a secret to install your code: a token for a private npm registry, a password for a private package index. A **build secret** gives the build that value while it runs, and nowhere else. It is never written into the image, the build cache or the logs, and it is never in the running service.

## Three kinds of values

| Kind | Where you set it | In the image? | Example |
|---|---|---|---|
| **Build secret** | The console, in **Build secrets**. The value is write-only. | Never | `NPM_TOKEN` |
| **Build variable** | `build.args` in your `zaroz.toml` | Yes, if the Dockerfile uses it | `NEXT_PUBLIC_API_URL` |
| **Environment variable** | The service's environment | No, it is for the running service | `DATABASE_URL` |

- Use a build secret for anything private that the build needs.
- Use a build variable for a value that is meant to be inside the build, such as a public URL that a framework bakes into the code. Its value is visible in the image, so never put a password there. Zaroz warns you if a build variable looks like a token.
- The environment variables of your running service are **not** available during the build. If the build needs a value, make it a build variable or a build secret.

## Add a secret

1. Open the service and go to the **Source** tab.
2. Open **Build secrets** and add the secret: a name and a value.
3. Name it in your [`zaroz.toml`](/git-builds/zaroz-toml), in `build.secrets`:

```toml
[build]
secrets = ["NPM_TOKEN"]
```

You can only see the names and when they were updated. The value is never shown again. To change it, add the secret again with the new value. To stop using it, remove it.

If the file names a secret that the service does not have, the build stops with [`secret_missing`](/git-builds/troubleshooting#secret_missing).

**Names and limits.** A name uses capital letters, digits and underscores, starts with a letter and has at most 64 characters. A service can have 50 secrets. Each can be up to 64 KiB, enough for a Maven `settings.xml`.

Anyone who can manage the service can see the list of secrets. When you create a service from a repository, Zaroz points out likely secrets: for example an `.npmrc` that uses `${NPM_TOKEN}`, or a `requirements.txt` with a private index.

## Use a secret

### In an automatically detected build

The secret is available as an environment variable with the same name during the install and build steps. Your tool reads it from there.

### In a Dockerfile

Mount the secret in the `RUN` line that needs it. BuildKit shows it to that one step and does not keep it in any layer:

```dockerfile
RUN --mount=type=secret,id=NPM_TOKEN,env=NPM_TOKEN npm ci
```

Do not use `ARG` or `ENV` for a secret. They end up in the image.

If a secret changes, a Railpack build uses the new value on its next build. A Dockerfile build may use cached layers, so choose **Rebuild without cache** after you change one.

## Logs

Zaroz hides the value of every secret in the build log, and also its base64 and URL-encoded forms. Do not print a secret on purpose. Hiding it is a safety net, not a plan.

## Recipes

In each recipe, you create the secret in the console, list its name in `build.secrets`, and keep the **name** of the secret in your repository, never the value.

### npm and pnpm

Secret: `NPM_TOKEN`. Commit an `.npmrc` that refers to it:

```ini
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
```

For a private scope on another registry:

```ini
@myorg:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${NPM_TOKEN}
```

### Yarn (version 2 and later)

Secret: `NPM_TOKEN`. In `.yarnrc.yml`:

```yaml
npmRegistries:
  "https://registry.example.com":
    npmAuthToken: "${NPM_TOKEN}"
```

### pip

Secret: `PIP_INDEX_URL`, with the full address of the index including the credentials, for example `https://user:token@pypi.example.com/simple/`. pip reads this variable by itself. Do not write the address with credentials in `requirements.txt`.

### Poetry

Two secrets, for a source named `private`: `POETRY_HTTP_BASIC_PRIVATE_USERNAME` and `POETRY_HTTP_BASIC_PRIVATE_PASSWORD`. The name of the source is in capitals, with `-` written as `_`.

### Composer

Secret: `COMPOSER_AUTH`, with this JSON as its value:

```json
{"http-basic": {"repo.example.com": {"username": "user", "password": "token"}}}
```

### Maven

Secrets: `MAVEN_USERNAME` and `MAVEN_PASSWORD`. Commit a `settings.xml` that refers to them:

```xml
<server>
  <id>private</id>
  <username>${env.MAVEN_USERNAME}</username>
  <password>${env.MAVEN_PASSWORD}</password>
</server>
```

Then run Maven with that file, for example with `build.command = "mvn -s settings.xml -B package"`.

### Gradle

Secrets: `ORG_GRADLE_PROJECT_mavenUser` and `ORG_GRADLE_PROJECT_mavenPassword`. Gradle turns variables that start with `ORG_GRADLE_PROJECT_` into project properties, so in `build.gradle`:

```groovy
credentials {
    username = findProperty("mavenUser")
    password = findProperty("mavenPassword")
}
```

### Bundler

Secret: `BUNDLE_GEMS__EXAMPLE__COM`, with `user:token` as its value. The host name is in capitals, with each `.` written as `__`.

## A build says 401 or 403

A line like `E401`, `E403` or `403 Client Error` in the log means a private registry refused the install. Zaroz points this out on the failed build. Add the secret and retry. See [`build_step_failed`](/git-builds/troubleshooting#build_step_failed).
