Reusable Tasks (YAML)

Re-use top-level tasks for workflow items to help cut down repetition

When working with workflows, you may find that you have repetitive tasks that you wish to re-use. To cut down on repeating tasks, Sail CI supports using the "from:" property against a workflow item.

Let's take the following task as an example. The top-level task "build-push" builds and pushes a container as part of a monorepo using Kaniko.

Example Project Structure

packages/package-1/index.js
packages/package-2/index.js
README.md

.sail.yml

tasks:
build-push:
image: gcr.io/kaniko-project/executor
args:
- --dockerfile=packages/$(PACKAGE)/Dockerfile
- --context=/workspace/repo
- --destination=$(PACKAGE):$(SHORT_SHA)
env:
- SHORT_SHA: $(SAIL_COMMIT_SHORT_SHA)
workflow:
build-push-package-1:
from: "build-push"
env:
- PACKAGE: "package-1"
build-push-package-2:
from: "build-push"
env:
- PACKAGE: "package-2"

Once specifying a name for the workflow item and the "from:" property you can then declare task properties. The behaviour of each property, when declared on a workflow item, as defined below:

  • image - replaces the original value
  • cwd - replaces the original value
  • command - replaces the original value
  • args - replaces the original value
  • env - merges values together
  • mounts - merges values together
  • when - replaces the original value

The full schema for a workflow item:

workflow:
build-push-package-1:
from: "build-push"
image: "replace/image"
command: ["replace", "command"]
cwd: "./bin"
args: ["replace", "args"]
env: ["MERGE": "ENV_VARS"]
mounts: ["/path": "MERGE_MOUNTS"]
when:
ref: "\/heads\/master$"
paths:
- packages/package-1/**/*

The example above shows assigning the PACKAGE env, each time the "build-push" task runs $(PACKAGE) get replaced with the value. Saving repetition of top-level tasks for each package (e.g. "build-push-package-1", "build-push-package-2").

Note: You must specify "from:" if you name a task in your workflow that is not declared at the top-level within "tasks:".

As you can see you now have full flexibility for declaring base properties on top-level tasks that can be re-used in a workflow.