Create a reusable Argo WorkflowTemplate

argo workflows kubernetes workflowtemplate

2 min read | by Jordi Prats

Given a specific Argo Workflow, you can convert it into a reusable Argo WorkflowTemplate. This allows you to reference the template across multiple workflows without repeating it's YAML definition.

We are going to use the Argo Workflow that we've used in a previous post to build and push container images using Kaniko. The yaml definition for the Argo WorkflowTemplate is as follows:

apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: build-and-push-image
spec:
  entrypoint: kaniko-build

  volumes:
    - name: kaniko-secret
      secret:
        secretName: dockerhub-registry
        items:
        - key: .dockerconfigjson
          path: config.json

  templates:
    - name: kaniko-build
      container:
        image: gcr.io/kaniko-project/executor:latest
        args:
          - --dockerfile=Dockerfile
          - --context=git://github.com/jordiprats/django-ampa.git#refs/heads/main
          - --destination=jordiprats/ampa:kaniko
        volumeMounts:
        - name: kaniko-secret
          mountPath: "/kaniko/.docker"

It's important to note the following key changes:

  • kind: Obviously, we've changed from Workflow to WorkflowTemplate to make it reusable.
  • metadata.name: The generateName has been replaced with name, as workflow templates require a fixed name that we'll reference later on the workflows.
$ kubectl apply -f kaniko-template.yaml
workflowtemplate.argoproj.io/build-and-push-image created

To create an actual Workflow from this template, you can reference it as follows:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: build-and-push-image-
spec:
  workflowTemplateRef:
    name: build-and-push-image

This way, you reuse the kaniko-build logic without needing to repeat it each time you need to run the same build. We can trigger the workflow as usual:

$ kubectl create -f kaniko-template-instance.yaml ; kubectl get workflow -w
workflow.argoproj.io/build-and-push-image-rvkkf created
NAME                         STATUS      AGE   MESSAGE
build-and-push-image-rvkkf   Running     0s
build-and-push-image-rvkkf   Running     10s
build-and-push-image-rvkkf   Succeeded   58s

Posted on 25/10/2024