0

I have a pipeline template in Azure DevOps which are used by many other pipelines. What I wanted to do is: based on a parameter pass by other pipelines, my template has to decide which k8s cluster the application deploy to.

From my below example, I wanted to find out the stageName from a predefined variable $(System.StageName) and based on the stage name I want to grab the k8s cluster details from the YAML parameter define with parameter name clusters, after that from the clusters object I have to choose the cluster name based on one more parameter called deployTo

pipeline/cd.yaml

parameters:
- name: stageName
  type: string
  default: $(System.StageName)
- name: deployTo
  type: string
  default: all
- name: clusters
  type: object
  default:
    test:
        all:
        - test1.cluster.net
        - test2.cluster.net
        test1: test1.cluster.net
        test2: test2.cluster.net
    prod:
        all:
        - prod1.cluster.net
        - prod2.cluster.net
        prod1: test1.cluster.net
        prod2: test2.cluster.net

jobs:
- ${{ if eq('parameters.deployTo','all') }}:
  - ${{ each clsuter in parameters.clusters[parameters.stageName][parameters.deployTo] }}:
    - job: job_all
      steps:
      - bash:|
          echo do_something_with_${{ cluster }}

- ${{ if ne('parameters.deployTo','all') }}:
  - job: job_not_all
    steps:
    - bash: |
        echo do_something_with ${{ parameters.clusters[parameters.stageName][parameters.deployTo] }}

azure-pipeline.yaml

trigger:
- main

pool:
  vmImage: ubuntu-latest

stages:
- stage: test
  jobs:
  - template: pipeline/cd.yaml

- stage: prod
  jobs:
  - template: pipeline/cd.yaml
    parameters:
      deployTo: 'prod1'

From the above example:

  1. I don't seem to fetch my expected value dynamically parameters. clusters[parameters.stageName][parameters.deployTo] - Is there any other way I can be able to fetch the value dynamically?

    If I just hardcode the value like parameters.clusters.test.all[0] or parameters.clusters.test.test1 - It just work fine.

  2. Wondering why this condition ${{ if eq('parameters.deployTo','all') }} always get evaluated FALSE ?

Samit Kumar Patel
  • 1,932
  • 1
  • 13
  • 20
  • 1
    in 2), it should be `${{ if eq(parameters.deployTo,'all') }}` (by wrapping parameters.deployTo in quotes you are literally comparing string "parameters.deployTo" with string "all"). – qbik Jun 21 '21 at 08:41

0 Answers0