Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concourse call job from another job with parameters

Tags:

concourse

I have a job with many tasks like this:

- name: main-job
  serial: true
  plan:
  - aggregate:
    - get: <git-resource>
      passed: [previous-job]
      trigger: true
    - get: <git-resource-3>
  - task: <task-1>
    file: <git-resource>/<path>/<task-1-no-db>.yml
  - task: <task-2>
    tags: ['<specific-tag>']
    file: <git-resource>/<path>/<task-1>.yml
    params:
      DATABASE_HOST: <file>
      DATABASE: <my-db-1>
  - task: <task-2>
    tags: ['<specific-tag>']
    file: <git-resource>/<path>/<task-1>.yml
    params:
      DATABASE_HOST: <file>
      DATABASE: <my-db-1>

The problem for me is, I have to literally call the same job but instead of DATABASE params being my-db-1, I want it to be my-db-2.

The only way I am able to do this is by having new job and pass the params, literally copy the entire set of lines. My job is too fat, as in has too many tasks in them, so copying it though is the obvious solution, I am wondering if there's a way to re-use by having multiple pipelines and one main pipeline that essentially calls these pipelines with the param for DATABASE passed or have two small jobs that calls this main job with different params something like this:

- name: <call-main-job-with-db-1>
  serial: true
  plan:
  - aggregate:
    - get: <git-resource>
      passed: [previous-job]
      trigger: true
  - task: <call-main-job-task>
    params:
      DATABASE_HOST: <file>
      DATABASE: <my-db-1>



- name: <call-main-job-with-db-2>
  serial: true
  plan:
  - aggregate:
    - get: <git-resource>
      passed: [previous-job]
      trigger: true
  - task: <call-main-job-task>
    params:
      DATABASE: <my-db-2>

I am not sure if this is even possible since I didn't find any example of this.

like image 666
Pavanraotk Avatar asked Mar 08 '23 07:03

Pavanraotk


2 Answers

Remember you are using YAML, so you can use YAML features like "Anchors"

You will find some additional information about "Anchors" in this link. Look for "EXTRA YAML FEATURES"

YAML also has a handy feature called 'anchors', which let you easily duplicate content across your document. Both of these keys will have the same value: anchored_content: &anchor_name This string will appear as the value of two keys. other_anchor: *anchor_name

# Anchors can be used to duplicate/inherit properties
base: &base
    name: Everyone has same name

foo: &foo
    <<: *base
    age: 10

bar: &bar
    <<: *base
    age: 20

Try this for your Concourse Pipeline:

common:
  db_common: &db_common
    serial: true
    plan:
    - aggregate:
        - get: <git-resource>
        passed: [previous-job]
        trigger: true
    - task: <call-main-job-task>
        params:

jobs:
- name: <call-main-job-with-db-1>
  <<: *db_common
      DATABASE_HOST: <file>
      DATABASE: <my-db-1>

- name: <call-main-job-with-db-2>
  <<: *db_common
      DATABASE: <my-db-2>

NOTE: Remember that you can have as many Anchors as you want, you can define two or more anchors for the same Job/Task/Resource, etc.

like image 118
Rolo Avatar answered May 16 '23 08:05

Rolo


You need to just copy and paste the task as you do in the question description. Concourse expects an expressive yaml, there is no branching or logic allowed. If you don't want to copy and paste so much yaml, then you can do some yaml generation magic to simplify what you look at and work with, but concourse will want the full yaml with each job defined separately.

like image 40
Josh Zarrabi Avatar answered May 16 '23 07:05

Josh Zarrabi