Skip to content

PyPI Release

PyPI Release #1

# This workflow will update the latest version with minor release and upload a package to PyPi
name: PyPI Release
on:
workflow_dispatch:
inputs:
package:
description: Package
required: false
default: dbt-postgres-python
type: choice
options:
- dbt-postgres-python
version:
description: Version
required: false
default: patch
type: choice
options:
- prerelease
- patch
- minor
- major
publish_from_any_branch:
description: Publish from any branch
required: false
default: false
type: boolean
jobs:
deploy:
# Run only for `release` branch or if marked as acceptable
if: github.ref == 'refs/heads/release' || inputs.publish_from_any_branch
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-python@v5
with:
python-version: "3.8"
- name: Package setup
run: |
if [[ ${{ inputs.package }} == dbt-postgres-python ]]
then
echo "PACKAGE_DIR=projects/adapter" >> $GITHUB_ENV
echo "TAG_PREFIX=adapter-v" >> $GITHUB_ENV
fi
- name: Install poetry
shell: bash
run: pip install poetry=="1.5.0"
- name: Bump to publishing version
working-directory: ${{ env.PACKAGE_DIR }}
shell: bash
run: |
VERSION_TYPE="${{ inputs.version }}"
if [[ ! "$VERSION_TYPE" == prerelease ]]
then
# Don't bump for prereleases, publish them
poetry version $VERSION_TYPE
fi
# version has format '0.4.1'
publishing_version=$(poetry version -s)
echo "publishing_version=$publishing_version" >> $GITHUB_ENV
# tag has format 'v0.4.0' (note the 'v')
prev_version_tag=$(git describe --tags --match '${{ env.TAG_PREFIX }}*' --abbrev=0)
echo "prev_version_tag=$prev_version_tag" >> $GITHUB_ENV
# set __version__.py files in src directory
VERSION_FILE_CONTENT="version = '$publishing_version'"
VERSION_FILES=$(find src -name __version__.py)
for FILE_PATH in $VERSION_FILES; do
echo $VERSION_FILE_CONTENT > $FILE_PATH
done
- name: Build package
working-directory: ${{ env.PACKAGE_DIR }}
shell: bash
run: poetry build
- name: Generate a changelog
uses: orhun/[email protected]
id: git-cliff
with:
config: ${{ env.PACKAGE_DIR }}/cliff.toml
args: ${{ env.prev_version_tag }}..${{ github.ref }}
env:
OUTPUT: CHANGES.md
- name: Set the release body
id: release
shell: bash
run: |
r=$(cat ${{ steps.git-cliff.outputs.changelog }})
r="${r//'%'/'%25'}" # Multiline escape sequences for %
r="${r//$'\n'/'%0A'}" # Multiline escape sequences for '\n'
r="${r//$'\r'/'%0D'}" # Multiline escape sequences for '\r'
echo "::set-output name=RELEASE_BODY::$r"
- name: Publish GitHub
uses: softprops/action-gh-release@v2
with:
name: ${{ inputs.package }} ${{ env.publishing_version }}
body: ${{ steps.release.outputs.RELEASE_BODY }}
tag_name: ${{ env.TAG_PREFIX }}${{ env.publishing_version }}
files: |
${{ env.PACKAGE_DIR }}/dist/${{ inputs.package }}-${{ env.publishing_version }}-py3-none-any.whl
${{ env.PACKAGE_DIR }}/dist/${{ inputs.package }}-${{ env.publishing_version }}.tar.gz
- name: Publish PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: ${{ env.PACKAGE_DIR }}/dist/
- name: Clear the repo untracked files
run: git clean -fxd
- name: Bump repo version
working-directory: ${{ env.PACKAGE_DIR }}
run: |
poetry version prerelease
# set __version__.py files in src directory
VERSION=$(poetry version -s)
VERSION_FILE_CONTENT="version = '$VERSION'"
VERSION_FILES=$(find src -name __version__.py)
for FILE_PATH in $VERSION_FILES; do
echo $VERSION_FILE_CONTENT > $FILE_PATH
done
- name: Create Pull Request
uses: peter-evans/create-pull-request@v4
if: inputs.package == 'dbt-postgres-python'
with:
branch: bump-${{ inputs.package }}-${{ env.publishing_version }}
delete-branch: true
title: Bump the pyproject.toml ${{ inputs.package }} version
base: main
token: ${{ secrets.RELEASER_GITHUB_PAT }}
body: ${{ steps.release.outputs.RELEASE_BODY }}