Nayden Van
Nayden Van

Reputation: 1569

Azure DevOps Pipeline yaml file Docker Tag

Hello my expert friends. I know this question might sound really simple but I am seeking for some advice and best practices to follow/learn.

I have a testing infra in azure divided in 2 environment. One is Staging and the other is Production.

Those environment have the same configurations for on hand practices as I want to learn how to deploy specific docker images from staging to production.

At the current state, I have 1 web app in Staging and 1 web app in Production.

My Build pipeline for this lab is try to trigger the build pipeline in staging only if I push to GitHub a Tag, and I achieved this by setting my pipeline as follow:

trigger:
  batch: true
  tags:
    include:
      - '*'
  branches:
    exclude:
    - Staging

This runs a docker build with some c# code and deploys it to a container registry. But in my current pipeline, under the task Docker:

- task: Docker@2
    name: 'dockerBuildAndPush'
    displayName: 'docker - Build & Push'
    inputs:
      repository: $(imageRepository)
      Dockerfile: $(dockerfilePath)
      containerRegistry: ${{ variables.dockerRegistryServiceConnection }}
      buildContext: ${{ variables.buildContext }}
      tags: |
        $(Build.BuildNumber)
        latest

As you can see I have a tags that is based on the Build.BuildNumber This of course during the build process it get the current date + build number, but I wanted to do is target the latest tag coming from GitHub and pass it to the build.

And this is where I got confuse and not really sure about the best practice to follow.

Assuming that on GitHub I push the update with the tag v1.0, is there a way how I can use the pipeline to pick the tag number and pass it automatically to the build? Or I have to update the Tag value manually in the pipeline every time before to push to GitHub?

So basically what I want to have in my container registry is as follow:

In this way, I will be easier to detect quickly which docker image is running on Staging and Production later on.

Sorry if I couldn't explain my dilemma clearly and please if this is the case, don't hesitate to ask for more informations.

UPDATE:

Thank you so much Dave for your reply and you solution. I will look into it asap. Right I was looking for something a bit easier to achieve to understand the full process and get confident with it.

At the current state I managed my pipeline in the following order.

parameters:
  - name: tag
    type: string
    default: 'v1.1'

trigger:
  batch: true
  tags:
    include:
      - '*'
  branches:
    exclude:
    - Staging

and in my docker task set the tag to the parameters

- task: Docker@2
    name: 'dockerBuildAndPush'
    displayName: 'docker - Build & Push'
    inputs:
      repository: $(imageRepository)
      Dockerfile: $(dockerfilePath)
      containerRegistry: ${{ variables.dockerRegistryServiceConnection }}
      buildContext: ${{ variables.buildContext }}
      tags: '${{ parameters.tag }}'

This process worked exactly how expected. In my container registry in the portal I have a version named 'v.1.1'

The issue I am facing is during the release phase of this image.

enter image description here

In the tag for the release I have the $(Build.BuildNumber) which of course as I am setting a parameters variable to build that image, I don't have a build number but a v1.1.

I have been reading around that I can override the BuildNumber with a specific variable name in the yaml file to map the Build.BuildNumber to a parameter and in the relelease pipeline I can leave the Build.BuildNumber as a reference. But is not 100% clear to me how this can be done.

Upvotes: 1

Views: 2617

Answers (1)

Dave Cluderay
Dave Cluderay

Reputation: 7426

You could use GitVersion to generate a version number from your repository tags.

This could be used from your pipeline as follows:

  - task: UseDotNet@2
    displayName: Use .NET Core CLI
    inputs:
      version: "6.x"
  - task: DotNetCoreCLI@2
    displayName: Install GitVersion
    continueOnError: true
    inputs:
      command: "custom"
      custom: "tool"
      arguments: "install GitVersion.Tool --version 5.* --global"
  - task: PowerShell@2
    displayName: Set build version
    inputs:
      targetType: "inline"
      script: |
        Write-Host "##vso[build.updatebuildnumber]$(dotnet-gitversion /showvariable FullSemVer)"

The above example will set the Build.BuildNumber variable to a version string based on the last tag. You can, of course, set some other variable instead.

You can also customise the way GitVersion chooses a version number by adding a GitVersion.yml file to your repository.

This answer should serve as a starting point for you - it doesn't produce exactly the format you want, you will need to look at configuration options and such if you want a very specific format.

Also, look at the different modes that GitVersion can run in, since they will affect the version numbers generated for any commits inbetween tagged commits!

Upvotes: 1

Related Questions