Skip to content

Commit 891c1d7

Browse files
jcgruenhagedvdksn
andauthored
build: git remote authentication secrets (#23460)
## Description I fixed the documentation, because it was wrong. See the issues and buildkit PR for details. ## Related issues or tickets - BuildKit issue: moby/buildkit#5703 - Docs issue: #22834 - Another BuildKit issue: moby/buildkit#6046 - PR that introduced the behaviour: moby/buildkit#1533 - PR that introduced the docs: #19739 ## Reviews <!-- Notes for reviewers here --> <!-- List applicable reviews (optionally @tag reviewers) --> - [ ] Technical review - [ ] Editorial review - [ ] Product review --------- Co-authored-by: David Karlsson <35727626+dvdksn@users.noreply.github.com>
1 parent 95fff41 commit 891c1d7

1 file changed

Lines changed: 43 additions & 26 deletions

File tree

content/manuals/build/building/secrets.md

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -175,61 +175,80 @@ building with remote, private Git repositories, including:
175175
- Building with a private Git repository as build context
176176
- Fetching private Git repositories in a build with `ADD`
177177

178-
For example, say you have a private GitLab project at
179-
`https://gitlab.com/example/todo-app.git`, and you want to run a build using
178+
For example, say you have a private GitHub repository at
179+
`https://github.com/example/todo-app.git`, and you want to run a build using
180180
that repository as the build context. An unauthenticated `docker build` command
181181
fails because the builder isn't authorized to pull the repository:
182182

183183
```console
184-
$ docker build https://gitlab.com/example/todo-app.git
184+
$ docker build https://github.com/example/todo-app.git
185185
[+] Building 0.4s (1/1) FINISHED
186-
=> ERROR [internal] load git source https://gitlab.com/example/todo-app.git
186+
=> ERROR [internal] load git source https://github.com/example/todo-app.git
187187
------
188-
> [internal] load git source https://gitlab.com/example/todo-app.git:
189-
0.313 fatal: could not read Username for 'https://gitlab.com': terminal prompts disabled
188+
> [internal] load git source https://github.com/example/todo-app.git:
189+
0.313 fatal: could not read Username for 'https://github.com': terminal prompts disabled
190190
------
191191
```
192192

193-
To authenticate the builder to the Git server, set the `GIT_AUTH_TOKEN`
194-
environment variable to contain a valid GitLab access token, and pass it as a
193+
To authenticate the builder to GitHub, set the `GIT_AUTH_TOKEN`
194+
environment variable to contain a valid GitHub access token, and pass it as a
195195
secret to the build:
196196

197197
```console
198-
$ GIT_AUTH_TOKEN=$(cat gitlab-token.txt) docker build \
198+
$ GIT_AUTH_TOKEN=$(gh auth token) docker build \
199199
--secret id=GIT_AUTH_TOKEN \
200-
https://gitlab.com/example/todo-app.git
200+
https://github.com/example/todo-app.git
201201
```
202202

203203
The `GIT_AUTH_TOKEN` also works with `ADD` to fetch private Git repositories as
204204
part of your build:
205205

206206
```dockerfile
207207
FROM alpine
208-
ADD https://gitlab.com/example/todo-app.git /src
208+
ADD https://github.com/example/todo-app.git /src
209209
```
210210

211211
### HTTP authentication scheme
212212

213-
By default, Git authentication over HTTP uses the Bearer authentication scheme:
213+
BuildKit supports two Git authentication secrets:
214+
215+
- **`GIT_AUTH_TOKEN`**: Uses Basic authentication with a fixed username of `x-access-token` (the GitHub-style default)
216+
- **`GIT_AUTH_HEADER`**: Uses the raw authorization header value you provide (works with any Git provider)
217+
218+
#### Using GIT_AUTH_TOKEN (for example, GitHub)
219+
220+
When you use `GIT_AUTH_TOKEN`, BuildKit constructs a Basic authentication header using `x-access-token` as the user:
214221

215222
```http
216-
Authorization: Bearer <GIT_AUTH_TOKEN>
223+
Authorization: Basic <base64("x-access-token:<GIT_AUTH_TOKEN>")>
217224
```
218225

219-
If you need to use a Basic scheme, with a username and password, you can set
220-
the `GIT_AUTH_HEADER` build secret:
226+
This method works for providers that accept the `x-access-token` Basic auth pattern, such as GitHub. Example usage:
221227

222228
```console
223-
$ export GIT_AUTH_TOKEN=$(cat gitlab-token.txt)
224-
$ export GIT_AUTH_HEADER=basic
229+
$ export GIT_AUTH_TOKEN=$(gh auth token)
225230
$ docker build \
226231
--secret id=GIT_AUTH_TOKEN \
232+
https://github.com/example/todo-app.git
233+
```
234+
235+
#### Using GIT_AUTH_HEADER (custom authorization header)
236+
237+
When you use `GIT_AUTH_HEADER`, BuildKit uses the exact value you provide as the `Authorization` header:
238+
239+
```http
240+
Authorization: <GIT_AUTH_HEADER>
241+
```
242+
243+
Example usage with GitLab CI/CD token:
244+
245+
```console
246+
$ export GIT_AUTH_HEADER="Basic $(echo -n "gitlab-ci-token:${CI_JOB_TOKEN}" | base64)"
247+
$ docker build \
227248
--secret id=GIT_AUTH_HEADER \
228249
https://gitlab.com/example/todo-app.git
229250
```
230251

231-
BuildKit currently only supports the Bearer and Basic schemes.
232-
233252
### Multiple hosts
234253

235254
You can set the `GIT_AUTH_TOKEN` and `GIT_AUTH_HEADER` secrets on a per-host
@@ -238,12 +257,10 @@ hostnames. To specify a hostname, append the hostname as a suffix to the secret
238257
ID:
239258

240259
```console
241-
$ export GITLAB_TOKEN=$(cat gitlab-token.txt)
242-
$ export GERRIT_TOKEN=$(cat gerrit-username-password.txt)
243-
$ export GERRIT_SCHEME=basic
260+
$ export GITHUB_TOKEN=$(gh auth token)
261+
$ export GITLAB_AUTH_HEADER="Basic $(echo -n "gitlab-ci-token:${CI_JOB_TOKEN}" | base64)"
244262
$ docker build \
245-
--secret id=GIT_AUTH_TOKEN.gitlab.com,env=GITLAB_TOKEN \
246-
--secret id=GIT_AUTH_TOKEN.gerrit.internal.example,env=GERRIT_TOKEN \
247-
--secret id=GIT_AUTH_HEADER.gerrit.internal.example,env=GERRIT_SCHEME \
248-
https://gitlab.com/example/todo-app.git
263+
--secret id=GIT_AUTH_TOKEN.github.com,env=GITHUB_TOKEN \
264+
--secret id=GIT_AUTH_HEADER.gitlab.com,env=GITLAB_AUTH_HEADER \
265+
https://github.com/example/todo-app.git
249266
```

0 commit comments

Comments
 (0)