publish action #1
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: Publish from Main | |
on: | |
push: | |
branches: [main] | |
jobs: | |
publish: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out repository | |
uses: actions/checkout@v3 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: "3.11" | |
- name: Install dependencies | |
run: pip install build twine tomli | |
- name: Extract version from pyproject.toml | |
id: get_version | |
run: | | |
python -c "import tomli; print(tomli.loads(open('pyproject.toml', 'rb').read())['project']['version'])" > version.txt | |
echo "version=$(cat version.txt)" >> $GITHUB_OUTPUT | |
- name: Determine Tag Name | |
id: determine_tag | |
run: | | |
version="${{ steps.get_version.outputs.version }}" | |
tag_name="v${version}" | |
echo "tag_name=$tag_name" >> $GITHUB_OUTPUT | |
- name: Check if tag exists on GitHub | |
id: check_tag | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const tagName = "${{ steps.determine_tag.outputs.tag_name }}"; | |
const { data: tags } = await github.request('GET /repos/{owner}/{repo}/tags', { | |
owner: context.repo.owner, | |
repo: context.repo.repo | |
}); | |
const tagExists = tags.some(tag => tag.name === tagName); | |
return { tagExists } | |
- name: Fail if tag already exists | |
if: steps.check_tag.outputs.tagExists == 'true' | |
run: | | |
echo "Tag ${{ steps.determine_tag.outputs.tag_name }} already exists for this version. Cannot create a new release." | |
exit 1 | |
- name: Build package | |
run: python -m build | |
- name: Create GitHub Release | |
if: steps.check_tag.outputs.tagExists == 'false' | |
uses: actions/create-release@v1 | |
with: | |
tag_name: ${{ steps.determine_tag.outputs.tag_name }} | |
release_name: ${{ steps.determine_tag.outputs.tag_name }} | |
draft: false | |
prerelease: false | |
body: "Release for version ${{ steps.determine_tag.outputs.tag_name }}" | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Publish to PyPI | |
if: steps.check_tag.outputs.tagExists == 'false' | |
env: | |
TWINE_USERNAME: __token__ | |
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} | |
run: twine upload dist/* |