Skip to content

Commit 42758dc

Browse files
authored
Merge pull request #4 from lsst-sqre/tickets/DM-52930
DM-52930: Add IMAGE_SECRETS for build-time secrets
2 parents dfeb010 + b6cb755 commit 42758dc

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

.github/workflows/build.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ on:
5858
required: false
5959
GAR_PUSH_TOKEN:
6060
required: false
61-
61+
IMAGE_SECRETS:
62+
description: "Build-time secrets to pass to docker build, in KEY=VALUE format, one per line."
63+
required: false
6264

6365
# We need actions/write if we want to do a GH App, and we need
6466
# packages/write to push to ghcr.io with GITHUB_TOKEN
@@ -170,6 +172,7 @@ jobs:
170172
push: ${{ inputs.push }}
171173
target: ${{ inputs.target }}
172174
build-args: ${{ inputs.build-args }}
175+
secrets: ${{ secrets.IMAGE_SECRETS }}
173176
builder: builder-${{ matrix.platform.arch }}
174177
platforms: linux/${{ matrix.platform.arch }}
175178
tags: ${{ steps.calculate_tags.outputs.tags }}

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,48 @@ jobs:
4848
By default, ghcr.io packages are named after the GitHub repository.
4949
To automatically set that, the above example uses the context variable `${{ github.repository }}` as the image name.
5050

51+
### Using build-time secrets
52+
53+
If your Docker build requires secrets at build time (e.g., for private npm packages, Sentry authentication, or API tokens), you can pass them using the `IMAGE_SECRETS` secret parameter. These secrets are made available to the Docker build process using Docker BuildKit's [secret mounts](https://docs.docker.com/build/building/secrets/).
54+
55+
#### Example usage
56+
57+
```yaml
58+
jobs:
59+
build:
60+
uses: lsst-sqre/multiplatform-build-and-push/.github/workflows/build.yaml@v1
61+
with:
62+
images: ghcr.io/${{ github.repository }}
63+
secrets:
64+
IMAGE_SECRETS: |
65+
SENTRY_AUTH_TOKEN=${{ secrets.SENTRY_AUTH_TOKEN }}
66+
NPM_TOKEN=${{ secrets.NPM_TOKEN }}
67+
```
68+
69+
Note that the `IMAGE_SECRETS` secret is defined in the `secrets` section of the job, not in the `with` section.
70+
Using `IMAGE_SECRETS` is not compatible with using `secrets: inherit` in the job.
71+
72+
#### Format
73+
74+
The `IMAGE_SECRETS` value should be a multiline string with one secret per line in `KEY=VALUE` format:
75+
76+
```
77+
SECRET_NAME=${{ secrets.SECRET_NAME }}
78+
ANOTHER_SECRET=${{ secrets.ANOTHER_SECRET }}
79+
```
80+
81+
#### Accessing secrets in your Dockerfile
82+
83+
In your Dockerfile, mount and use these secrets with the `--mount=type=secret` syntax:
84+
85+
```dockerfile
86+
RUN --mount=type=secret,id=NPM_TOKEN \
87+
echo "//registry.npmjs.org/:_authToken=$(cat /run/secrets/NPM_TOKEN)" > ~/.npmrc && \
88+
npm install
89+
```
90+
91+
**Note**: Build-time secrets are not stored in the final image layers, making them safe for private credentials.
92+
5193
## Action reference
5294

5395
### Inputs

0 commit comments

Comments
 (0)