You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Automatically Tag Latest Official Version with 'latest' in Docker Repository Based on Semantic Versioning #407
Our current release process does not automatically adjust the latest tag to the most recent official version based on semantic versioning. This limitation arises from our unique git workflow, where:
Official versions are not strictly confined to the main branch.
Version releases do not always follow a sequential temporal order.
Given these constraints, we seek a feature that, upon releasing an image triggered by a tag, checks if the tag adheres to the Semantic Versioning (SemVer) standard and represents the highest version within the repository. If both conditions are met, the image should additionally be tagged as latest.
Proposed Solution
Detects when a release is triggered by a tag.
Verifies that the tag follows the SemVer standard.
Compares the tagged version against all existing versions in the repository to determine if it is the highest.
If the tagged version is the highest based on SemVer, automatically tags the image with latest.
[Tag Release] ---> [Check if SemVer] ---> [Compare Versions] ---> [Is highest version?] ---> [Tag as 'latest']
(We have not found existing documentation or capabilities within our current toolset that meet this specific need.Additionally, I'm unsure if my idea above constitutes a valid requirement. I would appreciate some feedback on its viability. If deemed reasonable, I'm willing to implement this feature.)
Compares the tagged version against all existing versions in the repository to determine if it is the highest.
I wonder why you would need this at all? I guess you maintain n and n-1 versions of your project or more right? Such as v1.9.0 patch releases and v1.10.0. In such case you could just have an env in your workflow that would be the latest version digits like:
Compares the tagged version against all existing versions in the repository to determine if it is the highest.
I wonder why you would need this at all? I guess you maintain n and n-1 versions of your project or more right? Such as v1.9.0 patch releases and v1.10.0. In such case you could just have an env in your workflow that would be the latest version digits like:
Or I guess this would be fine to create a new global expression to handle this and remove extra overhead in your workflow.
Thank you for your suggestion on setting an environment variable for the latest version and using semver comparison with GitHub-script in the workflow. However, I am looking for a solution that does not require manual updates to the workflow file each time a new version is released. To achieve this, I plan to use the following approach:
LATEST_TAG=$(git fetch --tags && git tag --sort=-v:refname | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1)
This command will automatically fetch all tags, sort them in descending order according to semantic versioning, filter them to match the semantic versioning pattern, and then select the top one as the latest official version tag. ...
Or I guess this would be fine to create a new global expression to handle this and remove extra overhead in your workflow.
This might be a worthwhile feature to have vs separate implementations in workflows to support it, as it can be a bit surprising and unintentional due to the implicit latest=auto flavor paired with type=semver,pattern= / type=ref,event=tag qualifiers. Even with a manual type=raw,value=latest,enable=<condition> you'd run into this concern.
I've seen this in the wild with Tyk's images, it was not fun to troubleshoot with their release process as they had some other mishaps at the time with branches/tags. The main issue was any GH release with tag was overwriting the latest unintentionally for point releases of older minors.
Related was the semver tags itself. This does not only affect latest but the major tags will also be published pointing to an earlier minor release due to it's point release update. So it's not just latest to account for in this scenario.
Example
So to recap, a trigger from the new tag of v5.3.4 with the highest semver v5.4.3 in the 5.x series, and highest semver release of 6.1.2 will result in:
latest => 5.3.4 (bad, should be 6.1.2)
5 => 5.3.4 (bad, should stay at 5.4.3)
5.3 => 5.3.4 (good)
5.3.4 (good)
Related caveat - type=raw,value=latest,enable=false
There's a related issue for latest that is specific to type=raw,value=latest,enable=false, which I've seen a few projects try.
That tag will skip processing when enable=false is evaluated, but with the default flavor.latest=auto setting, any of the other tag types that would implicitly enable the latest tag (like the semver type) will cause the latest tag to be added/updated too.
Here's a more fleshed out solution for reference if it helps anyone:
on:
workflow_dispatch:
push:
branches:
- maintags:
- 'v*.*.*'# Opt-in to using `-o pipefail`:# https://stackoverflow.com/questions/75419587/does-a-github-action-step-use-set-e-semantics-by-defaultdefaults:
run:
shell: bashpermissions:
contents: readjobs:
publish-images:
name: 'Build and Publish'runs-on: ubuntu-lateststeps:
# ...
- name: 'Find the latest release tag (determined by semver sort)'id: latest-semverif: startsWith(github.ref, 'refs/tags/v')env:
REPO_URL: ${{ github.event.repository.html_url }}# Filter for semver tags like: v1.2.34 (Ignores pre-release tags)TAG_FILTER: '^refs/tags/v[0-9]+\.[0-9]+\.[0-9]+$'# Retrieves the tags for the repo sorted by version in descending order,# `awk` strips away the first column (commit hash)# `grep` filters the tags to only those we care about# `head` takes the top/first line which is the highest ranged semver tag# The result has `refs/tags/v` prefixing the semver value, similar to `github.ref`run: | TAG=$( git ls-remote --exit-code --refs --tags --sort='-v:refname' "${REPO_URL}" \ | awk '{print $2}' | grep -E "${TAG_FILTER}" | head -n 1 ) echo "tag-ref=${TAG}" >> "${GITHUB_OUTPUT}"# ...
- name: 'Prepare image tags'id: docker-metadatauses: docker/metadata-action@v5env:
is-latest-tag: ${{ github.ref == steps.latest-semver.outputs.tag-ref }}is-not-major-zero-ver: ${{ !startsWith(github.ref, 'refs/tags/v0.') }}with:
# WORKAROUND: Prevents accidentally overwriting `latest` + semver tags:# https://github.com/docker/metadata-action/issues/461#issuecomment-2686558629flavor: | latest=${{ env.is-latest-tag }}# `edge` tag is used for development images between tagged releases:# Release tags (implicitly adds `latest` tag unless tag belongs to an older release series):tags: | type=edge,branch=${{ github.event.repository.default_branch }} type=semver,pattern={{major}},enable=${{ fromJSON(env.is-latest-tag) && fromJSON(env.is-not-major-zero-ver) }} type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}}.{{minor}}.{{patch}}
Notes
If running git ls-remote on a private repo, unless there was an earlier actions/checkout step that persisted credentials to .git/config, you'll need to adjust the REPO_URL to support basic-auth with the GH token (no credentials would be persisted to disk for this git operation, but it would happen with git clone for example):
# NOTE: `gha` can be any value for the username, it must be at least one character in lengthREPO_URL: https://gha:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.event.repository.full_name }}
The TAG_FILTER regex above is a bit strict, you could replace it with ^refs/tags/v?[0-9]+(\.[0-9]+)?{2}$ which would allow an optional v prefix and no strict semver enforcement of x.y.z other than only permitting digits.
I've seen some projects are inconsistent with their release tags (v prefix usage and number of version components both varying between releases).
The example workflow has other conditions using the v prefix, you'd need to adjust those accordingly (the on.push.tags pattern also enforces a trigger only on strict semver tags v<major>.<minor>.<patch>).
type=semver,pattern={{major}} has a conditional on two ENV bool strings, to get a proper bool result they need to use fromJSON().
There's a third condition that should technically be applied to the major tag, when it is not the latest minor of it's major series (like in the example of my prior comment).
If you filter the tags retrieved by major versions, you could check for this like:
# This example isn't too useful, but this command can do semver comparisons with# le (<=), lt (<), etc instead of `eq`.
dpkg --compare-versions '5.3.4' eq '5.4.3'# It can also compare different components:# (this will set the `0` or `1` status ($?) as the value)
IS_MAJOR_ZERO=$(dpkg --compare-versions "${EVENT_TAG}" lt 1;echo${?})
Using the equivalent GH API / CLI might be a little friendlier, but would still need to sort the results properly (perhaps the CLI could add a feature for that).
Perhaps there's a trustworthy action out there, otherwise copy/paste of the above step works 👍
Hopefully the workaround conveys a bunch of caveats that the action could better support to DRY workflows handling these edge cases.
That said like many of the other features it'd only be helpful when leaning into repo context. The manual approach would still be necessary for configuring tags explicitly if the image is to build software from another repo and matching it's tags/refs 😓
@thompson-shaun@crazy-max Hello, I would like to inquire about the current status of this issue and understand what the next steps are planned. If possible, I would be more than happy to contribute to resolving it.
Compares the tagged version against all existing versions in the repository to determine if it is the highest.
This seems like something that should be enforced earlier in the release process. By the time a tag is pushed, the Git history is already in a state where an incorrect version can cause issues. Relying on CI to determine if a version is the "latest" feels like a workaround for a process issue. Ideally, version validation and ordering should happen before the tag is created and pushed.
While I did demonstrate how you can approach automated checking on latest semver tags, it's much simpler to take the earlier suggestion from @crazy-max to bump an ENV with a hard-coded latest version tag to compare to, but you must establish a release process that remembers to do that correctly before tagging the release (similar to updating other metadata files like package metadata).
If you're not as confident about that, you could avoid handling the latest tag release in the main workflow, and have a separate workflow with manual trigger to update the latest tag based on an existing image tag/digest input.
As shown above earlier, for semver there is more problems than just latest tag itself. Semver tags need to be conditionally managed too if your release process involves that.
My workaround example shows the final step handling the semver concerns raised. But if there were a new release to the 5.x minor series, it'd also fail there, requiring new logic to ensure the older major version tag is updated properly too.
Also given how I approached checking for latest in the prior step, I raised that concern for how the repo may differ. Say you manage a Dockerfile that builds another git project, you need to customize that now, so that becomes another configurable an action would need to support.
It'd be convenient for docker/metadata-action to simplify this but I can see why there is reluctance to add the logic vs deferring to another action, or handling it more explicitly.
Activity
crazy-max commentedon May 27, 2024
I wonder why you would need this at all? I guess you maintain n and n-1 versions of your project or more right? Such as
v1.9.0patch releases andv1.10.0. In such case you could just have an env in your workflow that would be the latest version digits like:And then you could just compare any tag to it by using semver comparison with https://github.com/npm/node-semver and https://github.com/actions/github-script in a dedicated step to set latest as new env for the metadata-action.
Or I guess this would be fine to create a new global expression to handle this and remove extra overhead in your workflow.
Gezi-lzq commentedon Jun 29, 2024
Thank you for your suggestion on setting an environment variable for the latest version and using semver comparison with GitHub-script in the workflow. However, I am looking for a solution that does not require manual updates to the workflow file each time a new version is released. To achieve this, I plan to use the following approach:
This command will automatically fetch all tags, sort them in descending order according to semantic versioning, filter them to match the semantic versioning pattern, and then select the top one as the latest official version tag. ...
polarathene commentedon Feb 25, 2025
This might be a worthwhile feature to have vs separate implementations in workflows to support it, as it can be a bit surprising and unintentional due to the implicit
latest=autoflavor paired withtype=semver,pattern=/type=ref,event=tagqualifiers. Even with a manualtype=raw,value=latest,enable=<condition>you'd run into this concern.I've seen this in the wild with Tyk's images, it was not fun to troubleshoot with their release process as they had some other mishaps at the time with branches/tags. The main issue was any GH release with tag was overwriting the
latestunintentionally for point releases of older minors.Related was the semver tags itself. This does not only affect
latestbut the major tags will also be published pointing to an earlier minor release due to it's point release update. So it's not justlatestto account for in this scenario.Example
So to recap, a trigger from the new tag of
v5.3.4with the highest semverv5.4.3in the 5.x series, and highest semver release of6.1.2will result in:latest=>5.3.4(bad, should be6.1.2)5=>5.3.4(bad, should stay at5.4.3)5.3=>5.3.4(good)5.3.4(good)Related caveat -
type=raw,value=latest,enable=falseThere's a related issue for
latestthat is specific totype=raw,value=latest,enable=false, which I've seen a few projects try.That tag will skip processing when
enable=falseis evaluated, but with the defaultflavor.latest=autosetting, any of the other tag types that would implicitly enable thelatesttag (like the semver type) will cause thelatesttag to be added/updated too.The README currently gives an example with this tag rule to conditionally enable the
latesttag, which AFAIK doesn't work with aflavorprefix/suffix proponlatestattribute (where an internalversion.latestthatflavor.latest=autouses to determine if an implicitlatesttag should be added). The README docs could clarify such better to the reader.polarathene commentedon Mar 2, 2025
Workaround
git fetch --tagsis a bit slow on larger repos like the Tyk one. It also increased the repo dir size by over 200MB as a result 🤔These
git ls-remoteexamples are much faster.Here's a more fleshed out solution for reference if it helps anyone:
Notes
If running
git ls-remoteon a private repo, unless there was an earlieractions/checkoutstep that persisted credentials to.git/config, you'll need to adjust theREPO_URLto support basic-auth with the GH token (no credentials would be persisted to disk for thisgitoperation, but it would happen withgit clonefor example):The
TAG_FILTERregex above is a bit strict, you could replace it with^refs/tags/v?[0-9]+(\.[0-9]+)?{2}$which would allow an optionalvprefix and no strict semver enforcement ofx.y.zother than only permitting digits.vprefix usage and number of version components both varying between releases).vprefix, you'd need to adjust those accordingly (theon.push.tagspattern also enforces a trigger only on strict semver tagsv<major>.<minor>.<patch>).type=semver,pattern={{major}}has a conditional on two ENV bool strings, to get a proper bool result they need to usefromJSON().There's a third condition that should technically be applied to the major tag, when it is not the latest minor of it's major series (like in the example of my prior comment).
If you filter the tags retrieved by major versions, you could check for this like:
Using the equivalent GH API / CLI might be a little friendlier, but would still need to sort the results properly (perhaps the CLI could add a feature for that).
Perhaps there's a trustworthy action out there, otherwise copy/paste of the above step works 👍
Hopefully the workaround conveys a bunch of caveats that the action could better support to DRY workflows handling these edge cases.
That said like many of the other features it'd only be helpful when leaning into repo context. The manual approach would still be necessary for configuring tags explicitly if the image is to build software from another repo and matching it's tags/refs 😓
Gezi-lzq commentedon May 17, 2025
@thompson-shaun @crazy-max Hello, I would like to inquire about the current status of this issue and understand what the next steps are planned. If possible, I would be more than happy to contribute to resolving it.
crazy-max commentedon May 22, 2025
This seems like something that should be enforced earlier in the release process. By the time a tag is pushed, the Git history is already in a state where an incorrect version can cause issues. Relying on CI to determine if a version is the "latest" feels like a workaround for a process issue. Ideally, version validation and ordering should happen before the tag is created and pushed.
polarathene commentedon May 22, 2025
Agree with @crazy-max 👍
While I did demonstrate how you can approach automated checking on latest semver tags, it's much simpler to take the earlier suggestion from @crazy-max to bump an ENV with a hard-coded latest version tag to compare to, but you must establish a release process that remembers to do that correctly before tagging the release (similar to updating other metadata files like package metadata).
If you're not as confident about that, you could avoid handling the
latesttag release in the main workflow, and have a separate workflow with manual trigger to update thelatesttag based on an existing image tag/digest input.As shown above earlier, for semver there is more problems than just
latesttag itself. Semver tags need to be conditionally managed too if your release process involves that.My workaround example shows the final step handling the semver concerns raised. But if there were a new release to the
5.xminor series, it'd also fail there, requiring new logic to ensure the older major version tag is updated properly too.Also given how I approached checking for latest in the prior step, I raised that concern for how the repo may differ. Say you manage a
Dockerfilethat builds another git project, you need to customize that now, so that becomes another configurable an action would need to support.It'd be convenient for
docker/metadata-actionto simplify this but I can see why there is reluctance to add the logic vs deferring to another action, or handling it more explicitly.ci: move :latest only for the highest version