Skip to content

Commit 9b815e3

Browse files
authored
Merge pull request #19739 from dvdksn/build-predefined-secrets
build: git remote authentication secrets
2 parents e57be72 + da207fa commit 9b815e3

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

content/build/building/secrets.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Build secrets
33
description: Manage credentials and other secrets securely
4-
keywords: build, secrets, credentials, passwords, tokens
4+
keywords: build, secrets, credentials, passwords, tokens, ssh, git, auth, http
55
---
66

77
A build secret is any piece of sensitive information, such as a password or API
@@ -116,3 +116,85 @@ options for [Bake](../bake/reference.md#targetssh).
116116
```console
117117
$ docker buildx build --ssh default .
118118
```
119+
120+
## Git authentication for remote contexts
121+
122+
BuildKit supports two pre-defined build secrets, `GIT_AUTH_TOKEN` and
123+
`GIT_AUTH_HEADER`. Use them to specify HTTP authentication parameters when
124+
building with remote, private Git repositories, including:
125+
126+
- Building with a private Git repository as build context
127+
- Fetching private Git repositories in a build with `ADD`
128+
129+
For example, say you have a private GitLab project at
130+
`https://gitlab.com/example/todo-app.git`, and you want to run a build using
131+
that repository as the build context. An unauthenticated `docker build` command
132+
fails because the builder isn't authorized to pull the repository:
133+
134+
```console
135+
$ docker build https://gitlab.com/example/todo-app.git
136+
[+] Building 0.4s (1/1) FINISHED
137+
=> ERROR [internal] load git source https://gitlab.com/dvdk/todo-app.git
138+
------
139+
> [internal] load git source https://gitlab.com/dvdk/todo-app.git:
140+
0.313 fatal: could not read Username for 'https://gitlab.com': terminal prompts disabled
141+
------
142+
```
143+
144+
To authenticate the builder to the Git server, set the `GIT_AUTH_TOKEN`
145+
environment variable to contain a valid GitLab access token, and pass it as a
146+
secret to the build:
147+
148+
```console
149+
$ GIT_AUTH_TOKEN=$(cat gitlab-token.txt) docker build \
150+
--secret id=GIT_AUTH_TOKEN \
151+
https://gitlab.com/example/todo-app.git
152+
```
153+
154+
The `GIT_AUTH_TOKEN` also works with `ADD` to fetch private Git repositories as
155+
part of your build:
156+
157+
```dockerfile
158+
FROM alpine
159+
ADD https://gitlab.com/example/todo-app.git /src
160+
```
161+
162+
### HTTP authentication scheme
163+
164+
By default, Git authentication over HTTP uses the Bearer authentication scheme:
165+
166+
```http
167+
Authorization: Bearer <GIT_AUTH_TOKEN>
168+
```
169+
170+
If you need to use a Basic scheme, with a username and password, you can set
171+
the `GIT_AUTH_HEADER` build secret:
172+
173+
```console
174+
$ export GIT_AUTH_TOKEN=$(cat gitlab-token.txt)
175+
$ export GIT_AUTH_HEADER=basic
176+
$ docker build \
177+
--secret id=GIT_AUTH_TOKEN \
178+
--secret id=GIT_AUTH_HEADER \
179+
https://gitlab.com/example/todo-app.git
180+
```
181+
182+
BuildKit currently only supports the Bearer and Basic schemes.
183+
184+
### Multiple hosts
185+
186+
You can set the `GIT_AUTH_TOKEN` and `GIT_AUTH_HEADER` secrets on a per-host
187+
basis, which lets you use different authentication parameters for different
188+
hostnames. To specify a hostname, append the hostname as a suffix to the secret
189+
ID:
190+
191+
```console
192+
$ export GITLAB_TOKEN=$(cat gitlab-token.txt)
193+
$ export GERRIT_TOKEN=$(cat gerrit-username-password.txt)
194+
$ export GERRIT_SCHEME=basic
195+
$ docker build \
196+
--secret id=GIT_AUTH_TOKEN.gitlab.com,env=GITLAB_TOKEN \
197+
--secret id=GIT_AUTH_TOKEN.gerrit.internal.example,env=GERRIT_TOKEN \
198+
--secret id=GIT_AUTH_HEADER.gerrit.internal.example,env=GERRIT_SCHEME \
199+
https://gitlab.com/example/todo-app.git
200+
```

0 commit comments

Comments
 (0)