5

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"
Adnan Zaman
  • 109
  • 1
  • 2
  • 9
  • 1
    Can you provide more detail on your pipeline, specifically where the parameter is being consumed? A screenshot of your configuration or your pipeline's YAML would help. – Max Morrow Feb 17 '21 at 20:42
  • 1
    I edited to add the yaml. I'm using the parameter as a command line argument during one of the steps. – Adnan Zaman Feb 17 '21 at 21:36
  • 1
    Thanks! This is helpful. Do you have specific branches that will be running integration tests vs unit tests? I ask because, for cases similar to these, I often recommend setting the value of the tests that will be run based on the branch it's building off of. For example, run Integration Tests for all **master** builds, and only unit tests for all **develop** builds. – Max Morrow Feb 17 '21 at 23:33
  • I'm not sure if I have to do it only for specific branches, but that sounds good, I'll keep that in mind, thanks. From my understanding, the ability to choose a subset of tests needs to be available regardless of any other factor – Adnan Zaman Feb 18 '21 at 16:04

1 Answers1

7

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

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28