Просмотр исходного кода

Pull docker image before pushing (#4173)

Stefan McShane 2 лет назад
Родитель
Сommit
1c3c2130ea
5 измененных файлов с 36 добавлено и 13 удалено
  1. 4 0
      cli/cmd/commands/apply.go
  2. 11 1
      cli/cmd/docker/builder.go
  3. 2 0
      cli/cmd/v2/apply.go
  4. 3 0
      cli/cmd/v2/build.go
  5. 16 12
      cli/cmd/v2/update.go

+ 4 - 0
cli/cmd/commands/apply.go

@@ -43,6 +43,8 @@ var (
 	porterYAML       string
 	previewApply     bool
 	imageTagOverride string
+	// pullImageBeforeBuild is a flag that determines whether to pull the docker image from a repo before building
+	pullImageBeforeBuild bool
 )
 
 func registerCommand_Apply(cliConf config.CLIConfig) *cobra.Command {
@@ -108,6 +110,7 @@ applying a configuration:
 
 	applyCmd.PersistentFlags().StringVarP(&porterYAML, "file", "f", "", "path to porter.yaml")
 	applyCmd.PersistentFlags().BoolVarP(&previewApply, "preview", "p", false, "apply as preview environment based on current git branch")
+	applyCmd.PersistentFlags().BoolVar(&pullImageBeforeBuild, "pull-before-build", false, "attempt to pull image from registry before building")
 	applyCmd.PersistentFlags().StringVar(&imageTagOverride, "tag", "", "set the image tag used for the application (overrides field in yaml)")
 	applyCmd.PersistentFlags().BoolVarP(
 		&appWait,
@@ -152,6 +155,7 @@ func apply(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client ap
 			ImageTagOverride:            imageTagOverride,
 			PreviewApply:                previewApply,
 			WaitForSuccessfulDeployment: appWait,
+			PullImageBeforeBuild:        pullImageBeforeBuild,
 		}
 		err := v2.Apply(ctx, inp)
 		if err != nil {

+ 11 - 1
cli/cmd/docker/builder.go

@@ -8,6 +8,7 @@ import (
 	"fmt"
 	"io"
 	"io/ioutil"
+	"log"
 	"os"
 	"os/exec"
 	"path/filepath"
@@ -37,8 +38,17 @@ type BuildOpts struct {
 	LogFile *os.File
 }
 
-// BuildLocal
+// BuildLocal builds a Dockerfile using the local Docker daemon
 func (a *Agent) BuildLocal(ctx context.Context, opts *BuildOpts) (err error) {
+	if opts == nil {
+		return errors.New("build opts cannot be nil")
+	}
+	if opts.UseCache {
+		err = a.PullImage(ctx, fmt.Sprintf("%s:%s", opts.ImageRepo, opts.CurrentTag))
+		if err != nil {
+			log.Printf("unable to pull image. Continuing with build: %s", err.Error())
+		}
+	}
 	if os.Getenv("DOCKER_BUILDKIT") == "1" {
 		return buildLocalWithBuildkit(ctx, *opts)
 	}

+ 2 - 0
cli/cmd/v2/apply.go

@@ -39,6 +39,8 @@ type ApplyInput struct {
 	PreviewApply bool
 	// WaitForSuccessfulDeployment is true when Apply should wait for the update to complete before returning
 	WaitForSuccessfulDeployment bool
+	// PullImageBeforeBuild will attempt to pull the image before building if true
+	PullImageBeforeBuild bool
 }
 
 // Apply implements the functionality of the `porter apply` command for validate apply v2 projects

+ 3 - 0
cli/cmd/v2/build.go

@@ -38,6 +38,8 @@ type buildInput struct {
 	// CurrentImageTag is used in docker build to cache from
 	CurrentImageTag string
 	RepositoryURL   string
+	// PullImageBeforeBuild is used to pull the docker image before building
+	PullImageBeforeBuild bool
 
 	Env map[string]string
 }
@@ -112,6 +114,7 @@ func build(ctx context.Context, client api.Client, inp buildInput) buildOutput {
 			IsDockerfileInCtx: isDockerfileInCtx,
 			Env:               inp.Env,
 			LogFile:           logFile,
+			UseCache:          inp.PullImageBeforeBuild,
 		}
 
 		err = dockerAgent.BuildLocal(

+ 16 - 12
cli/cmd/v2/update.go

@@ -37,6 +37,8 @@ type UpdateInput struct {
 	PreviewApply bool
 	// WaitForSuccessfulDeployment is true when Update should wait for the revision deployment to complete (all services deployed successfully)
 	WaitForSuccessfulDeployment bool
+	// PullImageBeforeBuild will attempt to pull the image before building if true
+	PullImageBeforeBuild bool
 }
 
 // Update implements the functionality of the `porter apply` command for validate apply v2 projects
@@ -170,12 +172,13 @@ func Update(ctx context.Context, inp UpdateInput) error {
 		color.New(color.FgGreen).Printf("Building new image with tag %s...\n", commitSHA) // nolint:errcheck,gosec
 
 		buildInput, err := buildInputFromBuildSettings(buildInputFromBuildSettingsInput{
-			projectID: cliConf.Project,
-			appName:   appName,
-			commitSHA: commitSHA,
-			image:     buildSettings.Image,
-			build:     buildSettings.Build,
-			buildEnv:  buildSettings.BuildEnvVariables,
+			projectID:            cliConf.Project,
+			appName:              appName,
+			commitSHA:            commitSHA,
+			image:                buildSettings.Image,
+			build:                buildSettings.Build,
+			buildEnv:             buildSettings.BuildEnvVariables,
+			pullImageBeforeBuild: inp.PullImageBeforeBuild,
 		})
 		if err != nil {
 			buildError = fmt.Errorf("error creating build input from build settings: %w", err)
@@ -302,12 +305,13 @@ func gitSourceFromEnv() (porter_app.GitSource, error) {
 }
 
 type buildInputFromBuildSettingsInput struct {
-	projectID uint
-	appName   string
-	commitSHA string
-	image     porter_app.Image
-	build     porter_app.BuildSettings
-	buildEnv  map[string]string
+	projectID            uint
+	appName              string
+	commitSHA            string
+	image                porter_app.Image
+	build                porter_app.BuildSettings
+	buildEnv             map[string]string
+	pullImageBeforeBuild bool
 }
 
 func buildInputFromBuildSettings(inp buildInputFromBuildSettingsInput) (buildInput, error) {