Home > Enterprise >  What's the best practice for Docker images tagging in a multibranch project?
What's the best practice for Docker images tagging in a multibranch project?

Time:10-21

I have a C# application on which different teams work in parallel and each team may work on feature branches. I'm configuring the build pipeline that builds the Docker images and pushes them to an Azure Container Registry. In our daily work, during the deploys we need to address the specific Docker images for each branch/team so that anytime each team can deploy their images.

I am wondering, is there any best practice in multi branch projects to tag the Docker images? For example, <version>-<commit_hash>-<branch_name>?

CodePudding user response:

Since you mentioned .NET, I would suggest having a look at GitVersion. GitVersion is an open source tool that generates a SemVer version number based on the history of the branch you're on.

There are a bunch of rules that dictate how a version is going to be calculated, which you can configure to fit your particular workflow.

For example, running gitversion on a master branch with the default settings will give you something like this:

{
  "Major":0,
  "Minor":1,
  "Patch":0,
  "PreReleaseTag":"",
  "PreReleaseTagWithDash":"",
  "PreReleaseLabel":"",
  "PreReleaseNumber":"",
  "WeightedPreReleaseNumber":60000,
  "BuildMetaData":45,
  "BuildMetaDataPadded":"0045",
  "FullBuildMetaData":"45.Branch.master.Sha.66042002fd0ddeb167932a2726de8f35364b1352",
  "MajorMinorPatch":"0.1.0",
  "SemVer":"0.1.0",
  "LegacySemVer":"0.1.0",
  "LegacySemVerPadded":"0.1.0",
  "AssemblySemVer":"0.1.0.0",
  "AssemblySemFileVer":"0.1.0.0",
  "FullSemVer":"0.1.0 45",
  "InformationalVersion":"0.1.0 45.Branch.master.Sha.66042002fd0ddeb167932a2726de8f35364b1352",
  "BranchName":"master",
  "EscapedBranchName":"master",
  "Sha":"66042002fd0ddeb167932a2726de8f35364b1352",
  "ShortSha":6604200,
  "NuGetVersionV2":"0.1.0",
  "NuGetVersion":"0.1.0",
  "NuGetPreReleaseTagV2":"",
  "NuGetPreReleaseTag":"",
  "VersionSourceSha":"835cac8f9c24a5f7d980f1ccc26af4eab399bbf0",
  "CommitsSinceVersionSource":45,
  "CommitsSinceVersionSourcePadded":"0045",
  "UncommittedChanges":1,
  "CommitDate":"2021-07-06"
}

You can use the value of FullSemVer as the tag of the Docker image produced by a particular branch. However, there's one catch: Docker doesn't support characters in a tag name:

A tag name must be valid ASCII and may contain lowercase and uppercase letters, digits, underscores, periods and dashes. A tag name may not start with a period or a dash and may contain a maximum of 128 characters.

In order to work around this, you'll have to configure GitVersion to generate unique version numbers without using the character. Have a look at the different versioning modes supported by GitVersion to find one that fits your branching and deployment strategy.

CodePudding user response:

You should try this kind of docker image tag that I mention below, which can easily find the branch name and comment of that branch.

Branchname-branchcommit

  • Related