John Hennesey
John Hennesey

Reputation: 375

Azure pipelines UI to accept parameters (like Jenkins)

Jenkins has a UI concept with dropdown lists, etc. to allow users to specify variables at run time. This has proven essential in our builds to make decisions in the pipeline (ie. which agent to run on, code base to choose, etc). By allowing parameters we are able to have a single pipeline/definition handle the same task for many clients/releases/environments.

I have been watching many people over the past year ask for this - to eliminate the number of almost identical build definitions - is there a best practice to handle this? Would be nice to have a single build definition for a specific task that can be smart enough to handle parameters.

Edit : example of possible pseudo-code to build on levi-lu@MSFT's suggestion.

parameters:
- name: ClientName
  displayName: Pool Image
  default: Select client
  values: powershell
  valuesScript : [
      assemble curl request to http://myUrl.com/Clients/GetAll
  ]

- name: TargetEnvironment
  displayName: Client Environment
  type: string
  values: powershell
  valuesScript: [
      assemble curl request using above parameter value to 
        https://myUrl.com/Clients/$(ClientName)/GetEnvironments
  ]


trigger: none

jobs:
- job: build
  displayName: Run pipeline job
  pool: 
    vmImage: windows-latest
  parameters:
    ClientName : $(ClientName)
    TargetEnvironment : $(TargetEnvironment)
  steps:
  - script: echo building $(Build.BuildNumber)

Upvotes: 1

Views: 3969

Answers (1)

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30353

Runtime parameters is available now. You can now set runtime parameters at the beginning of your pipepline YAML using parameters. For below example:

parameters:
- name: image
  displayName: Pool Image
  default: ubuntu-latest
  values:
  - windows-latest
  - vs2017-win2016
  - ubuntu-latest
  - ubuntu-16.04
  - macOS-latest
  - macOS-10.14
- name: test
  displayName: Run Tests?
  type: boolean
  default: false

trigger: none

jobs:
- job: build
  displayName: Build and Test
  pool: 
    vmImage: ${{ parameters.image }}
  steps:
  - script: echo building $(Build.BuildNumber)
  - ${{ if eq(parameters.test, true) }}:
    - script: echo "Running all the tests"

Above example is from Microsoft official document. Click here to learn more about runtime parameters.

When you run above Yaml pipeline, You will be able to select the parameter's value from the dropdown list. See below screenshot.

enter image description here

Update: To set variables dynamically at runtime.

You can use the task.setvariable logging command to set variables dynamically in scripts.

For below example: $resultValue is the value from rest api call. And its value is assigned to variable VariableName

- powershell: |
    $resultValue = call from Rest API
    echo "##vso[task.setvariable variable=VariableName]$resultValue"

Check document here for more information.

Upvotes: 2

Related Questions