Argo Workflows: Schedule workflows with CronWorkflow

argo workflows kubernetes cronworkflow schedule cron

2 min read | by Jordi Prats

With Argo workflows you can automate tasks like building and pushing Docker images on a regular schedule: You just need to use the CronWorkflow to call any WorkflowTemplate.

In this example we are going to set up an Argo CronWorkflow that runs every hour, clones a Git repository, builds a Docker image using Kaniko, and pushes it to a Docker registry.

Assuming that we already have the WorkflowTemplate that will clone, build and push, we can create a CronWorkflow that will call this template every hour:

apiVersion: argoproj.io/v1alpha1
kind: CronWorkflow
metadata:
  name: hourly-build-and-push-image
spec:
  schedule: "0 * * * *"
  concurrencyPolicy: "Allow"
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 1
  workflowSpec:
    workflowTemplateRef:
      name: build-and-push-image-multistep
    arguments:
      parameters:
        - name: git-repo-url
          value: "git@github.com:jordiprats/flask-pet2cattle.git"
        - name: branch
          value: "master"
        - name: image-destination
          value: "jordiprats/ampa:kaniko"

If we apply this CronWorkflow to the Kubernetes cluster, we won't see any Workflow running until the schedule is met:

$ kubectl apply -f cron.yaml
cronworkflow.argoproj.io/hourly-build-and-push-image created
$ kubectl get cronworkflow
NAME                          AGE
hourly-build-and-push-image   7s
$ kubectl get workflow
No resources found in argo namespace.

As soon as the schedule is met, the CronWorkflow will trigger the WorkflowTemplate:

$ kubectl get workflow
NAME                                     STATUS      AGE   MESSAGE
hourly-build-and-push-image-1729692000   Succeeded   84s

In the status of the CronWorkflow we'll be able to check when was the last time it was triggered:

$ kubectl get cronworkflow hourly-build-and-push-image  -o yaml
apiVersion: argoproj.io/v1alpha1
kind: CronWorkflow
metadata:
  (...)
spec:
  concurrencyPolicy: Allow
  failedJobsHistoryLimit: 1
  schedule: 0 * * * *
  successfulJobsHistoryLimit: 3
  workflowSpec:
    arguments:
      parameters:
      - name: git-repo-url
        value: git@github.com:jordiprats/flask-pet2cattle.git
      - name: branch
        value: master
      - name: image-destination
        value: jordiprats/ampa:kaniko
    workflowTemplateRef:
      name: build-and-push-image-multistep
status:
  lastScheduledTime: "2024-10-23T14:00:00Z"

Posted on 06/11/2024