NewDockerBuildExecutor function to create a run executor for the container
(input NewDockerBuildExecutorInput)
| 21 | |
| 22 | // NewDockerBuildExecutor function to create a run executor for the container |
| 23 | func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor { |
| 24 | return func(ctx context.Context) error { |
| 25 | logger := common.Logger(ctx) |
| 26 | if input.Platform != "" { |
| 27 | logger.Infof("%sdocker build -t %s --platform %s %s", logPrefix, input.ImageTag, input.Platform, input.ContextDir) |
| 28 | } else { |
| 29 | logger.Infof("%sdocker build -t %s %s", logPrefix, input.ImageTag, input.ContextDir) |
| 30 | } |
| 31 | if common.Dryrun(ctx) { |
| 32 | return nil |
| 33 | } |
| 34 | |
| 35 | cli, err := GetDockerClient(ctx) |
| 36 | if err != nil { |
| 37 | return err |
| 38 | } |
| 39 | defer cli.Close() |
| 40 | |
| 41 | logger.Debugf("Building image from '%v'", input.ContextDir) |
| 42 | |
| 43 | tags := []string{input.ImageTag} |
| 44 | options := client.ImageBuildOptions{ |
| 45 | Tags: tags, |
| 46 | Remove: true, |
| 47 | AuthConfigs: LoadDockerAuthConfigs(ctx), |
| 48 | Dockerfile: input.Dockerfile, |
| 49 | } |
| 50 | if input.Platform != "" { |
| 51 | parts := strings.SplitN(input.Platform, "/", 2) |
| 52 | if len(parts) == 2 { |
| 53 | options.Platforms = []specs.Platform{{OS: parts[0], Architecture: parts[1]}} |
| 54 | } |
| 55 | } |
| 56 | var buildContext io.ReadCloser |
| 57 | if input.BuildContext != nil { |
| 58 | buildContext = io.NopCloser(input.BuildContext) |
| 59 | } else { |
| 60 | buildContext, err = createBuildContext(ctx, input.ContextDir, input.Dockerfile) |
| 61 | } |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | |
| 66 | defer buildContext.Close() |
| 67 | |
| 68 | logger.Debugf("Creating image from context dir '%s' with tag '%s' and platform '%s'", input.ContextDir, input.ImageTag, input.Platform) |
| 69 | resp, err := cli.ImageBuild(ctx, buildContext, options) |
| 70 | |
| 71 | err = logDockerResponse(logger, resp.Body, err != nil) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | return nil |
| 76 | } |
| 77 | } |
| 78 | func createBuildContext(ctx context.Context, contextDir string, relDockerfile string) (io.ReadCloser, error) { |
| 79 | common.Logger(ctx).Debugf("Creating archive for build context dir '%s' with relative dockerfile '%s'", contextDir, relDockerfile) |
| 80 |
no test coverage detected
searching dependent graphs…