Branden
Branden

Reputation: 227

Azure Devops: Overwrite Environment Variable at runtime

I am trying to set up an environment variable that can be used from the Azure Devops Library that by default is set to null value. This part is working, but I want the development teams to be able to overwrite that value ad hoc. I don't want to add this manually to all environment variables and I would like to build a condition in my playbooks to say when var != "" rather then when: var != "$(rollback)"

Here is my config:

name: $(Date:yyyyMMdd)$(Rev:.r)

trigger: none

pr: none

resources:
  pipelines:
    - pipeline: my-ui
      source: my-ui-ci-dev
      trigger: true

variables:
  - group: Dev

jobs:
  - job: my_cd
    pool:
      vmImage: "ubuntu-20.04"
    container:
      image: "myacr.azurecr.io/devops/services-build:$(services_build_tag_version)"
      endpoint: "Docker Registry"

    steps:
      - task: Bash@3
        displayName: "My Playbook"
        env:
          git_username: "$(git_username)"
          git_password: "$(git_password)"
          config_repo: "$(config_repo)"
          service_principal_id: "$(service_principal_id)"
          service_principal_secret: "$(service_principal_secret)"
          subscription_id: "$(subscription_id)"
          tenant_id: "$(tenant_id)"
          rollback: "$(rollback)"
          source_dir: "$(Build.SourcesDirectory)"
          env_dir: "$(Agent.BuildDirectory)/env"
          HELM_EXPERIMENTAL_OCI: 1
        inputs:
          targetType: "inline"
          script: |
            ansible-playbook cicd/ansible/cd.yaml -i "localhost, " -v

When choosing to run the pipeline, I would like the developers to just go to Run > Add Variable > Manually add the variable and value > run pipeline

And then in the playbook the value is "" if not defined or if it is then show as the value they type. Any suggestions on how I can do this with AZDO?

Upvotes: 4

Views: 4456

Answers (1)

Vince Bowdren
Vince Bowdren

Reputation: 9208

You can do this more easily with a run-time parameter

parameters:
  - name: rollback
    type: string
    default: ' '

variables:
  - group: dev
  - ${{ if ne(parameters.rollback, ' ') }}:
    - name: rollback
      value: ${{ parameters.rollback }}

The way this works in practice is:

  1. the pipeline queue dialog automatically includes a 'rollback' text field: screenshot of rollback parameter textfield
  2. if the developer types a value into the rollback parameter field, that value is used to override the rollback variable
  3. otherwise, the value from the variable group is used.

Note that you need to give the parameter a default value of a single space; otherwise the pipeline won't let you leave it empty.

Upvotes: 5

Related Questions