Update lf-build-linux-x86_64.yml #125
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: 'Linuxfabrik: Semantic Auto-Versioning' | |
on: | |
push: | |
branches: | |
- 'main' | |
jobs: | |
versioning: | |
runs-on: 'ubuntu-24.04' | |
outputs: | |
version: "${{ steps.version_update.outputs.new_version }}" | |
steps: | |
- name: 'Checkout Repository' | |
uses: 'actions/checkout@v4' | |
with: | |
fetch-depth: 0 # Required for full commit history | |
- name: 'Find and Download Latest Version Artifact' | |
run: | | |
GH_API_URL="https://api.github.com/repos/${{ github.repository_owner }}/${{ github.event.repository.name }}/actions/artifacts" | |
ARTIFACT_NAME="version" | |
echo "Fetching latest artifact..." | |
# Fetch all artifacts and filter by name | |
ARTIFACT_INFO=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" "$GH_API_URL" | \ | |
jq -r ".artifacts | map(select(.name == \"$ARTIFACT_NAME\")) | sort_by(.created_at) | last // {}") | |
# Extract the download URL | |
ARTIFACT_URL=$(echo "$ARTIFACT_INFO" | jq -r ".archive_download_url // empty") | |
if [[ -z "$ARTIFACT_URL" ]]; then | |
echo "No previous version artifact found. Defaulting to 1.0.0.0" | |
echo "previous_version=1.0.0.0" >> $GITHUB_ENV | |
else | |
echo "Downloading artifact from: $ARTIFACT_URL" | |
curl -L -o version.zip -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "$ARTIFACT_URL" | |
mkdir -p version_artifact | |
unzip -o version.zip -d version_artifact | |
PREVIOUS_VERSION=$(cat version_artifact/version.txt) | |
echo "Previous version found: $PREVIOUS_VERSION" | |
echo "previous_version=$PREVIOUS_VERSION" >> $GITHUB_ENV | |
fi | |
- name: 'Create Version Bump Script' | |
run: | | |
cat <<EOF > bump_version.py | |
import os | |
import subprocess | |
import re | |
# Get previous version | |
previous_version = os.getenv("previous_version", "1.0.0.0").strip() | |
# Parse version into components | |
parts = previous_version.split(".") | |
while len(parts) < 4: | |
parts.append("0") # Ensure at least major.minor.patch.revision | |
major, minor, patch, revision = map(int, parts) | |
# Get second-to-last pushed commit (avoid failing on first commit) | |
try: | |
last_pushed_commit = subprocess.run( | |
["git", "rev-list", "--skip=1", "--max-count=1", "origin/main"], | |
capture_output=True, | |
text=True, | |
check=True | |
).stdout.strip() | |
except subprocess.CalledProcessError: | |
last_pushed_commit = None # No second-to-last commit exists | |
# Get commit messages since last push | |
commit_messages = [] | |
if last_pushed_commit: | |
commit_messages = subprocess.run( | |
["git", "log", f"{last_pushed_commit}..HEAD", "--pretty=%s", "--no-merges"], | |
capture_output=True, | |
text=True | |
).stdout.splitlines() | |
# Determine version bump type | |
bump_type = "revision" | |
for msg in commit_messages: | |
if "BREAKING CHANGE" in msg or re.search(r"(feat|fix)!:", msg): | |
bump_type = "major" | |
break | |
elif msg.startswith("feat:"): | |
bump_type = "minor" | |
elif msg.startswith("fix:") and bump_type != "minor": | |
bump_type = "patch" | |
# Apply version bump | |
if bump_type == "major": | |
major += 1 | |
minor, patch, revision = 0, 0, 0 | |
elif bump_type == "minor": | |
minor += 1 | |
patch, revision = 0, 0 | |
elif bump_type == "patch": | |
patch += 1 | |
revision = 0 | |
else: # revision bump | |
revision += 1 | |
new_version = f"{major}.{minor}.{patch}.{revision}" | |
# Output new version | |
with open("version.txt", "w") as f: | |
f.write(new_version) | |
print(new_version) | |
EOF | |
- name: 'Run Version Bump Script' | |
run: | | |
NEW_VERSION=$(python3 bump_version.py) | |
echo "new_version=$NEW_VERSION" >> $GITHUB_ENV | |
echo "$NEW_VERSION" > version.txt | |
echo "New version: $NEW_VERSION" | |
- name: 'Upload New Version as Artifact' | |
uses: 'actions/upload-artifact@v4' | |
with: | |
name: 'version' | |
path: 'version.txt' |