Add custom tasks to a build

When you build applications on Tanzu Platform for Kubernetes, you can use ContainerAppBuildPlan configurations to define the processes or tasks that run when you run tanzu build. Tanzu Platform provides a default ContainerAppBuildPlan named simple.tanzu.vmware.com, which you can use as a reference to create a custom ContainerAppBuildPlan for a specific Project. When you create a ContainerAppBuildPlan for a Project, it is available to all Spaces in that Project.

For information about Projects, see Add projects and assign users for Tanzu Platform hub. For information about ContainerApps, see What is a ContainerApp, and the ContainerAppBuildPlan API reference for the full ContainerAppBuildPlan specification.

Before you begin

  • Install Tanzu CLI v1.4.0 or later. See Installing the Tanzu CLI in the Tanzu CLI documentation.

  • If it is not present, install the Tanzu CLI app-developer plug-in group by running:

    tanzu plugin install --group vmware-tanzu/app-developer
    

    If the app-developer plug-in group is already installed, verify that you have the latest version of the build plug-in by running:

    tanzu plugin upgrade build
    

Create a ContainerAppBuildPlan file

You create a ContainerAppBuildPlan file by obtaining the default plan from a Project, saving, and updating it.

  1. Log in to Tanzu Platform with the Tanzu CLI:

    tanzu login
    
  2. Set the Tanzu CLI to your Project context:

    tanzu project use [project_name]
    
  3. Set KUBECONFIG:

    export KUBECONFIG=~/.config/tanzu/kube/config
    
  4. Retrieve the default ContainerAppBuildPlan from the Project:

    kubectl get containerappbuildplans.build.tanzu.vmware.com simple.tanzu.vmware.com -oyaml
    
  5. Save the test.tanzu.vmware.com ContainerAppBuildPlan as a YAML file and add your customizations.

    For example, save it as my-custom-conatinerappbuildplan.yaml.

    This example shows the default ContainerAppBuildPlan in YAML format.

    apiVersion: build.tanzu.vmware.com/v1
    kind: ContainerAppBuildPlan
    metadata:
      annotations:
        provided.tanzu.vmware.com: ""
      creationTimestamp: "2024-09-23T16:06:28Z"
      generation: 1
      name: simple.tanzu.vmware.com
      namespace: default
      resourceVersion: "48312055"
      uid: 0f913514-97ad-436e-a553-288b3bd263d7
    spec:
      buildpacks:
        builder: tanzu-build-public.packages.broadcom.com/builders/builder-jammy-base:0.1.2@sha256:2370d9e499afb6e0b2fb0d3c4533d733a47f771eb0a115c4f9aa75cd081ee072
      postBuildSteps:
      - namedTask: tanzu-spring-boot
      registry: "" # deprecated
      runtimes:
      - description: create a carvel package for deploying on kubernetes
        name: kubernetes-carvel-package
        steps:
        - namedTask: tanzu-kubernetes-deployment
        - namedTask: tanzu-kubernetes-carvel-package
      - description: create kubernetes yaml files for deploying on kubernetes
        name: kubernetes-plain
        steps:
        - namedTask: tanzu-kubernetes-deployment
      - description: write image to file for use with container runtime (i.e docker run)
        name: container-image
        steps:
        - namedTask: tanzu-container-image
    

    See Example: Add a container task to a ContainerAppBuildPlan below for an example of how to update a ContainerAppBuildPlan to add a ContainerTask.

  6. Use kubectl to create the custom ContainerAppBuildPlan in your Project.

    kubectl apply -f my-custom-conatinerappbuildplan.yaml
    
  7. Run tanzu build to build your application with the updated ContainerAppBuildPlan.

    tanzu build
    

Example: Add a container task to a ContainerAppBuildPlan

The following example shows a custom ContainerAppBuildPlan named test.tanzu.vmware.com, that adds a ContainerTask to the plan. A ContainerTask runs a one-off task in the post-build step of a build plan, using a container image and a set of instructions.

apiVersion: build.tanzu.vmware.com/v1
kind: ContainerAppBuildPlan
metadata:
  name: test.tanzu.vmware.com
spec:
  buildpacks:
    builder: tanzu-build-public.packages.broadcom.com/builders/builder-jammy-base:0.1.2@sha256:2370d9e499afb6e0b2fb0d3c4533d733a47f771eb0a115c4f9aa75cd081ee072
  postBuildSteps:
  - name: add-env
    containerTask:
      image: my-resgistry.io/container-app-modifier
      command: ["/cnb/process/container-app-modifier"]
      args: ["some-env-name=some-env-value"]
  registry: ""
  runtimes:
  - description: create a carvel package for deploying on kubernetes
    name: kubernetes-carvel-package
    steps:
    - namedTask: tanzu-kubernetes-deployment
    - namedTask: tanzu-kubernetes-carvel-package
  - description: create kubernetes yaml files for deploying on kubernetes
    name: kubernetes-plain
    steps:
    - namedTask: tanzu-kubernetes-deployment
  - description: write image to file for use with container runtime (i.e docker run)
    name: container-image
    steps:
    - namedTask: tanzu-container-image

This example custom ContainerAppBuildPlan replaces the NamedTask tanzu-spring-boot in spec.postBuildSteps with the ContainerTask object add-env, as highlighted in the extract below.

postBuildSteps:
- name: add-env
      containerTask:
        image: my-resgistry.io/container-app-modifier
        command: ["/cnb/process/container-app-modifier"]
        args: ["some-env-name=some-env-value"]

Running tanzu build with this ContainerAppBuildPlan runs the new add-env ContainerTask after the build steps instead running of the tanzu-spring-boot NamedTask.

In addition, ContainerAppBuildPlan also allows you to customize spec.runtimes to customize the steps to run during the build. See the ContainerAppBuildPlan API reference for the full ContainerAppBuildPlan specification.

check-circle-line exclamation-circle-line close-line
Scroll to top icon