Skip to content

Publish to PyPI and Docker Hub #57

Publish to PyPI and Docker Hub

Publish to PyPI and Docker Hub #57

Workflow file for this run

name: Publish to PyPI and Docker Hub
on:
push:
paths:
- 'setup.py' # Only trigger when setup.py is changed
branches:
- main # Adjust to the branches you want to trigger releases
workflow_dispatch:
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.9' # Use the Python version you are targeting
- name: Extract version from setup.py
id: extract_version
run: |
VERSION=$(python -c "import re; version_line = next(line for line in open('setup.py') if line.strip().startswith('version=')); print(re.search(r'version=[\"\\']([^\"\\']+)[\"\\']', version_line).group(1))")
echo "VERSION=$VERSION" >> $GITHUB_ENV
shell: bash
- name: Debug - Print setup.py
run: cat setup.py
- name: Get previous version
id: previous_version
run: |
echo "Finding previous version..."
git log -p -1 -- setup.py || echo "git log command failed"
PREV_VERSION=$(git log -p -1 -- setup.py | grep -oP '(?<=version=\").+?(?=\")')
if [ -z "$PREV_VERSION" ]; then
echo "No previous version found, using initial commit"
PREV_VERSION=$(git rev-list --max-parents=0 HEAD)
fi
echo "PREV_VERSION=${PREV_VERSION}" >> $GITHUB_ENV
echo "Previous version: ${PREV_VERSION}"
shell: bash
- name: Generate changelog
id: changelog
run: |
PREV_VERSION=${{ env.PREVIOUS_VERSION }}
CURRENT_VERSION=${{ env.VERSION }}
echo "Generating changelog from $PREV_VERSION to $CURRENT_VERSION"
git log $PREV_VERSION..HEAD --pretty=format:"- %s" > changelog.txt
cat changelog.txt
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine importlib_metadata==4.8.1 # Use a specific version of importlib_metadata
- name: Build package
run: python setup.py sdist bdist_wheel
- name: Publish to Test PyPI
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.TEST_PYPI_TOKEN }}
run: |
twine check dist/*
twine upload --repository-url https://test.pypi.org/legacy/ dist/* --verbose
- name: Verify installation from Test PyPI
run: |
pip install --index-url https://test.pypi.org/simple/ uglypy --extra-index-url https://pypi.org/simple
- name: Publish package to PyPI
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
twine upload dist/*
- name: Create GitHub Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
with:
tag_name: v${{ env.VERSION }}
release_name: Release ${{ env.VERSION }}
body: |
## Changes in this Release
$(cat changelog.txt)
draft: false
prerelease: false
docker_deploy:
runs-on: ubuntu-latest
needs: release # Ensures this job runs after the 'release' job
steps:
- name: Check out code
uses: actions/checkout@v3
- name: Extract version from setup.py
id: extract_version
run: |
VERSION=$(python -c "import re; version_line = next(line for line in open('setup.py') if line.strip().startswith('version=')); print(re.search(r'version=[\"\\']([^\"\\']+)[\"\\']', version_line).group(1))")
echo "VERSION=$VERSION" >> $GITHUB_ENV
shell: bash
- name: Build Docker image
run: |
docker build -t fabriziosalmi/uglyfeed:latest .
- name: Push Docker image
env:
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_HUB_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
run: |
echo $DOCKER_HUB_PASSWORD | docker login --username $DOCKER_HUB_USERNAME --password-stdin
docker tag fabriziosalmi/uglyfeed:latest fabriziosalmi/uglyfeed:${{ env.VERSION }}
docker push fabriziosalmi/uglyfeed:${{ env.VERSION }}
docker push fabriziosalmi/uglyfeed:latest