Skip to content

Add logic to auto update release version in package after a release is cut #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,62 @@ jobs:
password: ${{ secrets.PYPI_PUBLIC_AUTH }}
whl: $(find dist -name '*.tar.gz')

update-main-version:
needs: build-and-publish
permissions:
contents: write
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: main
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.9"
- name: Install dependencies
run: pip install packaging
- name: Set Tag Version
id: set-tag-version
run: echo "tag_version=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT
- name: Set setup.py version
id: set-setup-version
run: echo "setup_version=$(grep -oP 'LAST_RELEASE_VERSION = Version\("\K[^"]+' setup.py)" >> $GITHUB_OUTPUT
- name: Check if version needs to be updated
id: check-version
run: |
TAG_VERSION=${{ steps.set-tag-version.outputs.tag_version }}
SETUP_VERSION=${{ steps.set-setup-version.outputs.setup_version }}
if [ "$(python -c "from packaging.version import Version; print(Version('$TAG_VERSION') > Version('$SETUP_VERSION'))")" = "True" ]; then
echo "Version needs to be updated."
echo "update_needed=true" >> $GITHUB_OUTPUT
else
echo "No update needed."
echo "update_needed=false" >> $GITHUB_OUTPUT
fi
- name: Generate GitHub App token
id: app-token
if: steps.check-version.outputs.update_needed == 'true'
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.GH_NM_REDHAT_AUTOMATION_APP_ID }}
private-key: ${{ secrets.GH_NM_REDHAT_AUTOMATION_APP_PRIVATE_KEY }}
- name: Update LAST_RELEASE_VERSION in setup.py
if: steps.check-version.outputs.update_needed == 'true'
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
run:
TAG_VERSION=${{ steps.set-tag-version.outputs.tag_version }}
SETUP_VERSION=${{ steps.set-setup-version.outputs.setup_version }}
sed -i "s/LAST_RELEASE_VERSION = Version(\"[^\"]*\")/LAST_RELEASE_VERSION = Version(\"$TAG_VERSION\")/" setup.py
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add setup.py
git commit -m "Update LAST_RELEASE_VERSION from $SETUP_VERSION to $TAG_VERSION"
git push origin main
Copy link
Preview

Copilot AI May 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The update job directly commits and pushes changes without handling concurrent updates, which might lead to merge conflicts. Consider adding a retry mechanism or merge strategy to address potential race conditions.

Suggested change
git push origin main
MAX_RETRIES=3
RETRY_COUNT=0
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
git pull --rebase origin main || exit 1
if git push origin main; then
echo "Push succeeded."
break
else
echo "Push failed. Retrying... ($((RETRY_COUNT+1))/$MAX_RETRIES)"
RETRY_COUNT=$((RETRY_COUNT+1))
sleep 5
fi
done
if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
echo "Push failed after $MAX_RETRIES attempts. Exiting."
exit 1
fi

Copilot uses AI. Check for mistakes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does seem a little fragile. Maybe instead have the workflow open a PR and tag some reviewers?


unit-tests:
runs-on: ubuntu-latest
strategy:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from setuptools import setup
from setuptools_git_versioning import count_since, get_branch, get_sha, get_tags

LAST_RELEASE_VERSION = Version("0.0.0")
LAST_RELEASE_VERSION = Version("0.2.1")
TAG_VERSION_PATTERN = re.compile(r"^v(\d+\.\d+\.\d+)$")


Expand Down