Skip to content

Commit

Permalink
Add: Make is-latest-tag action more flexible
Browse files Browse the repository at this point in the history
Allow to run the action not only for pushed tag events. With this change
it will be possible to run it even for workflow_dispatch events.
  • Loading branch information
bjoernricks committed Mar 19, 2024

Verified

This commit was signed with the committer’s verified signature.
mchack-work Michael Cardell Widerkrantz
1 parent e226183 commit 78d04c8
Showing 3 changed files with 205 additions and 22 deletions.
165 changes: 165 additions & 0 deletions .github/workflows/test-is-latest-tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
name: Test Is Latest Tag Action

on:
push:
tags:
- "v*"
branches:
- main
pull_request:

jobs:
test-older-tag:
runs-on: ubuntu-latest
name: Test older Tag
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Repo and Tag
run: |
mkdir -p /tmp/repo
cd /tmp/repo
git config --global init.defaultBranch main
git config --global user.email "you@example.com"
git config --global user.name "Some User"
git init
touch some.file
git add some.file
git commit -m "Initial commit"
git tag v1.2.3
- name: Test is-latest-tag
uses: ./is-latest-tag
id: is-latest-tag
with:
tag-name: v1.2.2
working-directory: /tmp/repo
- name: Assert
run: |
if [[ "${{ steps.is-latest-tag.outputs.is-latest-tag }}" = "true" ]]; then
echo "::error::The tag 1.2.2 is not the latest tag"
exit 1
fi
test-newer-tag:
runs-on: ubuntu-latest
name: Test newer Tag
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Repo and Tag
run: |
mkdir -p /tmp/repo
cd /tmp/repo
git config --global init.defaultBranch main
git config --global user.email "you@example.com"
git config --global user.name "Some User"
git init
touch some.file
git add some.file
git commit -m "Initial commit"
git tag v1.2.3
- name: Test is-latest-tag
uses: ./is-latest-tag
id: is-latest-tag
with:
tag-name: v1.2.4
working-directory: /tmp/repo
- name: Assert
run: |
if [[ "${{ steps.is-latest-tag.outputs.is-latest-tag }}" != "true" ]]; then
echo "::error::The tag 1.2.4 is the latest tag"
exit 1
fi
test-is-latest-tag:
runs-on: ubuntu-latest
name: Test is latest Tag
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Repo and Tag
run: |
mkdir -p /tmp/repo
cd /tmp/repo
git config --global init.defaultBranch main
git config --global user.email "you@example.com"
git config --global user.name "Some User"
git init
touch some.file
git add some.file
git commit -m "Initial commit"
git tag v1.2.3
- name: Test is-latest-tag
uses: ./is-latest-tag
id: is-latest-tag
with:
tag-name: v1.2.3
working-directory: /tmp/repo
- name: Assert
run: |
if [[ "${{ steps.is-latest-tag.outputs.is-latest-tag }}" != "true" ]]; then
echo "::error::The tag 1.2.3 is the latest tag"
exit 1
fi
test-branch-as-tag-name:
runs-on: ubuntu-latest
name: Test branch as latest Tag
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Repo and Tag
run: |
mkdir -p /tmp/repo
cd /tmp/repo
git config --global init.defaultBranch main
git config --global user.email "you@example.com"
git config --global user.name "Some User"
git init
touch some.file
git add some.file
git commit -m "Initial commit"
git tag v1.2.3
- name: Test is-latest-tag
uses: ./is-latest-tag
id: is-latest-tag
with:
tag-name: some-branch
working-directory: /tmp/repo
- name: Assert
run: |
if [[ "${{ steps.is-latest-tag.outputs.is-latest-tag }}" = "true" ]]; then
echo "::error::The branch some-branch is not the latest tag"
exit 1
fi
test-empty-tag-name:
runs-on: ubuntu-latest
name: Test empty tag name as latest Tag
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Repo and Tag
run: |
mkdir -p /tmp/repo
cd /tmp/repo
git config --global init.defaultBranch main
git config --global user.email "you@example.com"
git config --global user.name "Some User"
git init
touch some.file
git add some.file
git commit -m "Initial commit"
git tag v1.2.3
- name: Test is-latest-tag
uses: ./is-latest-tag
id: is-latest-tag
with:
tag-name: ""
working-directory: /tmp/repo
- name: Assert
run: |
if [[ "${{ steps.is-latest-tag.outputs.is-latest-tag }}" = "true" ]]; then
echo "::error::An empty tag name is not the latest tag"
exit 1
fi
10 changes: 9 additions & 1 deletion is-latest-tag/README.md
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@
GitHub Action to check if a git tag is the latest tag.

