skip to Main Content

I am absolutely new to Azure Devops, but was able to create a pipeline with stages. In the config.yml file, is it also possible to indicate separate parameters for each stage?

This is a snippet of the pipeline where it uses a script inside a stage:

- stage: Configure_Iflow_in_QA
dependsOn: Copy_Iflow_to_QA_Package
jobs:
- job: Configure_Iflow_in_QA
  variables:
  - group: development
  pool:
    vmImage: 'ubuntu-latest'
  steps:
  - task: Cache@2
    inputs:
      key: piper-go-official
      path: bin 
    displayName: Configure_Iflow_in_QA
  - script: |
        bin/piper integrationArtifactUpdateConfiguration --verbose true --apiServiceKey $(CREDENTIALS) --integrationFlowId "Test_Iflow1_QA"

And here is the config file:

steps:
  integrationArtifactUpload:
    integrationFlowId: 'Test_Iflow1_QA'
    integrationFlowName: 'Test Iflow1 QA'
    packageId: 'CICD2'

  integrationArtifactUpdateConfiguration:
    integrationFlowId: 'Test_Iflow1_QA'
    integrationFlowVersion: 'Active'
    parameterKey: 'LogEnabled'
    parameterValue: 'true'

But what if I wanted to use the script integrationArtifactUpdateConfiguration again in a different stage with different parameters?

2

Answers


  1. You can specify parameters and their data types in a template, which allows you to reuse not only stages but also variables, steps, and jobs.

    Example:

    /pipelines/stages/hello-world-stage.yaml

    Defining the stages template with multiple parameters:

    parameters:
      - name: stageName
        type: string
        displayName: "Stage Name"
        default: "hello_world"
      
      - name: stageDisplayName
        type: string
        displayName: "Stage Display Name"
        default: "Hello World"
    
      - name: name
        type: string
        displayName: "Name"
    
    stages:
    - stage: ${{ parameters.stageName }}
      displayName: ${{ parameters.stageDisplayName }}
      jobs:
      - job: hello
        displayName: Hello ${{ parameters.name }}
        steps:
        - script: |
            echo "Creating dummy artifact..."
            echo "Hello, ${{ parameters.name }} !" 
          displayName: "Hello ${{ parameters.name }}"
    

    hello-world-pipeline.yaml

    Using the stages template:

    parameters:
      - name: name
        type: string
        displayName: "Name"
        default: John
    
    trigger: none
    
    pool:
      vmImage: 'ubuntu-latest'
    
    stages:
    - template: /pipelines/stages/hello-world-stage.yaml # relative or absolute path to the stage template
      parameters:
        # stageName: hello # optional
        # stageDisplayName: Hello # optional
        name: ${{ parameters.name }}
    
    Login or Signup to reply.
  2. If you have piper config.xml content and would like to use different parameters for stages, you need to:

    1. use placeholder as the value in config.xml.
    2. install extension Replace Tokens to your devops organization, then you can use task replacetokens@6 to replace the value for each stage.

    The config.xml sample:

    steps:
      integrationArtifactUpload:
        integrationFlowId: 'Test_Iflow1_QA'
        integrationFlowName: 'Test Iflow1 QA'
        packageId: 'CICD2'
    
      integrationArtifactUpdateConfiguration:
        integrationFlowId: 'Test_Iflow1_QA'
        integrationFlowVersion: 'Active'
        parameterKey: 'LogEnabled'
        parameterValue: '#{booleanvar}#'   # user placeholder for the value
    

    In the DevOps yaml:

    pool:
      vmImage: ubuntu-latest
    
    stages:
      - stage: stage1
        variables:
        - name: booleanvar                    # define same name as placehold name
          value: true                       # define the real value to replace for stage1
        jobs:
         - job: job1
           steps:
             - task: replacetokens@6
               inputs:
                 sources: '**/config.xml'
            
             - script: cat config.xml    # check the config.xml cotent
             
             - task: Cache@2
               inputs:
                 key: piper-go-official
                 path: bin 
               displayName: Configure_Iflow_in_QA
             - script: |
                  bin/piper integrationArtifactUpdateConfiguration --verbose true --apiServiceKey $(CREDENTIALS) --integrationFlowId "Test_Iflow1_QA"
      
      - stage: stage2
        variables:
        - name: booleanvar
          value: false                      # define the real value to replace for stage2
        jobs:
         - job: job2
           steps:
             - task: replacetokens@6
               inputs:
                 sources: '**/config.xml'
            
             - script: cat config.xml      # check the config.xml cotent
    
             # add your code here.
    
    

    You can add more placeholder in the config.xml if needed. use same named variable in each stage for replacement.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search