|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# ------------------------------------------------------------------------------ |
| 5 | +# This script determines the version numbers used in builds and releases. |
| 6 | +# |
| 7 | +# It sets three environment variables: |
| 8 | +# - PKG_VER → The package version (semantic version). |
| 9 | +# - BUILD_VER → The build identifier (may include build number). |
| 10 | +# - PYPI_VER → A PyPI-compatible version string for publishing. |
| 11 | +# |
| 12 | +# Behavior: |
| 13 | +# - On a tagged commit (e.g. "v1.2.3"), it uses that tag as the version. |
| 14 | +# - On an untagged commit, it generates a "next dev version" by: |
| 15 | +# • Taking the latest tag (default v0.0.0 if none). |
| 16 | +# • Incrementing the minor version. |
| 17 | +# • Appending "+<run number>". |
| 18 | +# - PYPI_VER is derived from BUILD_VER by replacing "+" with ".dev". |
| 19 | +# |
| 20 | +# This ensures that: |
| 21 | +# - Release builds (tags) get clean versions like "1.2.3". |
| 22 | +# - Development builds get versions like "1.3.0+45" (PyPI: "1.3.0.dev45"). |
| 23 | +# ------------------------------------------------------------------------------ |
| 24 | + |
| 25 | +if [[ "$GITHUB_REF" == refs/tags/* ]]; then |
| 26 | + # ------------------------------------------------------------- |
| 27 | + # Case 1: This build is triggered by a Git tag |
| 28 | + # ------------------------------------------------------------- |
| 29 | + # Extract the tag name (strip "refs/tags/") |
| 30 | + tag="${GITHUB_REF#refs/tags/}" |
| 31 | + # Remove leading "v" if present (e.g. "v1.2.3" → "1.2.3") |
| 32 | + export PKG_VER="${tag#v}" |
| 33 | + # For tagged releases, BUILD_VER is the same as PKG_VER |
| 34 | + export BUILD_VER="$PKG_VER" |
| 35 | +else |
| 36 | + # ------------------------------------------------------------- |
| 37 | + # Case 2: This is not a tagged build (e.g. main branch commit) |
| 38 | + # ------------------------------------------------------------- |
| 39 | + # Get the latest tag, or fall back to "v0.0.0" if none exist |
| 40 | + cv=$(git describe --abbrev=0 2>/dev/null || echo "v0.0.0") |
| 41 | + # Remove leading "v" if present |
| 42 | + cv=${cv#v} |
| 43 | + |
| 44 | + # Split into major/minor components |
| 45 | + major=$(echo "$cv" | cut -d. -f1) |
| 46 | + minor=$(echo "$cv" | cut -d. -f2) |
| 47 | + |
| 48 | + # Increment the minor version (e.g. "1.2" → "1.3") |
| 49 | + minor=$((minor + 1)) |
| 50 | + |
| 51 | + # Construct the package version: <major>.<minor>.0 |
| 52 | + export PKG_VER="${major}.${minor}.0" |
| 53 | + |
| 54 | + # Append the GitHub Actions run number for uniqueness |
| 55 | + export BUILD_VER="${PKG_VER}+$((GITHUB_RUN_NUMBER + 500))" |
| 56 | +fi |
| 57 | + |
| 58 | +# ------------------------------------------------------------- |
| 59 | +# Derive PyPI-compatible version |
| 60 | +# PyPI does not accept "+" in versions, so replace with ".dev" |
| 61 | +# Example: "1.3.0+45" → "1.3.0.dev45" |
| 62 | +# ------------------------------------------------------------- |
| 63 | +export PYPI_VER="${BUILD_VER/+/.dev}" |
| 64 | + |
| 65 | +# Print values for debugging in logs |
| 66 | +echo "PKG_VER=$PKG_VER" |
| 67 | +echo "BUILD_VER=$BUILD_VER" |
| 68 | +echo "PYPI_VER=$PYPI_VER" |
| 69 | + |
| 70 | +# Export values as environment variables |
| 71 | +echo "PKG_VER=$PKG_VER" >> $GITHUB_ENV |
| 72 | +echo "BUILD_VER=$BUILD_VER" >> $GITHUB_ENV |
| 73 | +echo "PYPI_VER=$PYPI_VER" >> $GITHUB_ENV |
0 commit comments