Please keep in mind that the full history (`checkout with fetch-depth 0`) is
required for the `is-latest-tag` action.
required for the `is-latest-tag` action. A tag is considered latest if its
version is greater or equal as the latest existing tag.

## Example

@@ -34,6 +35,13 @@ jobs:
# do something else if the current tag is not the latest tag
```
## Input Arguments
| Input Variable | Description | Default |
| ----------------- | --------------------------------------------------------------------------------- | ------------------------- |
| tag-name | Name of the tag to check if it is the latest tag | `${{ github.ref_name }}` |
| working-directory | Working directory for the action. Has to reference the directory of the checkout. | `${{ github.workspace }}` |

## Output Arguments

| Output Variable | Description |
52 changes: 31 additions & 21 deletions is-latest-tag/action.yml
Original file line number Diff line number Diff line change
@@ -2,6 +2,13 @@ name: "Check if a tag is the latest tag in a repository"
author: "Björn Ricks <bjoern.ricks@greenbone.net>"
description: "An action to check if a tag is the latest tag in a repository"

inputs:
tag-name:
description: "Name of the tag to check. Defaults to `github.ref_name`."
default: ${{ github.ref_name }}
working-directory:
description: "Working directory for the action. Defaults to `github.workspace`."
default: ${{ github.workspace }}
outputs:
is-latest-tag:
description: Evaluates to true if the created tag is the latest git tag
@@ -17,33 +24,36 @@ runs:
- name: "set is-latest-tag"
id: latest
run: |
if [ "${{ github.ref_type }}" = "tag" ]; then
# find the latest version that is not ourself
export LATEST_VERSION=$(git tag -l | grep -v '${{ github.ref_name }}' | sort -r --version-sort | head -n 1)
# no tag name given
if [[ -z "${{ inputs.tag-name }}" ]]; then
echo "is-latest-tag=false" >> $GITHUB_OUTPUT
fi
# find the latest version that is not ourself
TAGS=$(git tag -l)
export LATEST_VERSION=$(echo $TAGS | grep -v '${{ inputs.tag-name }}' | sort -r --version-sort | head -n 1)
# get major minor patch versions
IFS='.' read -r latest_major latest_minor latest_patch << EOF
# get major minor patch versions
IFS='.' read -r latest_major latest_minor latest_patch << EOF
$LATEST_VERSION
EOF
IFS='.' read -r tag_major tag_minor tag_patch << EOF
${{ github.ref_name }}
IFS='.' read -r tag_major tag_minor tag_patch << EOF
${{ inputs.tag-name }}
EOF
# remove leading v
latest_major=$(echo $latest_major | cut -c2-)
tag_major=$(echo $tag_major | cut -c2-)
if [[ $tag_major -gt $latest_major ]]; then
echo "is-latest-tag=true" >> $GITHUB_OUTPUT
elif [[ $tag_major -eq $latest_major && $tag_minor -gt $latest_minor ]]; then
echo "is-latest-tag=true" >> $GITHUB_OUTPUT
elif [[ $tag_major -eq $latest_major && $tag_minor -eq $latest_minor && $tag_patch -gt $latest_patch ]]; then
echo "is-latest-tag=true" >> $GITHUB_OUTPUT
else
echo "is-latest-tag=false" >> $GITHUB_OUTPUT
fi
# remove leading v
latest_major=$(echo $latest_major | cut -c2-)
tag_major=$(echo $tag_major | cut -c2-)
if [[ $tag_major -gt $latest_major ]]; then
echo "is-latest-tag=true" >> $GITHUB_OUTPUT
elif [[ $tag_major -eq $latest_major && $tag_minor -gt $latest_minor ]]; then
echo "is-latest-tag=true" >> $GITHUB_OUTPUT
elif [[ $tag_major -eq $latest_major && $tag_minor -eq $latest_minor && $tag_patch -gt $latest_patch ]]; then
echo "is-latest-tag=true" >> $GITHUB_OUTPUT
else
echo "is-latest-tag=false" >> $GITHUB_OUTPUT
echo "is-latest-tag=false" >> $GITHUB_OUTPUT
fi
working-directory: ${{ inputs.working-directory }}
shell: bash

0 comments on commit 78d04c8

Please sign in to comment.