Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

branch name in the metadata #20

Open
nuclon opened this issue Apr 5, 2022 · 3 comments
Open

branch name in the metadata #20

nuclon opened this issue Apr 5, 2022 · 3 comments

Comments

@nuclon
Copy link

nuclon commented Apr 5, 2022

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)

@mdomke
Copy link
Owner

mdomke commented Apr 12, 2022

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

@steirico
Copy link

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

@needleshaped
Copy link

needleshaped commented Oct 7, 2024

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).

P.S. If you have troubles imaging in which way of format you should add this "branch" in existing x.y.z-p+m, request for free formatting may be the answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants