Skip to content
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

use the library version number to publish to pypi, create a git tag and define a github release #688

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
48 changes: 41 additions & 7 deletions .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ jobs:
with:
python-version: '3.10'

- name: Gets the tag version
id: context
run: |
echo ::set-output name=TAG_VERSION::${GITHUB_REF#refs/tags/}

- name: Setup the Python Environment by installing Poetry
uses: ./.github/actions/setup-python-build-env

Expand All @@ -28,12 +23,51 @@ jobs:
run: make install && make tests

- name: Poetry bump version, build and publish
id: build
shell: bash
run: |
proj_version=$(poetry version -s)
if [ $proj_version != $TAG_VERSION ]; then echo "Version $proj_version, defined in pyproject.toml, does not match TAG $TAG_VERSION of this release"; exit 3; fi
echo ::set-output name=PROJ_VERSION::$proj_version
poetry update
Copy link
Contributor

Choose a reason for hiding this comment

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

Dependencies should not be updated blindly at release without testing ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In addition to the code quality checks being run on every pull request, it is run as the second step in the release process: make install && make tests

- name: Code Quality Check

Or is there a different set of testing that you think should occur before release?

Copy link
Contributor

Choose a reason for hiding this comment

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

Running poetry update at release is a never ever do that thing. The dependencies version lock file must not differ from the one in the repo ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thank you for the careful review! i misread your comment originally.

I had reused that section from the current one on master; I should have scrutinized it better. now updated.

poetry publish --build -u __token__ -p $PYPI_TOKEN
env:
TAG_VERSION: ${{ steps.context.outputs.TAG_VERSION }}
PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}

- name: Check if Tag exists 🔍
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the workflow is triggered at tag push, the tag is always here. Why do you want to add these 2 steps?

The sanity check should be that pyproject.toml version match the tag version. And stop the workflow if not by explicitly setting a chain of id: and needs: at each different steps. The version match can even be the first standalone step.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of triggering on tag push, I just changed it to triggering on workflow_dispatch; this should allow the releaser to trigger the workflow from the UI or github cli and it will generate and apply the tags all based on the version in pyproject.toml. This should make sure that the tag, the github release and pypi release all use the same version number.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then if a tag already exists, the release workflow must fail hard at the beginning: version not bumped correctly and version already released.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

id: check_tag
run: |
echo "Checking for tag: $PROJ_TAG"
if git rev-parse "refs/tags/$PROJ_TAG" >/dev/null 2>&1
then
echo "Tag $PROJ_TAG already exists."
echo "tag_exists=true" >> $GITHUB_OUTPUT
exit 0
else
echo "Tag $PROJ_TAG does not exist."
echo "tag_exists=false" >> $GITHUB_OUTPUT
fi
env:
PROJ_TAG: v${{ steps.build.outputs.PROJ_VERSION }}

- name: Create and Push Tag 🏷️
if: steps.check_tag.outputs.tag_exists == 'false'
run: |
TAG="v${{ steps.extract_version.outputs.version }}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
TAG="v${{ steps.extract_version.outputs.version }}"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

git tag $TAG
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }}
git push origin $TAG
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
git push origin $TAG
git push origin $PROJ_TAG

Write permission with the default GH token will also need to be set in the workflow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

echo "tag_created=true" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PROJ_TAG: v${{ steps.build.outputs.PROJ_VERSION }}

- name: Create a GitHub Release
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PROJ_TAG: v${{ steps.build.outputs.PROJ_VERSION }}
with:
generate_release_notes: true
tag_name: $PROJ_TAG