Auto-Tag on Version Change workflow (xai-org/xai-sdk-python)
The Auto-Tag on Version Change workflow from xai-org/xai-sdk-python, explained and optimized by Latchkey.
D
CI health: D - needs work
Run this on Latchkey for self-healing, caching, and up to 58% lower cost.
Grade your own workflow free or run it on Latchkey →What it does
This is the Auto-Tag on Version Change workflow from the xai-org/xai-sdk-python repository, a real project running GitHub Actions. It is shown here with attribution under its Apache-2.0 license.
Below, Latchkey shows a faster, safer version produced by its optimization engine.
The workflow
workflow (.yml)
name: Auto-Tag on Version Change
on:
push:
branches: [main]
paths:
- src/xai_sdk/__about__.py
jobs:
auto-tag:
runs-on: xai-sdk-python-runner
permissions:
contents: write
steps:
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
with:
fetch-depth: 0 # Fetch all history to compare commits
- name: Set up Python
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
with:
python-version: "3.10"
- name: Install uv
uses: astral-sh/setup-uv@c7f87aa956e4c323abf06d5dec078e358f6b4d04 # v6.0.0
with:
version: "0.7.3"
- name: Install dependencies
run: uv sync --locked
- name: Check version update and create tag
env:
GH_TOKEN: ${{ github.token }}
run: |
# Get the new version (from the current __about__.py, already checked out at HEAD)
export NEW_VERSION=$(uv run hatch version)
echo "New version: $NEW_VERSION"
# Get the previous version (from the previous commit's __about__.py)
PREVIOUS_COMMIT=$(git rev-parse origin/main^)
git checkout $PREVIOUS_COMMIT -- src/xai_sdk/__about__.py
export PREVIOUS_VERSION=$(uv run hatch version)
echo "Previous version: $PREVIOUS_VERSION"
# Restore the modified __about__.py to the latest commit
git restore src/xai_sdk/__about__.py
# Compare versions semantically to ensure the new version is greater
uv run python -c "
import os
from packaging import version
previous_version = os.environ['PREVIOUS_VERSION']
new_version = os.environ['NEW_VERSION']
prev_ver = version.parse(previous_version)
new_ver = version.parse(new_version)
if new_ver <= prev_ver:
print(f'Error: New version {new_version} is not greater than previous version {previous_version}')
exit(1)
else:
print('Version bump is valid.')
"
# If all checks pass, create and push the tag, assuming it doesn't already exist
if [ -n "$(git tag -l "v${NEW_VERSION}")" ]; then
echo "Tag v${NEW_VERSION} already exists, skipping."
exit 0
fi
git config --global user.email "actions@github.com"
git config --global user.name "GitHub Actions"
git tag -a v${NEW_VERSION} -m "Release version ${NEW_VERSION} of the xAI Python SDK"
git push origin v${NEW_VERSION}
The same workflow, on Latchkey
Estimated ~20% faster on cache hits, plus fewer wasted runs and a safer supply chain. Added and changed lines are highlighted.
name: Auto-Tag on Version Change on: push: branches: [main] paths: - src/xai_sdk/__about__.py concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: auto-tag: timeout-minutes: 30 runs-on: xai-sdk-python-runner permissions: contents: write steps: - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 with: fetch-depth: 0 # Fetch all history to compare commits - name: Set up Python uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: cache: 'pip' python-version: "3.10" - name: Install uv uses: astral-sh/setup-uv@c7f87aa956e4c323abf06d5dec078e358f6b4d04 # v6.0.0 with: version: "0.7.3" - name: Install dependencies run: uv sync --locked - name: Check version update and create tag env: GH_TOKEN: ${{ github.token }} run: | # Get the new version (from the current __about__.py, already checked out at HEAD) export NEW_VERSION=$(uv run hatch version) echo "New version: $NEW_VERSION" # Get the previous version (from the previous commit's __about__.py) PREVIOUS_COMMIT=$(git rev-parse origin/main^) git checkout $PREVIOUS_COMMIT -- src/xai_sdk/__about__.py export PREVIOUS_VERSION=$(uv run hatch version) echo "Previous version: $PREVIOUS_VERSION" # Restore the modified __about__.py to the latest commit git restore src/xai_sdk/__about__.py # Compare versions semantically to ensure the new version is greater uv run python -c " import os from packaging import version previous_version = os.environ['PREVIOUS_VERSION'] new_version = os.environ['NEW_VERSION'] prev_ver = version.parse(previous_version) new_ver = version.parse(new_version) if new_ver <= prev_ver: print(f'Error: New version {new_version} is not greater than previous version {previous_version}') exit(1) else: print('Version bump is valid.') " # If all checks pass, create and push the tag, assuming it doesn't already exist if [ -n "$(git tag -l "v${NEW_VERSION}")" ]; then echo "Tag v${NEW_VERSION} already exists, skipping." exit 0 fi git config --global user.email "actions@github.com" git config --global user.name "GitHub Actions" git tag -a v${NEW_VERSION} -m "Release version ${NEW_VERSION} of the xAI Python SDK" git push origin v${NEW_VERSION}
What changed
- Cancel superseded runs when a branch or PR gets a newer push.
- Cache dependency installs on the setup step so they are served from cache.
- Add a job timeout so a hung step cannot burn hours of runner time.
This workflow runs 1 job per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.