Skip to content

Package Publishing

Package Publishing #68

name: "Package Publishing"
on:
workflow_dispatch:
inputs:
target:
type: choice
description: 'Target:'
required: true
options:
- MAJOR
- MINOR
- RELEASE
- NONE
default: 'MINOR'
pypi:
type: boolean
description: 'PyPI'
required: true
default: true
conda:
type: boolean
description: 'Conda'
required: true
default: true
jobs:
package_publishing:
name: Package Publishing
runs-on: ubuntu-latest
steps:
- name: Repository Checkout
uses: actions/checkout@v3
- name: Python Setup
uses: actions/setup-python@v4
with:
python-version: '3.11'
architecture: x64
- name: Version Bump
id: bump
env:
TARGET: ${{ github.event.inputs.target }}
PYPI: ${{ github.event.inputs.pypi }}
CONDA: ${{ github.event.inputs.conda }}
VERSION_REGEX: __version__ = '\d+\.\d+\.\d+'
VERSION_FILE: pydtmc/__init__.py
run: |
TARGET="${TARGET^^}"
PYPI="${PYPI^^}"
CONDA="${CONDA^^}"
if [[ "$PYPI" == "FALSE" ]] && [[ "$CONDA" == "FALSE" ]]; then
echo "INVALID INPUT (PYPI=FALSE && CONDA=FALSE)"
exit 1
fi
if [[ "$PYPI" == "TRUE" ]] && [[ "$TARGET" == "NONE" ]]; then
echo "INVALID INPUT (PYPI=TRUE && TARGET=NONE)"
exit 1
fi
if [[ "$PYPI" == "FALSE" ]] && [[ "$CONDA" == "TRUE" ]] && [[ "$TARGET" != "NONE" ]]; then
echo "INVALID INPUT (PYPI=FALSE && CONDA=TRUE && TARGET!=NONE)"
exit 1
fi
grep -P "$VERSION_REGEX" "$VERSION_FILE"
VERSION=$(grep -P "$VERSION_REGEX" "$VERSION_FILE" | cut -d "'" -f2)
VERSION_MAJOR=$(echo "$VERSION" | cut -d "." -f1)
VERSION_MINOR=$(echo "$VERSION" | cut -d "." -f2)
VERSION_RELEASE=$(echo "$VERSION" | cut -d "." -f3)
if [[ "$PYPI" == "TRUE" ]]; then
if [[ "$TARGET" == "MAJOR" ]]; then
VERSION_MAJOR=$(($VERSION_MAJOR+1))
VERSION_MINOR=0
VERSION_RELEASE=0
elif [[ "$TARGET" == "MINOR" ]]; then
VERSION_MINOR=$(($VERSION_MINOR+1))
VERSION_RELEASE=0
elif [[ "$TARGET" == "RELEASE" ]]; then
VERSION_RELEASE=$(($VERSION_RELEASE+1))
fi
fi
VERSION=$(printf "%d.%d.%d" $VERSION_MAJOR $VERSION_MINOR $VERSION_RELEASE 2>/dev/null)
PATTERN=$(grep -Poh "$VERSION_REGEX" "$VERSION_FILE")
sed -i "s/$PATTERN/__version__ = '$VERSION'/g" "$VERSION_FILE"
grep -P "$VERSION_REGEX" "$VERSION_FILE"
echo "target=$TARGET" >> $GITHUB_OUTPUT
echo "pypi=$PYPI" >> $GITHUB_OUTPUT
echo "conda=$CONDA" >> $GITHUB_OUTPUT
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Cache
id: pypi_cache
env:
PYPI: ${{ steps.bump.outputs.pypi }}
if: ${{ env.PYPI == 'TRUE' }}
uses: actions/cache@v3
with:
path: ${{ env.pythonLocation }}
key: ${{ runner.os }}-PP-${{ env.pythonLocation }}-${{ hashFiles('setup.cfg') }}
- name: Dependencies Installation
env:
PYPI: ${{ steps.bump.outputs.pypi }}
if: ${{ env.PYPI == 'TRUE' }} && steps.pypi_cache.outputs.cache-hit != 'true'
run: |
pip install --quiet --upgrade pip
pip install --quiet --upgrade -r requirements.txt
pip install --quiet --upgrade -e .[build]
- name: PyPI Publishing
env:
PYPI: ${{ steps.bump.outputs.pypi }}
TWINE_REPOSITORY_URL: https://upload.pypi.org/legacy/
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
TWINE_NON_INTERACTIVE: true
if: ${{ env.PYPI == 'TRUE' }}
run: |
python setup.py sdist bdist_wheel
twine check --strict dist/*
twine upload dist/*
- name: Conda Publishing
env:
TARGET: ${{ steps.bump.outputs.target }}
PYPI: ${{ steps.bump.outputs.pypi }}
CONDA: ${{ steps.bump.outputs.conda }}
VERSION: ${{ steps.bump.outputs.version }}
META_REGEX: 'sha256: [A-Fa-f0-9]{64}'
META_FILE: meta.yaml
ANACONDA_API_TOKEN: ${{ secrets.ANACONDA_TOKEN }}
if: ${{ env.CONDA == 'TRUE' }}
run: |
if [[ "$PYPI" == "TRUE" ]]; then
sleep 900
fi
conda clean --all
conda update --quiet conda
conda install --quiet conda-verify conda-build anaconda-client
wget "https://pypi.io/packages/source/P/PyDTMC/PyDTMC-${VERSION}.tar.gz" -O "/tmp/PyDTMC-${VERSION}.tar.gz"
SHA256=$(openssl sha256 "/tmp/PyDTMC-${VERSION}.tar.gz" | awk '{print $2}')
grep -P "$META_REGEX" "$META_FILE"
PATTERN=$(grep -Poh "$META_REGEX" "$META_FILE")
sed -i "s/$PATTERN/sha256: $SHA256/g" "$META_FILE"
grep -P "$SHA256" "$META_FILE"
conda config --set always_yes yes
conda config --set anaconda_upload yes
conda config --set changeps1 no
conda build --channel conda-forge --override-channels --use-local --output-folder /tmp/conda-build .
- name: Version Commit
env:
TARGET: ${{ steps.bump.outputs.target }}
PYPI: ${{ steps.bump.outputs.pypi }}
VERSION: ${{ steps.bump.outputs.version }}
if: ${{ env.TARGET != 'NONE' }}
run: |
git remote set-url origin "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}"
git config --local user.name "${{ secrets.BOT_USERNAME }}"
git config --local user.email "${{ secrets.BOT_EMAIL }}"
git add --a
git commit -m "Release $VERSION"
git push