To reduce duplicates I want to rewrite our Azure DevOps pipelines. The main pipelines looks like:
parameters:
- name: MODULE_Foo
type: boolean
default: false
- name: MODULE_Bar
type: boolean
default: false
...
- name: MODULE_X
type: boolean
default: false
...rest of parameters
- name: BRANCH_NAME
type: string
- name: CHECKOUT_TAG
type: boolean
default: false
extends:
template: template1.yml
parameters:
MODULES: ${{paramaters}}
BRANCH_NAME: ${{parameters.BRANCH_NAME}}
...rest of parameters
The template template1.yml:
parameters:
- name: MODULES
type: object
default: {}
...rest of parameters
stages:
- stage: Build
jobs:
- job: BuildAndDeploy
steps:
- bash: |
MODULES=$(echo "${{ convertToJson(parameters.MODULES) }}" \
| sed -E 's/^([[:space:]]+)/\1"/;s/(:[[:space:]]+)/"\1"/;s/,$/",/;s/([^{},])$/\1"/' \
| jq 'with_entries( select(.key | startswith("MODULE_") ) )' \
)
echo "##vso[task.setvariable variable=MODULES;]$MODULES"
- template: template2.yml
parameters:
MODULES: $(MODULES)
...rest of parameters
And finally template2.yml:
parameters:
- name: MODULES
type: object
default: {}
...rest of parameters
- ${{ each module in parameters.MODULES }}:
- ${{ if and(startsWith(module.Key, 'module'), eq(module.Value, true)) }}:
- bash: |
echo "Module to build: ${{module.key}}, Value: ${{module.value}}"
Now I'm stuck.
How to pass a variable as parameter to a template? To the template is passed empty string '' for ${{variables.MODULES}} or '$(MODULES)' for $(MODULES). But never generated string. I think that $(MODULES) is valid for this situation.
How to create a valid object, which can be iterated inside each loop in template? I think that my construction with convertToJson, sed, and jq is not correct.
My goal is to eliminate the MODULE_ parameter group from the templates. Currently, every template contains a duplicate of this parameter section.