@@ -57,14 +57,128 @@ jobs:
5757 "github_token=${{ secrets.GITHUB_TOKEN }}"
5858` ` `
5959
60- > [!NOTE]
61- >
62- > You can also expose a secret file to the build with the ` secret-files` input:
63- >
64- > ```yaml
65- > secret-files: |
66- > "MY_SECRET=./secret.txt"
67- > ```
60+ ### Using secret files
61+
62+ The ` secret-files` input lets you mount existing files as secrets in your build.
63+ This is useful when you need to use credential files that are generated during your workflow,
64+ or when you need to mount configuration files like `.npmrc` or `.pypirc` that are already in the expected format.
65+
66+ The key difference between `secrets` and `secret-files` :
67+
68+ - ` secrets` - Pass secret values as strings (from environment variables or GitHub secrets)
69+ - ` secret-files` - Mount existing files from the runner's filesystem
70+
71+ # ### When to use secret-files
72+
73+ Use `secret-files` when :
74+
75+ - You generate credential files earlier in your workflow
76+ - You need to mount files that are already in a specific format (like `.npmrc` for npm authentication)
77+ - You check out credential files from a secure repository
78+ - You create configuration files dynamically based on multiple secrets
79+
80+ # ### Example: Using .npmrc for private npm packages
81+
82+ If your build needs to install packages from a private npm registry,
83+ you can create an `.npmrc` file and mount it as a secret :
84+
85+ ` ` ` yaml
86+ name: ci
87+
88+ on:
89+ push:
90+
91+ jobs:
92+ docker:
93+ runs-on: ubuntu-latest
94+ steps:
95+ - name: Checkout
96+ uses: actions/checkout@v4
97+
98+ - name: Set up Docker Buildx
99+ uses: docker/setup-buildx-action@{{% param "setup_buildx_action_version" %}}
100+
101+ - name: Create .npmrc file
102+ run: |
103+ echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > .npmrc
104+
105+ - name: Build
106+ uses: docker/build-push-action@{{% param "build_push_action_version" %}}
107+ with:
108+ context: .
109+ secret-files: |
110+ "npmrc=./.npmrc"
111+ tags: user/app:latest
112+ ` ` `
113+
114+ In your Dockerfile, mount the secret file to the expected location :
115+
116+ ` ` ` dockerfile
117+ # syntax=docker/dockerfile:1
118+ FROM node:20-alpine
119+
120+ WORKDIR /app
121+
122+ COPY package*.json ./
123+
124+ RUN --mount=type=secret,id=npmrc,target=/root/.npmrc \
125+ npm ci
126+
127+ COPY . .
128+
129+ RUN npm run build
130+ ` ` `
131+
132+ # ### Example: Using dynamically generated credentials
133+
134+ You can generate credential files from multiple secrets and mount them :
135+
136+ ` ` ` yaml
137+ name: ci
138+
139+ on:
140+ push:
141+
142+ jobs:
143+ docker:
144+ runs-on: ubuntu-latest
145+ steps:
146+ - name: Checkout
147+ uses: actions/checkout@v4
148+
149+ - name: Set up Docker Buildx
150+ uses: docker/setup-buildx-action@{{% param "setup_buildx_action_version" %}}
151+
152+ - name: Create credentials file
153+ run: |
154+ cat <<EOF > aws-credentials
155+ [default]
156+ aws_access_key_id = ${{ secrets.AWS_ACCESS_KEY_ID }}
157+ aws_secret_access_key = ${{ secrets.AWS_SECRET_ACCESS_KEY }}
158+ EOF
159+
160+ - name: Build
161+ uses: docker/build-push-action@{{% param "build_push_action_version" %}}
162+ with:
163+ context: .
164+ secret-files: |
165+ "aws=./aws-credentials"
166+ tags: user/app:latest
167+ ` ` `
168+
169+ In your Dockerfile :
170+
171+ ` ` ` dockerfile
172+ # syntax=docker/dockerfile:1
173+ FROM alpine
174+
175+ RUN apk add --no-cache aws-cli
176+
177+ RUN --mount=type=secret,id=aws,target=/root/.aws/credentials \
178+ aws s3 cp s3://my-private-bucket/data.tar.gz /tmp/
179+ ` ` `
180+
181+ # ## Multi-line secrets
68182
69183If you're using [GitHub secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets)
70184and need to handle multi-line value, you will need to place the key-value pair
0 commit comments