Azure DevOps set env variable

Viewed 35

I am trying to set an environment variable to use it in another task but it doesn't work.

The first task should set the variable "versionTag" so I can use it in the next task as $(versionTag).

Can anyone help me with this?

    - task: Bash@3
      displayName: Create version tag
      inputs:
        targetType: 'inline'
        script: |
         versionTag=$(echo "$(Build.BuildNumber)" | tr '+' '-')
         echo "versionTag: ${versionTag}"
         echo "##vso[task.setvariable variable=versionTag]${versionTag}"

    - task: Docker@2
      displayName: Create runtime docker image
      inputs:
        containerRegistry: '$(dockerRegistryServiceConnection)'
        repository: '$(imageRepository)'
        command: 'build'
        Dockerfile: '$(dockerfilePath)'
        buildContext: '$(Build.SourcesDirectory)'
        tags: |
          $(tags)
          $(versionTag)
1 Answers

There's a magic command string you can write to the log:

echo "##vso[task.prependpath]c:\my\directory\path"

The path will be updated for the scope of the Job. If your pipeline has multiple jobs, you need to issue the same command for future jobs as well.

The updated path will be available in the next step. Not in the step in which you issue the command.

Related