4

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?

4
  • Hi Branden; is there any reason you can't use a run-time parameter? learn.microsoft.com/en-us/azure/devops/pipelines/process/… Commented Apr 11, 2022 at 12:54
  • Reading it looks like parameters support static values, the version or rollback value needs to be dynamic allowing developers to set it to any value they desire. Commented Apr 11, 2022 at 13:04
  • 1
    No, you can define a parameter to allow free data entry Commented Apr 11, 2022 at 13:48
  • Sorry came back after a vacation. Got it, ok I tested it with a non-required field ' ' and it worked. Their documentation was confusing and not straight forward. Thanks for this. Commented Apr 19, 2022 at 15:15

1 Answer 1

5
+25

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome thanks. Yea Azures documentation didn't do a good job at explaining this imo. You helped clear up what I needed to know. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.