Skip to content

Commit 0dffd5b

Browse files
committed
add support for build.secrets
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent e016874 commit 0dffd5b

File tree

7 files changed

+70
-3
lines changed

7 files changed

+70
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/AlecAivazis/survey/v2 v2.3.2
77
github.com/buger/goterm v1.0.4
88
github.com/cnabio/cnab-to-oci v0.3.1-beta1
9-
github.com/compose-spec/compose-go v1.2.3
9+
github.com/compose-spec/compose-go v1.2.4
1010
github.com/containerd/console v1.0.3
1111
github.com/containerd/containerd v1.6.2
1212
github.com/distribution/distribution/v3 v3.0.0-20210316161203-a01c71e2477e

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoC
302302
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
303303
github.com/codahale/hdrhistogram v0.0.0-20160425231609-f8ad88b59a58/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
304304
github.com/compose-spec/compose-go v1.0.8/go.mod h1:REnCbBugoIdHB7S1sfkN/aJ7AJpNApGNjNiVjA9L8x4=
305-
github.com/compose-spec/compose-go v1.2.3 h1:r9zZfxQKG2A3fTy/MobMecUR1cd3uf7DlQQeB/NKVJ0=
306-
github.com/compose-spec/compose-go v1.2.3/go.mod h1:pAy7Mikpeft4pxkFU565/DRHEbDfR84G6AQuiL+Hdg8=
305+
github.com/compose-spec/compose-go v1.2.4 h1:nzTFqM8+2J7Veao5Pq5U451thinv3U1wChIvcjX59/A=
306+
github.com/compose-spec/compose-go v1.2.4/go.mod h1:pAy7Mikpeft4pxkFU565/DRHEbDfR84G6AQuiL+Hdg8=
307307
github.com/compose-spec/godotenv v1.1.1/go.mod h1:zF/3BOa18Z24tts5qnO/E9YURQanJTBUf7nlcCTNsyc=
308308
github.com/containerd/aufs v0.0.0-20200908144142-dab0cbea06f4/go.mod h1:nukgQABAEopAHvB6j7cnP5zJ+/3aVcE7hCYqvIwAHyE=
309309
github.com/containerd/aufs v0.0.0-20201003224125-76a6863f2989/go.mod h1:AkGGQs9NM2vtYHaUen+NljV0/baGCAPELGm2q9ZXpWU=

pkg/compose/build.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
bclient "github.com/moby/buildkit/client"
3232
"github.com/moby/buildkit/session"
3333
"github.com/moby/buildkit/session/auth/authprovider"
34+
"github.com/moby/buildkit/session/secrets/secretsprovider"
3435
"github.com/moby/buildkit/session/sshforward/sshprovider"
3536
specs "github.com/opencontainers/image-spec/specs-go/v1"
3637

@@ -254,6 +255,26 @@ func (s *composeService) toBuildOptions(project *types.Project, service types.Se
254255
sessionConfig = append(sessionConfig, sshAgentProvider)
255256
}
256257

258+
if len(service.Build.Secrets) > 0 {
259+
var sources []secretsprovider.Source
260+
for _, secret := range service.Build.Secrets {
261+
config := project.Secrets[secret.Source]
262+
if config.File == "" {
263+
return build.Options{}, fmt.Errorf("build.secrets only supports file-based secrets: %q", secret.Source)
264+
}
265+
sources = append(sources, secretsprovider.Source{
266+
ID: secret.Source,
267+
FilePath: config.File,
268+
})
269+
}
270+
store, err := secretsprovider.NewStore(sources)
271+
if err != nil {
272+
return build.Options{}, err
273+
}
274+
p := secretsprovider.NewSecretProvider(store)
275+
sessionConfig = append(sessionConfig, p)
276+
}
277+
257278
return build.Options{
258279
Inputs: build.Inputs{
259280
ContextPath: service.Build.Context,

pkg/e2e/compose_build_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,15 @@ func TestLocalComposeBuild(t *testing.T) {
169169
c.RunDockerCmd("rmi", "custom-nginx")
170170
})
171171
}
172+
173+
func TestBuildSecrets(t *testing.T) {
174+
c := NewParallelE2eCLI(t, binDir)
175+
176+
t.Run("build with secrets", func(t *testing.T) {
177+
// ensure local test run does not reuse previously build image
178+
c.RunDockerOrExitError("rmi", "build-test-secret")
179+
180+
res := c.RunDockerComposeCmd("--project-directory", "fixtures/build-test/secrets", "build")
181+
res.Assert(t, icmd.Success)
182+
})
183+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# syntax=docker/dockerfile:1.2
2+
3+
4+
# Copyright 2020 Docker Compose CLI authors
5+
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
FROM alpine
19+
20+
RUN echo "foo" > /tmp/expected
21+
RUN --mount=type=secret,id=mysecret cat /run/secrets/mysecret > /tmp/actual
22+
RUN diff /tmp/expected /tmp/actual
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
ssh:
3+
image: build-test-secret
4+
build:
5+
context: .
6+
secrets:
7+
- mysecret
8+
9+
secrets:
10+
mysecret:
11+
file: ./secret.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo

0 commit comments

Comments
 (0)