skip to Main Content

I’ve release two tags, 0.1.0 and then 0.0.1. The latest release tag is 0.1.0.

How do I check inside the GitHub Action if the workflow was dispatched by the latest release?

name: Test
on:
  workflow_dispatch:
    branches:
      - main
  release:
    types: [published]
jobs:
  test:
    name: Test
    runs-on: ubuntu-20.04
    steps:
      - run: echo ${{ github.ref_name }}

${{ github.ref_name }} returns the tag version 0.1.0 or 0.0.1, I want to tag Docker images in action the same as GitHub: as image:latest only if the action was dispatched by 0.1.0, 0.1.0 is the latest even if 0.0.1 was dispatched later like displayed in the screenshot below:

enter image description here

2

Answers


  1. Unfortunately, github context doesn’t provide this information.

    As a walkaround you can use git command to retrieve the latest version:

    git tag | sort --version-sort | tail -n1
    

    and then compare it to the current tag in the github actions context:

    ${{ github.ref_name }}
    

    example:

      - name: "Set latest tag"
        id: set-latest-tag
        run: echo "latest_tag=$(git tag | sort --version-sort | tail -n1)" >> $GITHUB_ENV
    
      - name: "Tag Docker image as latest"
        id: tag-as-latest
        if: ${{ github.ref_name == env.latest_tag }}
        run: |
          ...
    
    Login or Signup to reply.
  2. To retrieve the Github release that is marked as latest by the maintainers (does not have to be chronologically or alphabetically the latest created git tag):

    - name: Export LATEST_TAG
      run: |
        echo "LATEST_TAG=$(curl -qsSL 
          -H "Accept: application/vnd.github+json" 
          -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" 
          -H "X-GitHub-Api-Version: 2022-11-28" 
          "${{ github.api_url }}/repos/${{ github.repository }}/releases/latest" 
        | jq -r .tag_name)" >> $GITHUB_ENV
    
    - name: Do something when the current tag is the latest tag
      if: ${{ github.ref_name == env.LATEST_TAG }}
      run: ...
    

    This uses the tag_name attribute in the REST API response (docs)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search