Skip to content

Commit 3b435ae

Browse files
committed
Support docker build --secret flag
1 parent 67e81bc commit 3b435ae

File tree

8 files changed

+85
-2
lines changed

8 files changed

+85
-2
lines changed

docs/content/en/schemas/v2beta7.json

+31-1
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,11 @@
923923
"x-intellij-html-description": "used to pass in --no-cache to docker build to prevent caching.",
924924
"default": "false"
925925
},
926+
"secret": {
927+
"$ref": "#/definitions/DockerSecret",
928+
"description": "contains information about a local secret passed to `docker build`, along with optional destination information.",
929+
"x-intellij-html-description": "contains information about a local secret passed to <code>docker build</code>, along with optional destination information."
930+
},
926931
"target": {
927932
"type": "string",
928933
"description": "Dockerfile target name to build.",
@@ -935,7 +940,8 @@
935940
"buildArgs",
936941
"network",
937942
"cacheFrom",
938-
"noCache"
943+
"noCache",
944+
"secret"
939945
],
940946
"additionalProperties": false,
941947
"description": "describes an artifact built from a Dockerfile, usually using `docker build`.",
@@ -962,6 +968,30 @@
962968
"description": "contains information about the docker `config.json` to mount.",
963969
"x-intellij-html-description": "contains information about the docker <code>config.json</code> to mount."
964970
},
971+
"DockerSecret": {
972+
"required": [
973+
"id"
974+
],
975+
"properties": {
976+
"id": {
977+
"type": "string",
978+
"description": "id of the secret.",
979+
"x-intellij-html-description": "id of the secret."
980+
},
981+
"src": {
982+
"type": "string",
983+
"description": "path to the secret on the host machine.",
984+
"x-intellij-html-description": "path to the secret on the host machine."
985+
}
986+
},
987+
"preferredOrder": [
988+
"id",
989+
"src"
990+
],
991+
"additionalProperties": false,
992+
"description": "contains information about a local secret passed to `docker build`, along with optional destination information.",
993+
"x-intellij-html-description": "contains information about a local secret passed to <code>docker build</code>, along with optional destination information."
994+
},
965995
"DockerfileDependency": {
966996
"properties": {
967997
"buildArgs": {

hack/schemas/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,11 @@ func (g *schemaGenerator) newDefinition(name string, t ast.Expr, comment string,
264264
}
265265

266266
if g.strict && name != "" {
267+
if comment == "" {
268+
panic(fmt.Sprintf("field %s needs comment (all public fields require comments)", name))
269+
}
267270
if !strings.HasPrefix(comment, name+" ") {
268-
panic(fmt.Sprintf("comment should start with field name on field %s", name))
271+
panic(fmt.Sprintf("comment %s should start with field name on field %s", comment, name))
269272
}
270273
}
271274

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# syntax=docker/dockerfile:1.0.0-experimental
2+
3+
FROM alpine:3.10
4+
5+
RUN --mount=type=secret,id=mysecret cat /run/secrets/mysecret
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
secret

integration/testdata/build/skaffold.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ apiVersion: skaffold/v2beta7
22
kind: Config
33
build:
44
local:
5+
useBuildkit: true
56
push: false
67
artifacts:
78
# A simple Docker build
@@ -38,3 +39,10 @@ build:
3839
# Would have caught #2315
3940
- image: multi-stage
4041
context: multi-stage
42+
43+
- image: secret
44+
context: secret
45+
docker:
46+
secret:
47+
id: mysecret
48+
src: secret/mysecret.txt

pkg/skaffold/docker/image.go

+12
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ func (l *localDaemon) ConfigFile(ctx context.Context, image string) (*v1.ConfigF
160160
func (l *localDaemon) Build(ctx context.Context, out io.Writer, workspace string, a *latest.DockerArtifact, ref string, mode config.RunMode) (string, error) {
161161
logrus.Debugf("Running docker build: context: %s, dockerfile: %s", workspace, a.DockerfilePath)
162162

163+
if a.Secret != nil {
164+
return "", fmt.Errorf("docker build secrets require BuildKit - set `useBuildkit: true` in your config, or run with `DOCKER_BUILDKIT=1`")
165+
}
166+
163167
// Like `docker build`, we ignore the errors
164168
// See https://github.com/docker/cli/blob/75c1bb1f33d7cedbaf48404597d5bf9818199480/cli/command/image/build.go#L364
165169
authConfigs, _ := DefaultAuthHelper.GetAllAuthConfigs()
@@ -462,6 +466,14 @@ func ToCLIBuildArgs(a *latest.DockerArtifact, evaluatedArgs map[string]*string)
462466
args = append(args, "--no-cache")
463467
}
464468

469+
if a.Secret != nil {
470+
secretString := fmt.Sprintf("id=%s", a.Secret.ID)
471+
if a.Secret.Source != "" {
472+
secretString += ",src=" + a.Secret.Source
473+
}
474+
args = append(args, "--secret", secretString)
475+
}
476+
465477
return args, nil
466478
}
467479

pkg/skaffold/docker/image_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,16 @@ func TestGetBuildArgs(t *testing.T) {
312312
},
313313
want: []string{"--no-cache"},
314314
},
315+
{
316+
description: "secret",
317+
artifact: &latest.DockerArtifact{
318+
Secret: &latest.DockerSecret{
319+
ID: "mysecret",
320+
Source: "foo.bar",
321+
},
322+
},
323+
want: []string{"--secret", "id=mysecret,src=foo.bar"},
324+
},
315325
{
316326
description: "all",
317327
artifact: &latest.DockerArtifact{

pkg/skaffold/schema/latest/config.go

+14
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,20 @@ type DockerArtifact struct {
964964

965965
// NoCache used to pass in --no-cache to docker build to prevent caching.
966966
NoCache bool `yaml:"noCache,omitempty"`
967+
968+
// Secret contains information about a local secret passed to `docker build`,
969+
// along with optional destination information.
970+
Secret *DockerSecret `yaml:"secret,omitempty"`
971+
}
972+
973+
// DockerSecret contains information about a local secret passed to `docker build`,
974+
// along with optional destination information.
975+
type DockerSecret struct {
976+
// ID is the id of the secret.
977+
ID string `yaml:"id,omitempty" yamltags:"required"`
978+
979+
// Source is the path to the secret on the host machine.
980+
Source string `yaml:"src,omitempty"`
967981
}
968982

969983
// BazelArtifact describes an artifact built with [Bazel](https://bazel.build/).

0 commit comments

Comments
 (0)