This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Calculate Next Semver Based on All Releases | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- main | |
jobs: | |
calculate-version: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Get all releases | |
id: get_all_releases | |
run: | | |
# Fetch all releases and check for API errors | |
RESPONSE=$(curl -s -o response.json -w "%{http_code}" "https://api.github.com/repos/${{ github.repository }}/releases?per_page=100") | |
if [ "$RESPONSE" -ne 200 ]; then | |
echo "Failed to fetch releases. HTTP status: $RESPONSE" | |
cat response.json | |
exit 1 | |
fi | |
# Extract and sort tags | |
ALL_TAGS=$(jq -r '.[].tag_name' response.json | grep -E '^v[0-9]+\.[0-9]+(-[0-9]+)?$' | sed 's/-.*//' | sort -V | tail -n 1) | |
# Exit if no tags were found | |
if [ -z "$ALL_TAGS" ]; then | |
echo "No valid tags found." | |
exit 1 | |
fi | |
echo "::set-output name=tag::$ALL_TAGS" | |
- name: Calculate next semver | |
id: calculate_next_version | |
run: | | |
LATEST_VERSION=${{ steps.get_all_releases.outputs.tag }} | |
# Strip 'v' prefix and split into major.minor | |
LATEST_VERSION=${LATEST_VERSION//v/} | |
IFS='.' read -r -a VERSION_PARTS <<< "$LATEST_VERSION" | |
MAJOR=${VERSION_PARTS[0]} | |
MINOR=${VERSION_PARTS[1]} | |
# Increment the minor version | |
MINOR=$((MINOR + 1)) | |
# Construct the next version as v<major>.<minor> | |
NEXT_VERSION="v${MAJOR}.${MINOR}" | |
echo "Next version: $NEXT_VERSION" | |
echo "::set-output name=next_version::$NEXT_VERSION" | |
# access as: ${{ steps.calculate_next_version.outputs.next_version }} |