Skip to content

Commit dec28b9

Browse files
committed
Add a next-repository-tag-number action
1 parent e97200f commit dec28b9

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ All notable changes to this project will be documented in this file.
99
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
1010
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1111

12+
## [Unreleased]
13+
14+
### Added
15+
16+
- Initial release of the next-repository-tag-number action.
17+
1218
## [1.0.1] - 2024-11-04
1319

1420
### Fixed

next-repository-tag-number/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM alpine:latest
2+
RUN apk add --update --no-cache --no-progress curl jq
3+
COPY get-next-tag-number.sh /get-next-tag-number.sh
4+
ENTRYPOINT ["/get-next-tag-number.sh"]

next-repository-tag-number/action.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Next Repository Tag Number
2+
description: Get the next tag number for a repository.
3+
4+
inputs:
5+
repository_url:
6+
description: The URL of the OCI-compatible repository.
7+
required: true
8+
bearer_token:
9+
description: Optional bearer token for repository authentication.
10+
required: false
11+
initial_number:
12+
description: Starting tag number if no tags exist.
13+
default: '1'
14+
required: false
15+
tag_prefix:
16+
description: Prefix for the tags.
17+
default: 'build-'
18+
required: false
19+
20+
outputs:
21+
tag:
22+
description: The next tag.
23+
value: ${{ steps.next-tag.outputs.tag }}
24+
number:
25+
description: The next tag number.
26+
value: ${{ steps.next-tag.outputs.number }}
27+
28+
runs:
29+
using: docker
30+
image: Dockerfile
31+
args:
32+
- ${{ inputs.repository_url }}
33+
- ${{ inputs.bearer_token }}
34+
- ${{ inputs.initial_number }}
35+
- ${{ inputs.tag_prefix }}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env sh
2+
3+
REPOSITORY_URL=$1
4+
BEARER_TOKEN=$2
5+
INITIAL_NUMBER=${3:-1}
6+
TAG_PREFIX=${4:-"build-"}
7+
8+
if [ -n "$BEARER_TOKEN" ]; then
9+
AUTH_HEADER="-H \"Authorization: Bearer ${BEARER_TOKEN}\""
10+
else
11+
AUTH_HEADER=""
12+
fi
13+
14+
TAGS=$(curl -s $AUTH_HEADER "${REPOSITORY_URL}/tags/list" | jq -r '.tags[]' || echo "")
15+
16+
if [ -z "$TAGS" ]; then
17+
echo "No existing tags were found. Using initial tag number ${INITIAL_NUMBER}."
18+
NEXT_TAG_NUMBER=$INITIAL_NUMBER
19+
else
20+
HIGHEST_TAG=$(echo "$TAGS" | grep -Eo "${TAG_PREFIX}[0-9]+" | sort -V | tail -n 1)
21+
22+
if [ -z "$HIGHEST_TAG" ]; then
23+
echo "No existing tags with the specified prefix were found. Using initial tag number ${INITIAL_NUMBER}."
24+
NEXT_TAG_NUMBER=$INITIAL_NUMBER
25+
else
26+
HIGHEST_TAG_NUMBER=$(echo "$HIGHEST_TAG" | grep -Eo '[0-9]+')
27+
NEXT_TAG_NUMBER=$((HIGHEST_TAG_NUMBER + 1))
28+
fi
29+
fi
30+
31+
NEXT_TAG="${TAG_PREFIX}${NEXT_TAG_NUMBER}"
32+
echo "Next tag: $NEXT_TAG"
33+
echo "tag=$NEXT_TAG" >> $GITHUB_OUTPUT
34+
echo "number=$NEXT_TAG_NUMBER" >> $GITHUB_OUTPUT

0 commit comments

Comments
 (0)