Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting parameter value dynamically for automatic pipelines

If I create a parameter, I can set its value when I run a pipeline manually. But when the pipeline runs automatically, it uses the default value. When a pipeline runs automatically (say in response to pushing to a repo) is there any way to pass it the value of a parameter?

This is the yaml file I was playing around with. The goal is to be able to control which tests get run in the pipeline.

parameters:
  - name: testFiles
    type: string
    default: cypress/integration/first.spec.js

trigger:
  - azure-test

pool:
  vmImage: ubuntu-latest

steps:
  - task: NodeTool@0
    inputs:
      versionSpec: "10.x"
    displayName: "Install Node.js"

  - script: npm install
    displayName: "npm install"

  - script: npx cypress run --record --key [record key removed] --spec ${{ parameters.testFiles }}
    displayName: "run cypress"
like image 836
Adnan Zaman Avatar asked Jun 28 '26 12:06

Adnan Zaman


1 Answers

When a pipeline runs automatically (say in response to pushing to a repo) is there any way to pass it the value of a parameter?

When running the automatical pipeline, it will only use the default parameter values.

So we can achieve this requirement by changing the default value of parameters.

Based on my test, I have found a convenient method: you could use If expression to check the trigger method (manually or CI). Then you could set the value of the parameters.

Note: We cannot use if expressions when defining parameters, so we need to use variables to pass values.

You could refer to this ticket.

Here is my example:

trigger:
- master


variables:
   ${{ if eq( variables['Build.Reason'], 'IndividualCI' ) }}: 
    buildVersion: $(BUILD.SOURCEVERSIONMESSAGE)
   ${{ if ne( variables['Build.Reason'], 'IndividualCI' ) }}: 
    buildVersion: cypress/integration/first.spec.js

parameters:
  - name: testFiles
    type: string  
    default: $(buildVersion)


pool:
  vmImage: ubuntu-latest

steps:
- script: echo ${{ parameters.testFiles }}
  displayName: 'Run a one-line script'

The variable $(Build.Reason) is used to confirm the trigger method.

The variable $(BUILD.SOURCEVERSIONMESSAGE) contains the message of the commit.

Here are the steps:

  1. When you push the changes in a repo, you need to add a comment. The comment is the testfile path.

enter image description here

  1. The CI triggered pipeline will get this comment and set it as Parameters default value.

enter image description here

In this case, it is similar to manually running the pipeline to set the parameters value.

When you manually run the pipeline, it will use the defined default value. You can also modify the value when you maually run the pipeline.

enter image description here

like image 167
Kevin Lu-MSFT Avatar answered Jun 30 '26 00:06

Kevin Lu-MSFT