git-semver icon indicating copy to clipboard operation
git-semver copied to clipboard

branch name in the metadata

Open nuclon opened this issue 3 years ago • 3 comments

Hi

It would be quite useful if there could be a possibility to have a branch name somewhere in the metadata as well.

For not breaking back compatibility, it could be implemented as a placeholder for a custom format (for example x.y.z+b-m)

nuclon avatar Apr 05 '22 10:04 nuclon

Could you give an example in which situations this would be helpful?

mdomke avatar Apr 12 '22 15:04 mdomke

IHMO this could be useful when using git-semver for determine the tag of a docker image.

steirico avatar May 30 '22 11:05 steirico

User story:

  • as a developer, I want to be able to deploy bugfix/feature/other branch on Test environment temporarily (and not, let's say usual e.g. main branch)
  • as a devops engineer, I thus must create artifacts (Docker Image / Helm Chart) that can not overlap with versions generated on main, and are easily traced back (SHA is not good enough here).

So I wrote function, that adds branch name to the Docker/Helm tag. Now artifacts are uniquely and easily identified at all times - if I decide to publish/deploy it, or not.

getDockerImageTags.groovy:

def call(String appVersion) {
    // Docker tags are not compatible with semver (+ sign): https://github.com/distribution/distribution/issues/1201
    env.BRANCH_NAME_SANITIZED = env.BRANCH_NAME.replaceAll('[^a-zA-Z0-9_.-]', '-')
    println "env.BRANCH_NAME_SANITIZED = $env.BRANCH_NAME_SANITIZED"
    env.APPVERSION_SANITIZED = appVersion.replaceAll('[^a-zA-Z0-9_.-]', '-')
    println "env.APPVERSION_SANITIZED = $env.APPVERSION_SANITIZED"

    Map dockerTags = [:]

    if (env.BRANCH_NAME in ['main', 'master'] || git.tagIsSemVer2() ) {
        // this logic is later repeated for deployment
        dockerTags['versionTag'] = env.APPVERSION_SANITIZED
        dockerTags['latestTag'] = 'latest'
    } else {
        // optionally deployed to optionally deploy code various branches
        dockerTags['versionTag'] = "$env.APPVERSION_SANITIZED-$env.BRANCH_NAME_SANITIZED"
        dockerTags['latestTag'] = "latest-$env.BRANCH_NAME_SANITIZED"
    }
    currentBuild.displayName += " 🐳 ${dockerTags['versionTag']}"
    println "Tags auto-generated based on branch: $dockerTags"
    return dockerTags
}

The code above, includes logic, that sometimes ADD branch name, sometimes NOT (if it is main branch). So the version becomes 1.1.3-alpha.dev.11-ce117b4b-feature-FOO or 1.1.3-alpha.dev.11-ce117b4b-feature-bugfix-BAR ('x.y.z-p+m' format is used)

However I see myself removing this abstraction, and make branch name part version at all times, if git-semver would support it (as manually tagged/released version will just have clean X.Y.Z anyway).

needleshaped avatar Oct 07 '24 14:10 needleshaped