0
votes

I'm having an issue with a new pipeline I'm setting up for a microservice on Azure DevOps where the pipeline seems to be unable to reference environment variables provided by the build environment.

I have a step in my pipeline that runs a Powershell script that prints out all the environment variables for debugging purposes and this allows me to confirm that the environment does indeed contain the variables I am trying to use.

These variables are BUILD_DEFINITIONNAME = my-microservice-repo and BUILD_BUILDNUMBER = 20200519.2. Essentially I want to combine these two variables to create another variable to name my docker container so I'd have a variable equal to $(BUILD_DEFINITIONNAME):$(BUILD_BUILDNUMBER) which should expand out into my-microservice-repo:20200519.2. We will call this variable imageName. I have defined it in my variables section on my .yml script as

variables:
  - name: imageName
    value: $(BUILD_DEFINITIONNAME):$(BUILD_BUILDNUMBER)

However, if I try to use this variable on my Docker steps:

    - task: Docker@0
      inputs:
        dockerFile: Dockerfile
        azureSubscriptionEndpoint: 'endpoint'
        azureContainerRegistry: 'my container registry'
        imageName: $(imageName)
        action: 'Build an image'

    - task: Docker@0
      inputs:
        azureSubscriptionEndpoint: 'endpoint'
        azureContainerRegistry: 'my container registry'
        action: 'Push an image'
        imageName: $(imageName)

Then when the script runs, I get this error: ##[error]invalid argument "myazurecontainerregistry.azurecr.io/$(build_definitionname):$(build_buildnumber)" for "-t, --tag" flag: invalid reference format

So clearly it's concatenating the strings correctly since I get $(build_definitionname):$(build_buildnumber), however, they're not being expanded, despite the fact that they both exist within the printed variables as I mentioned earlier.

Is there something special I need to do to allow environment variables to be used within my script? We have other pipeline scripts that are working just fine that I copied this yml script from, but mine refuses to work and I can't figure out why, nor can my colleagues.

Any help would be greatly appreciated.

1

1 Answers

0
votes

Figured it out. I was misnaming the variables I wanted to use.

$(BUILD_BUILDNUMBER) should have been $(Build.BuildNumber).