Skip to content

Automatically Tag Latest Official Version with 'latest' in Docker Repository Based on Semantic Versioning #407

Open

Description

@Gezi-lzq

Feature Request

Problem Statement

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

  1. Detects when a release is triggered by a tag.
  2. Verifies that the tag follows the SemVer standard.
  3. Compares the tagged version against all existing versions in the repository to determine if it is the highest.
  4. 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.)

Activity

  1. crazy-max commented on May 27, 2024

    @crazy-max
    Member

    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:

    env:
      LATEST_VERSION: v1.10

    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.

  2. Gezi-lzq commented on Jun 29, 2024

    @Gezi-lzq
    Author

    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:

    env:
      LATEST_VERSION: v1.10

    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.

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

  3. polarathene commented on Feb 25, 2025

    @polarathene

    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.

    The README currently gives an example with this tag rule to conditionally enable the latest tag, which AFAIK doesn't work with a flavor prefix/suffix prop onlatest attribute (where an internal version.latest that flavor.latest=auto uses to determine if an implicit latest tag should be added). The README docs could clarify such better to the reader.

  4. polarathene commented on Mar 2, 2025

    @polarathene

    Workaround

    git fetch --tags is 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-remote examples are much faster.

    Here's a more fleshed out solution for reference if it helps anyone:

    on:
      workflow_dispatch:
      push:
        branches:
          - main
        tags:
          - 'v*.*.*'
    
    # Opt-in to using `-o pipefail`:
    # https://stackoverflow.com/questions/75419587/does-a-github-action-step-use-set-e-semantics-by-default
    defaults:
      run:
        shell: bash
    
    permissions:
      contents: read
    
    jobs:
      publish-images:
        name: 'Build and Publish'
        runs-on: ubuntu-latest
        steps:
          # ...
         
          - name: 'Find the latest release tag (determined by semver sort)'
            id: latest-semver
            if: 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-metadata
            uses: docker/metadata-action@v5
            env:
              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-2686558629
              flavor: |
                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 length
      REPO_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 😓

  5. Gezi-lzq commented on May 17, 2025

    @Gezi-lzq
    Author

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

  6. crazy-max commented on May 22, 2025

    @crazy-max
    Member

    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.

  7. polarathene commented on May 22, 2025

    @polarathene

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions