Skip to content

Commit

Permalink
Add a next-repository-tag-number action
Browse files Browse the repository at this point in the history
  • Loading branch information
lnhrdt committed Nov 9, 2024
1 parent e97200f commit dec28b9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Initial release of the next-repository-tag-number action.

## [1.0.1] - 2024-11-04

### Fixed
Expand Down
4 changes: 4 additions & 0 deletions next-repository-tag-number/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM alpine:latest
RUN apk add --update --no-cache --no-progress curl jq
COPY get-next-tag-number.sh /get-next-tag-number.sh
ENTRYPOINT ["/get-next-tag-number.sh"]
35 changes: 35 additions & 0 deletions next-repository-tag-number/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Next Repository Tag Number
description: Get the next tag number for a repository.

inputs:
repository_url:
description: The URL of the OCI-compatible repository.
required: true
bearer_token:
description: Optional bearer token for repository authentication.
required: false
initial_number:
description: Starting tag number if no tags exist.
default: '1'
required: false
tag_prefix:
description: Prefix for the tags.
default: 'build-'
required: false

outputs:
tag:
description: The next tag.
value: ${{ steps.next-tag.outputs.tag }}
number:
description: The next tag number.
value: ${{ steps.next-tag.outputs.number }}

runs:
using: docker
image: Dockerfile
args:
- ${{ inputs.repository_url }}
- ${{ inputs.bearer_token }}
- ${{ inputs.initial_number }}
- ${{ inputs.tag_prefix }}
34 changes: 34 additions & 0 deletions next-repository-tag-number/get-next-tag-number.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env sh

REPOSITORY_URL=$1
BEARER_TOKEN=$2
INITIAL_NUMBER=${3:-1}
TAG_PREFIX=${4:-"build-"}

if [ -n "$BEARER_TOKEN" ]; then
AUTH_HEADER="-H \"Authorization: Bearer ${BEARER_TOKEN}\""
else
AUTH_HEADER=""
fi

TAGS=$(curl -s $AUTH_HEADER "${REPOSITORY_URL}/tags/list" | jq -r '.tags[]' || echo "")

if [ -z "$TAGS" ]; then
echo "No existing tags were found. Using initial tag number ${INITIAL_NUMBER}."
NEXT_TAG_NUMBER=$INITIAL_NUMBER
else
HIGHEST_TAG=$(echo "$TAGS" | grep -Eo "${TAG_PREFIX}[0-9]+" | sort -V | tail -n 1)

if [ -z "$HIGHEST_TAG" ]; then
echo "No existing tags with the specified prefix were found. Using initial tag number ${INITIAL_NUMBER}."
NEXT_TAG_NUMBER=$INITIAL_NUMBER
else
HIGHEST_TAG_NUMBER=$(echo "$HIGHEST_TAG" | grep -Eo '[0-9]+')
NEXT_TAG_NUMBER=$((HIGHEST_TAG_NUMBER + 1))
fi
fi

NEXT_TAG="${TAG_PREFIX}${NEXT_TAG_NUMBER}"
echo "Next tag: $NEXT_TAG"
echo "tag=$NEXT_TAG" >> $GITHUB_OUTPUT
echo "number=$NEXT_TAG_NUMBER" >> $GITHUB_OUTPUT

0 comments on commit dec28b9

Please sign in to comment.