Skip to content

HATCH to PyPI by @Marceau-h #1

HATCH to PyPI by @Marceau-h

HATCH to PyPI by @Marceau-h #1

name: Hatch Build and Publish
run-name: HATCH to PyPI by @${{ github.actor }}
on:
workflow_dispatch:
push:
tags:
- 'v*.*.*'
permissions:
contents: write
packages: write
id-token: write
jobs:
get-ref:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get current branch
id: check_step
run: |
raw=$(git branch -r --contains ${{ github.ref }})
if [[ -z "$raw" ]]; then
raw=$(git branch -r --contains ${{ github.sha }})
fi
if [[ -z "$raw" ]]; then
echo "No branch found for ${{ github.ref }} or ${{ github.sha }}."
echo "This is probably a tag push without a branch."
exit 1
fi
BRANCH=${raw##*/}
BRANCH=$(echo $BRANCH | base64 -w 0)
echo "BRANCH=$BRANCH" >> $GITHUB_OUTPUT
echo "Branch is $BRANCH."
outputs:
branch-name: ${{ steps.check_step.outputs.BRANCH }}
get-tag:
runs-on: ubuntu-latest
needs: get-ref
steps:
- name: Decode ref
id: decode_ref
run: |
name=${{ needs.get-ref.outputs.branch-name }}
name=$(echo $name | base64 -d)
echo "NAME=$name" >> $GITHUB_ENV
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ env.NAME }}
fetch-depth: 0
- name: Set env
id: set-env
run: |
if [[ "${{ github.event_name }}" == "push" ]]; then
TAG=$(echo $GITHUB_REF | cut -d / -f 3)
echo "TAG=$TAG" >> $GITHUB_ENV
fi
outputs:
tag: ${{ env.TAG }}
deploy:
runs-on: ubuntu-latest
environment: release
needs:
- get-ref
- get-tag
steps:
- name: Decode ref
id: decode_ref
run: |
name=${{ needs.get-ref.outputs.branch-name }}
echo "Encoded ref is $name"
name=$(echo $name | base64 -d)
echo "Decoded ref is $name"
echo "REF=$name" >> $GITHUB_ENV
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ env.REF }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install hatch
- name: Version bump
if : ${{ github.event_name != 'push' }}
run: |
hatch version fix
echo "Version bumped"
- name: Version from tag
if : ${{ github.event_name == 'push' }}
run: |
TAG=${{ needs.get-tag.outputs.tag }}
echo "TAG=$TAG"
hatch version $TAG
# && echo "Version set to $TAG" || echo "Version already exists" && exit 1
- name: Build package
run: |
hatch build
# - name: Verify package
# run: |
# cd dist
# ls -Al
# tar -xvf TkSel-0.7.2.tar.gz
# ls -Al TkSel-0.7.2
# cat TkSel-0.7.2/TkSel/__about__.py
# cat TkSel-0.7.2/PKG-INFO
# rm -rf TkSel-0.7.2
- name: Publish package
uses: pypa/gh-action-pypi-publish@release/v1
# with:
# user: __token__
# password: ${{ secrets.PYPI_API_TOKEN }}
# repository-url: https://test.pypi.org/legacy/ # uncomment for test.pypi.org
- name: Update the about.py file
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "Update about.py file"
file_pattern: "TkSel/__about__.py"
status_options: '--untracked-files=no'
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ env.REF }}