func createBuildTask()

in cmd/acb/commands/build/build.go [259:364]


func createBuildTask(
	ctx gocontext.Context,
	isolation string,
	pull bool,
	labels []string,
	noCache bool,
	dockerfile string,
	tags []string,
	buildArgs []string,
	secretBuildArgs []string,
	target string,
	platform string,
	buildContext string,
	renderOpts *templating.BaseRenderOptions,
	debug bool,
	registry string,
	push bool,
	creds []string,
	workingDirectory string,
) (*graph.Task, error) {
	// Create the run command to be used in the template
	args := []string{}
	if isolation != "" {
		args = append(args, fmt.Sprintf("--isolation=%s", isolation))
	}
	if pull {
		args = append(args, "--pull")
	}
	for _, label := range labels {
		args = append(args, "--label", label)
	}
	if noCache {
		args = append(args, "--no-cache")
	}
	if dockerfile != "" {
		args = append(args, "-f", dockerfile)
	}
	for _, tag := range tags {
		args = append(args, "-t", tag)
	}
	for _, buildArg := range buildArgs {
		args = append(args, "--build-arg", buildArg)
	}
	for _, secretBuildArg := range secretBuildArgs {
		args = append(args, "--build-arg", secretBuildArg)
	}
	if target != "" {
		args = append(args, "--target", target)
	}
	if platform != "" {
		args = append(args, "--platform", platform)
	}
	args = append(args, buildContext)
	runCmd := strings.Join(args, " ")

	// Create the template
	template := templating.NewTemplate("build", []byte(runCmd))

	rendered, err := templating.LoadAndRenderBuildSteps(ctx, template, renderOpts)
	if err != nil {
		return nil, err
	}

	if debug {
		log.Println("Rendered template:")
		log.Println(rendered)
	}

	var credentials []*graph.RegistryCredential
	allKnownRegistries := []string{registry}
	for _, credString := range creds {
		cred, err := graph.CreateRegistryCredentialFromString(credString)
		if err != nil {
			return nil, err
		}
		credentials = append(credentials, cred)
		allKnownRegistries = append(allKnownRegistries, cred.Registry)
	}

	// After the template has rendered, we have to parse the tags again
	// so we can properly set the build/push tags.
	rendered, prefixedTags := util.PrefixTags(rendered, registry, allKnownRegistries)
	tags = prefixedTags

	buildStep := &graph.Step{
		ID:      "build",
		Build:   rendered,
		Timeout: buildTimeoutInSec,
		Tags:    tags,
	}

	steps := []*graph.Step{buildStep}

	if push {
		pushStep := &graph.Step{
			ID:      "push",
			Push:    tags,
			Timeout: pushTimeoutInSec,
			When:    []string{buildStep.ID},
		}

		steps = append(steps, pushStep)
	}

	return graph.NewTask(ctx, steps, []*secretmgmt.Secret{}, registry, credentials, true, workingDirectory, "")
}