yamlrefactoringconcourseyaml-anchors

Is it possible to achieve such a refactor in YAML


I'm working on a concourse pipeline and I need to duplicate a lot of code in my YAML so I'm trying to refactor it so it is easily maintainable and I don't end up with thousands of duplicates lines/blocks.

I have achieve the following yaml file after what seems to be the way to go but it doesn't fullfill all my needs.

add-rotm-points: &add-rotm-points
  task: add-rotm-points
  config:
    platform: linux
    image_resource:
      type: docker-image
      source:
        repository: ((registre))/polygone/concourse/cf-cli-python3
        tag: 0.0.1
        insecure_registries: [ ((registre)) ]
    run:
      path: source-pipeline/commun/rotm/trigger-rotm.sh
      args: [ "source-pipeline", "source-code-x" ]
    inputs:
      - name: source-pipeline
      - name: source-code-x

jobs:
  - name: test-a
    plan:
      - in_parallel:
          - get: source-pipeline
          - get: source-code-a
            trigger: true
      - <<: *add-rotm-points
  - name: test-b
    plan:
      - in_parallel:
          - get: source-pipeline
          - get: source-code-b
            trigger: true
      - <<: *add-rotm-points

My problem is that both my jobs uses the generic task defined at the top. But in the generic task I need to change source-code-x to the -a or -b version I use in my jobs.

I cannot find a way to achieve this without duplicating my anchor in every jobs and that seems to be counter productive. But i may not have full understood yaml anchors/merges.


Solution

  • All you need to do is map inputs on individual tasks, like this:

    add-rotm-points: &add-rotm-points
      task: add-rotm-points
      config:
        platform: linux
        image_resource:
          type: docker-image
          source:
            repository: ((registre))/polygone/concourse/cf-cli-python3
            tag: 0.0.1
            insecure_registries: [ ((registre)) ]
        run:
          path: source-pipeline/commun/rotm/trigger-rotm.sh
          args: [ "source-pipeline", "source-code-x" ]
        inputs:
          - name: source-pipeline
          - name: source-code-x
    
    jobs:
      - name: test-a
        plan:
          - in_parallel:
              - get: source-pipeline
              - get: source-code-a
                trigger: true
          - <<: *add-rotm-points
            input_mapping:
              source-code-x: source-code-a
      - name: test-b
        plan:
          - in_parallel:
              - get: source-pipeline
              - get: source-code-b
                trigger: true
          - <<: *add-rotm-points
            input_mapping:
              source-code-x: source-code-b