Run runs the command logic
(ctx context.Context)
| 39 | |
| 40 | // Run runs the command logic |
| 41 | func (cmd *BuildCmd) Run(ctx context.Context) error { |
| 42 | // write workspace info |
| 43 | shouldExit, workspaceInfo, err := agent.WriteWorkspaceInfoAndDeleteOld(cmd.WorkspaceInfo, func(workspaceInfo *provider2.AgentWorkspaceInfo, log log.Logger) error { |
| 44 | return deleteWorkspace(ctx, workspaceInfo, log) |
| 45 | }, log.Default.ErrorStreamOnly()) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } else if shouldExit { |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | // make sure daemon does shut us down while we are doing things |
| 53 | agent.CreateWorkspaceBusyFile(workspaceInfo.Origin) |
| 54 | defer agent.DeleteWorkspaceBusyFile(workspaceInfo.Origin) |
| 55 | |
| 56 | // initialize the workspace |
| 57 | cancelCtx, cancel := context.WithCancel(ctx) |
| 58 | defer cancel() |
| 59 | _, logger, credentialsDir, err := initWorkspace(cancelCtx, cancel, workspaceInfo, cmd.Debug, false) |
| 60 | if err != nil { |
| 61 | return err |
| 62 | } else if credentialsDir != "" { |
| 63 | defer func() { |
| 64 | _ = os.RemoveAll(credentialsDir) |
| 65 | }() |
| 66 | } |
| 67 | |
| 68 | runner, err := CreateRunner(workspaceInfo, nil, logger) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | |
| 73 | // if there is no platform specified, we use empty to let |
| 74 | // the builder find out itself. |
| 75 | platforms := workspaceInfo.CLIOptions.Platform |
| 76 | if len(platforms) == 0 { |
| 77 | platforms = []string{""} |
| 78 | } |
| 79 | |
| 80 | // build and push images |
| 81 | for _, platform := range platforms { |
| 82 | // build the image |
| 83 | imageName, err := runner.Build(ctx, provider2.BuildOptions{ |
| 84 | CLIOptions: workspaceInfo.CLIOptions, |
| 85 | RegistryCache: workspaceInfo.RegistryCache, |
| 86 | Platform: platform, |
| 87 | ExportCache: true, |
| 88 | }) |
| 89 | if err != nil { |
| 90 | logger.Errorf("Error building image: %v", err) |
| 91 | return errors.Wrap(err, "build") |
| 92 | } |
| 93 | |
| 94 | if workspaceInfo.CLIOptions.SkipPush { |
| 95 | logger.Donef("Successfully build image %s", imageName) |
| 96 | } else { |
| 97 | logger.Donef("Successfully build and pushed image %s", imageName) |
| 98 | } |
no test coverage detected