Skip to content

Commit

Permalink
use ssh config when building from compose up
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Lours <[email protected]>
  • Loading branch information
glours authored and ndeloof committed Apr 4, 2022
1 parent fcff36f commit f2d9acd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
30 changes: 15 additions & 15 deletions pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
if service.Build != nil {
imageName := getImageName(service, project.Name)
imagesToBuild = append(imagesToBuild, imageName)
buildOptions, err := s.toBuildOptions(project, service, imageName)
buildOptions, err := s.toBuildOptions(project, service, imageName, options.SSHs)
if err != nil {
return err
}
Expand All @@ -81,15 +81,6 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
Attrs: map[string]string{"ref": image},
})
}

if len(options.SSHs) > 0 || len(service.Build.SSH) > 0 {
sshAgentProvider, err := sshAgentProvider(append(service.Build.SSH, options.SSHs...))
if err != nil {
return err
}
buildOptions.Session = append(buildOptions.Session, sshAgentProvider)
}

opts[imageName] = buildOptions
}
}
Expand Down Expand Up @@ -168,7 +159,7 @@ func (s *composeService) getBuildOptions(project *types.Project, images map[stri
if localImagePresent && service.PullPolicy != types.PullPolicyBuild {
continue
}
opt, err := s.toBuildOptions(project, service, imageName)
opt, err := s.toBuildOptions(project, service, imageName, []types.SSHKey{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -218,7 +209,7 @@ func (s *composeService) doBuild(ctx context.Context, project *types.Project, op
return s.doBuildBuildkit(ctx, project, opts, mode)
}

func (s *composeService) toBuildOptions(project *types.Project, service types.ServiceConfig, imageTag string) (build.Options, error) {
func (s *composeService) toBuildOptions(project *types.Project, service types.ServiceConfig, imageTag string, sshKeys []types.SSHKey) (build.Options, error) {
var tags []string
tags = append(tags, imageTag)

Expand Down Expand Up @@ -252,6 +243,17 @@ func (s *composeService) toBuildOptions(project *types.Project, service types.Se
return build.Options{}, err
}

sessionConfig := []session.Attachable{
authprovider.NewDockerAuthProvider(s.stderr()),
}
if len(sshKeys) > 0 || len(service.Build.SSH) > 0 {
sshAgentProvider, err := sshAgentProvider(append(service.Build.SSH, sshKeys...))
if err != nil {
return build.Options{}, err
}
sessionConfig = append(sessionConfig, sshAgentProvider)
}

return build.Options{
Inputs: build.Inputs{
ContextPath: service.Build.Context,
Expand All @@ -269,9 +271,7 @@ func (s *composeService) toBuildOptions(project *types.Project, service types.Se
Labels: service.Build.Labels,
NetworkMode: service.Build.Network,
ExtraHosts: service.Build.ExtraHosts,
Session: []session.Attachable{
authprovider.NewDockerAuthProvider(s.stderr()),
},
Session: sessionConfig,
}, nil
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/e2e/compose_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ func TestLocalComposeBuild(t *testing.T) {
})
})

t.Run("build succeed as part of up with ssh from Compose file", func(t *testing.T) {
c.RunDockerOrExitError("rmi", "build-test-ssh")

c.RunDockerComposeCmd("--project-directory", "fixtures/build-test/ssh", "up", "-d", "--build")
t.Cleanup(func() {
c.RunDockerComposeCmd("--project-directory", "fixtures/build-test/ssh", "down")
})
c.RunDockerCmd("image", "inspect", "build-test-ssh")
})

t.Run("build as part of up", func(t *testing.T) {
c.RunDockerOrExitError("rmi", "build-test_nginx")
c.RunDockerOrExitError("rmi", "custom-nginx")
Expand Down

0 comments on commit f2d9acd

Please sign in to comment.