Michele
Michele

Reputation: 349

Azure Pipeline: Update variables according to parameter

I have some very simple variables, which I would like to change according to the environment.

I have written the code below in very different ways (including indentation) but none was fruitful. Alternatives I see are

variables:
  - group : secrets

  - name: hello
    value: world


  ${{ if eq(parameters.environment, 'dev') }}:
    - name: RabbitMQ_replicaCount
      value: 3
  ${{ if eq(parameters.environment, 'test') }}:
      RabbitMQ_replicaCount: '1'

Any other ideas will be appriciated :)

Upvotes: 0

Views: 3130

Answers (1)

Aatif Akhter
Aatif Akhter

Reputation: 2206

I would rather go by a PS script/Bash script for this task. Why ? The logic part of build where manipulation is needed like setting or overriding var based on branch or env can be done in a better way in script rather than the build yaml itself. Also this part un-necessary elongates the yaml.

Step 1 : Define a var in the build pipe with default env name

enter image description here

and may be another var whose value you want to set based on condition

enter image description here

Step 2 : Add a yml file(lets name it BuildEnv.yml) in your repo which actually contains your PowerShell/Bash code:

steps:
- powershell: |
    if($BuildEnv -ne "Test"){
        Write-Host "##vso[task.setvariable variable=BuildEnv]Dev"
        Write-Host "##vso[task.setvariable variable=RabbitMQ_replicaCount]11"
    }
  displayName: 'Override Build Env'
  # MORE CODE HERE

Step 3: Plug your yml in the build pipe as a template-

trigger:
 branches:
   include:
     - master

name: $(date:yyyy-MM-dd_HH.mm)_$(rev:.r)

stages:

- stage: Build_Stage
  displayName: Build_Stage
  jobs:
  - job: Build_Job
    pool:
      name: ABC

    steps:

    - template: ..\BuildEnv.yml

    #REST CODE

That's it. You are done.

Reference : Template usage in Azure DevOps build - https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops

Upvotes: 1

Related Questions