diff --git a/CHANGELOG.md b/CHANGELOG.md index 7abbf69..d1afa70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/next-repository-tag-number/Dockerfile b/next-repository-tag-number/Dockerfile new file mode 100644 index 0000000..f7fc0fc --- /dev/null +++ b/next-repository-tag-number/Dockerfile @@ -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"] diff --git a/next-repository-tag-number/action.yml b/next-repository-tag-number/action.yml new file mode 100644 index 0000000..4b045e9 --- /dev/null +++ b/next-repository-tag-number/action.yml @@ -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 }} diff --git a/next-repository-tag-number/get-next-tag-number.sh b/next-repository-tag-number/get-next-tag-number.sh new file mode 100755 index 0000000..7ad41ea --- /dev/null +++ b/next-repository-tag-number/get-next-tag-number.sh @@ -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