Search code examples
dockerazure-devopsazure-pipelines

Set Tag name from git as docker image tag in azure devops


my main goal is to have my docker tag defined with the tag I enter in azure devops.

enter image description here

here is should trigger and should be "DOCKER:0.0.2"

Pipeline Main:

trigger:
  tags:
    include:
      - '*'

pr: none

jobs:
  - job: Build
    displayName: 'Build Job'
    pool:
      vmImage: 'ubuntu-20.04'
    steps:
      - task: PowerShell@2
        displayName: Prepare variables
        inputs:
          targetType: inline
          script: |
            $newSourceBranch = "$(Build.SourceBranch)" -replace 'refs/tags/', '' 
            $Command = "##vso[build.addbuildtag]"+$newSourceBranch
            write-host "Create a Build TAG called $newSourceBranch"
            ##vso[task.setvariable variable=myVar]$newSourceBranch
            write-host "Set variable: $(myVar)"
      - template: azure-pipelines-template.yml
        parameters:
          repositoryName: 'BeeHive-Prod'
          dockerTag: "$(myVar)"

Template:

parameters:
  - name: repositoryName
    type: string
  - name: dockerTag
    type: string
    default: $(Build.BuildId)

steps:
  - task: UseDotNet@2
    displayName: Install .NET 8 SDK
    inputs:
      packageType: 'sdk'
      version: '8.0.100'

  - task: DotNetCoreCLI@2
    displayName: 'Restore Solution'
    inputs:
      command: 'restore'
      projects: '**/*.csproj'

  - task: DotNetCoreCLI@2
    displayName: 'Build Solution'
    inputs:
      command: 'build'
      projects: '**/*.csproj'

  - task: DotNetCoreCLI@2
    displayName: Dotnet Test
    inputs:
      command: test
      projects: |
        **/*Test.csproj
      arguments: '--configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat="cobertura%2copencover"'
      publishTestResults: true

  - task: Docker@2
    inputs:
      containerRegistry: 'acrtitans'
      repository: ${{ parameters.repositoryName }}
      command: 'buildAndPush'
      Dockerfile: './BeeHive/Dockerfile'
      buildContext: './'
      tag: ${{ parameters.dockerTag }}

Error:

enter image description here


Solution

  • Instead of:

    ##vso[task.setvariable variable=myVar]$newSourceBranch
    

    Try:

    Write-Host "##vso[task.setvariable variable=myVar;]$newSourceBranch"
    

    As per the documentation, the variable will be exposed to the following tasks as an environment variable using macro syntax $(myVar).

    This means that in order to test if myVar was properly set you need to print the variable in a new task - example:

    script: |
        $newSourceBranch = "$(Build.SourceBranch)" -replace 'refs/tags/', '' 
        $Command = "##vso[build.addbuildtag]"+$newSourceBranch
        Write-Host $Command
        write-host "Create a Build TAG called $newSourceBranch"
        write-host "##vso[task.setvariable variable=myVar;]$newSourceBranch"
    script: |
        write-host "Set variable: $(myVar)"
    

    Please note that the syntax might change depending on whether you set isOutput to true or false, and where you use the variable (same job? different job/stage?). See the documentation for more details.