Skip to content

Task parameters

Another important feature of tasks is parameterization.

Take the hello-world pipeline for example.

What if the task had to be used multiple times so that it could output a different message in different contexts?

The idea is to write a single task but parameterize the inputs.

Challenge

Can you create a variant of the existing hello-world.yml pipeline with two tasks, each using the same external task definition, where one prints Hello, Texas! while the other prints Hello, California! ?

How it works

The mechanism that Concourse provides for parameterization is straightforward:

  • A task definition can define a section for parameters, optionally with a default value.

  • For each parameter defined, Concourse creates an environment variable in the container before the task runs.

  • When the task runs, the task command (or script) can reference any environment variables it wishes to.

Instructions

The Concourse examples do not provide an illustration of task parameters, so let's concoct one together:

  • Make a copy of the separate-task-config.yml pipeline as a starting point. Name it task-params.yml.
  • Retrofit the pipeline yaml to use your personal github repository in lieu of the concourse-examples resource.

  • Copy the tasks/hello-world.yml from the examples repository to your repository.

  • Modify the task to define a params section, with a parameter named MESSAGE with default value of world.

  • Retrofit the implementation of the task to reference the $MESSAGE environment variable to output its greeting.
  • Check in the new file, and push it to your remote repository on github

Continue editing the task-params.yml pipeline.

The job plan should consist of these steps:

  • Get the repository resource.
  • Delegate to the external task hello-world.yml.
  • Parameterize the call with a params section.

Hints

Below are examples of the finished pipeline and task, in case you get stuck.

Parameterized pipeline
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
---
resources:
  - name: test-repo
    type: git
    icon: github
    source:
      uri: https://github.com/eitansuez/test-repo

jobs:
  - name: job
    plan:
      - get: test-repo
      - task: hi-texas
        file: test-repo/tasks/hello-world.yml
        params:
          MESSAGE: Texas
      - task: hi-calif
        file: test-repo/tasks/hello-world.yml
        params:
          MESSAGE: California
Parameterized Task
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
---
platform: linux

image_resource:
  type: registry-image
  source: { repository: busybox }

params:
  MESSAGE: world

run:
  path: /bin/sh
  args:
  - -c
  - echo "Hello, ${MESSAGE}!"

Make it so!

Set the pipeline, trigger the job, and check the build output.

Did you manage to get the job to emit both hello messages?