func()

in builder/builder.go [253:343]


func (b *Builder) runStep(ctx context.Context, step *graph.Step, credentials []*graph.RegistryCredential) error {
	log.Printf("Executing step ID: %s. Timeout(sec): %d, Working directory: '%s', Network: '%s'\n", step.ID, step.Timeout, step.WorkingDirectory, step.Network)
	if step.StartDelay > 0 {
		log.Printf("Waiting %d seconds before executing step ID: %s\n", step.StartDelay, step.ID)
		time.Sleep(time.Duration(step.StartDelay) * time.Second)
	}

	if step.IsCmdStep() && step.Pull {
		log.Printf("Step specified pull. Performing an explicit pull...\n")
		if err := b.pullImageBeforeRun(ctx, step.Cmd, step.CmdDownloadRetries, step.CmdDownloadRetryDelayInSeconds); err != nil {
			return err
		}
	}

	step.StepStatus = graph.InProgress
	step.StartTime = time.Now()
	defer func() {
		step.EndTime = time.Now()
	}()

	var args []string

	if step.IsBuildStep() {
		dockerfile, target, dockerContext := parseDockerBuildCmd(step.Build)
		volName := b.workspaceDir

		// Print out a warning message if a remote context doesn't appear to be valid, i.e. doesn't end with .git.
		validateDockerContext(dockerContext)

		log.Println("Scanning for dependencies...")
		timeout := time.Duration(scrapeTimeoutInSec) * time.Second
		scrapeCtx, cancel := context.WithTimeout(ctx, timeout)
		defer cancel()
		deps, err := b.scrapeDependencies(scrapeCtx, volName, step.WorkingDirectory, step.ID, dockerfile, dockerContext, step.Tags, step.BuildArgs, target, credentials)
		if err != nil {
			return errors.Wrap(err, "failed to scan dependencies")
		}
		log.Println("Successfully scanned dependencies")
		step.ImageDependencies = deps

		workingDirectory := step.WorkingDirectory
		// Modify the Run command if it's a tar or a git URL.
		if !util.IsLocalContext(dockerContext) {
			// NB: use step.ID as the working directory if the context is remote,
			// since we obtained the source code from the scanner and put it in this location.
			// If the remote context also has additional context specified, we have to append it
			// to the working directory.
			if util.IsSourceControlURL(dockerContext) {
				workingDirectory = step.ID + "/" + getContextFromGitURL(dockerContext)
			} else {
				workingDirectory = step.ID
			}
			step.Build = replacePositionalContext(step.Build, ".")
		}
		step.UpdateBuildStepWithDefaults()

		if step.UseBuildCacheForBuildStep() {
			args = b.getDockerRunArgsForStep(volName, workingDirectory, step, "", buildxImg+" build "+step.Build)
		} else {
			args = b.getDockerRunArgsForStep(volName, workingDirectory, step, "", dockerImg+" build "+step.Build)
		}
	} else if step.IsPushStep() {
		timeout := time.Duration(step.Timeout) * time.Second
		pushCtx, cancel := context.WithTimeout(ctx, timeout)
		defer cancel()
		return b.pushWithRetries(pushCtx, step.Push)
	} else {
		args = b.getDockerRunArgsForStep(b.workspaceDir, step.WorkingDirectory, step, step.EntryPoint, step.Cmd)
	}

	if b.debug {
		log.Printf("Step args: %v\n", strings.Join(args, ", "))
	}

	timeout := time.Duration(step.Timeout) * time.Second
	stepCtx, cancel := context.WithTimeout(ctx, timeout)
	defer cancel()

	return b.procManager.RunRepeatWithRetries(
		stepCtx,
		args,
		nil,
		os.Stdout,
		os.Stderr,
		"",
		step.Retries,
		step.RetryOnErrors,
		step.RetryDelayInSeconds,
		step.ID,
		step.Repeat)
}