@@ -63,7 +63,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
6363 if service .Build != nil {
6464 imageName := getImageName (service , project .Name )
6565 imagesToBuild = append (imagesToBuild , imageName )
66- buildOptions , err := s .toBuildOptions (project , service , imageName )
66+ buildOptions , err := s .toBuildOptions (project , service , imageName , options . SSHs )
6767 if err != nil {
6868 return err
6969 }
@@ -81,15 +81,6 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
8181 Attrs : map [string ]string {"ref" : image },
8282 })
8383 }
84-
85- if len (options .SSHs ) > 0 || len (service .Build .SSH ) > 0 {
86- sshAgentProvider , err := sshAgentProvider (append (service .Build .SSH , options .SSHs ... ))
87- if err != nil {
88- return err
89- }
90- buildOptions .Session = append (buildOptions .Session , sshAgentProvider )
91- }
92-
9384 opts [imageName ] = buildOptions
9485 }
9586 }
@@ -168,7 +159,7 @@ func (s *composeService) getBuildOptions(project *types.Project, images map[stri
168159 if localImagePresent && service .PullPolicy != types .PullPolicyBuild {
169160 continue
170161 }
171- opt , err := s .toBuildOptions (project , service , imageName )
162+ opt , err := s .toBuildOptions (project , service , imageName , []types. SSHKey {} )
172163 if err != nil {
173164 return nil , err
174165 }
@@ -218,7 +209,7 @@ func (s *composeService) doBuild(ctx context.Context, project *types.Project, op
218209 return s .doBuildBuildkit (ctx , project , opts , mode )
219210}
220211
221- func (s * composeService ) toBuildOptions (project * types.Project , service types.ServiceConfig , imageTag string ) (build.Options , error ) {
212+ func (s * composeService ) toBuildOptions (project * types.Project , service types.ServiceConfig , imageTag string , sshKeys []types. SSHKey ) (build.Options , error ) {
222213 var tags []string
223214 tags = append (tags , imageTag )
224215
@@ -252,6 +243,17 @@ func (s *composeService) toBuildOptions(project *types.Project, service types.Se
252243 return build.Options {}, err
253244 }
254245
246+ sessionConfig := []session.Attachable {
247+ authprovider .NewDockerAuthProvider (s .stderr ()),
248+ }
249+ if len (sshKeys ) > 0 || len (service .Build .SSH ) > 0 {
250+ sshAgentProvider , err := sshAgentProvider (append (service .Build .SSH , sshKeys ... ))
251+ if err != nil {
252+ return build.Options {}, err
253+ }
254+ sessionConfig = append (sessionConfig , sshAgentProvider )
255+ }
256+
255257 return build.Options {
256258 Inputs : build.Inputs {
257259 ContextPath : service .Build .Context ,
@@ -269,9 +271,7 @@ func (s *composeService) toBuildOptions(project *types.Project, service types.Se
269271 Labels : service .Build .Labels ,
270272 NetworkMode : service .Build .Network ,
271273 ExtraHosts : service .Build .ExtraHosts ,
272- Session : []session.Attachable {
273- authprovider .NewDockerAuthProvider (s .stderr ()),
274- },
274+ Session : sessionConfig ,
275275 }, nil
276276}
277277
0 commit comments