Skip to content

Instantly share code, notes, and snippets.

@aoirint
Created November 27, 2022 14:48
Show Gist options
  • Save aoirint/f5c59f62ec34e3a0e43f9ab59f378909 to your computer and use it in GitHub Desktop.
Save aoirint/f5c59f62ec34e3a0e43f9ab59f378909 to your computer and use it in GitHub Desktop.
Release Python Package on GitHub Actions
name: Release Binary
on:
release:
types:
- created
env:
VERSION: ${{ github.event.release.tag_name != '' && github.event.release.tag_name || '0.0.0' }}
jobs:
build-binary:
strategy:
fail-fast: false
matrix:
include:
-
os: ubuntu-20.04
python_version: '3.8.14' # the latest version of 3.8.x
sed: 'sed'
tar: 'tar'
dist_artifact_name: binary-dist-linux-amd64
binary_name: liveinfo
asset_name: liveinfo-linux-amd64.tgz
-
os: windows-2019
python_version: '3.8.10' # the latest version of 3.8.x
sed: 'sed'
tar: 'tar'
dist_artifact_name: binary-dist-windows-amd64
binary_name: liveinfo.exe
asset_name: liveinfo-windows-amd64.zip
-
os: macos-11
python_version: '3.8.14' # the latest version of 3.8.x
sed: 'gsed'
tar: 'gtar'
dist_artifact_name: binary-dist-macos-amd64
binary_name: liveinfo
asset_name: liveinfo-macos-amd64.tgz
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Install gsed for macOS
if: startsWith(matrix.os, 'macos-')
shell: bash
run: brew install gnu-sed
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python_version }}
cache: 'pip'
cache-dependency-path: |
**/requirements-development.txt
- name: Install Dependencies
run: |
pip3 install -r requirements-development.txt
pip3 install wheel
- name: Replace version
shell: bash
run: |
"${{ matrix.sed }}" -i "s/__VERSION__ = '0.0.0'/__VERSION__ = '${{ env.VERSION }}'/" liveinfo/__init__.py
- name: Build Binary
run: pyinstaller --name liveinfo --onefile main.py
- name: Upload dist/* to Artifact
uses: actions/upload-artifact@v3
with:
name: ${{ matrix.dist_artifact_name }}
path: dist/*
upload-binary:
needs: [build-binary]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
-
dist_artifact_name: binary-dist-linux-amd64
binary_name: liveinfo
asset_name: liveinfo-linux-amd64.tgz
-
dist_artifact_name: binary-dist-windows-amd64
binary_name: liveinfo.exe
asset_name: liveinfo-windows-amd64.zip
-
dist_artifact_name: binary-dist-macos-amd64
binary_name: liveinfo
asset_name: liveinfo-macos-amd64.tgz
steps:
- name: Restore dist/ from Binary artifact
uses: actions/download-artifact@v3
with:
name: ${{ matrix.dist_artifact_name }}
path: ./dist
- name: Create release asset (.tgz)
if: endsWith(matrix.asset_name, '.tgz')
shell: bash
run: |
ASSET_NAME="${{ matrix.asset_name }}"
ASSET_NAME_STEM="${ASSET_NAME%.*}"
cd dist
mkdir "${ASSET_NAME_STEM}"
mv "${{ matrix.binary_name }}" "${ASSET_NAME_STEM}/"
tar czf "${{ matrix.asset_name }}" "${ASSET_NAME_STEM}/"
- name: Create release asset (.zip)
if: endsWith(matrix.asset_name, '.zip')
shell: bash
run: |
ASSET_NAME="${{ matrix.asset_name }}"
ASSET_NAME_STEM="${ASSET_NAME%.*}"
cd dist
mkdir "${ASSET_NAME_STEM}"
mv "${{ matrix.binary_name }}" "${ASSET_NAME_STEM}/"
zip -r "${{ matrix.asset_name }}" "${ASSET_NAME_STEM}/"
- name: Upload Binary to Release
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ env.VERSION }}
target_commitish: ${{ github.sha }}
files: dist/${{ matrix.asset_name }}
name: Release Python Package
on:
release:
types:
- created
env:
VERSION: ${{ github.event.release.tag_name != '' && github.event.release.tag_name || '0.0.0' }}
jobs:
build-python-package:
strategy:
fail-fast: false
matrix:
include:
-
os: ubuntu-20.04
python_version: '3.8.14' # the latest version of 3.8.x
sed: 'sed'
setup_py_subcommand: sdist
dist_artifact_name: python-package-dist-sdist
-
os: ubuntu-20.04
python_version: '3.8.14' # the latest version of 3.8.x
sed: 'sed'
setup_py_subcommand: bdist_wheel
dist_artifact_name: python-package-dist-bdist_wheel-linux-amd64
-
os: windows-2019
python_version: '3.8.10' # the latest version of 3.8.x
sed: 'sed'
setup_py_subcommand: bdist_wheel
dist_artifact_name: python-package-dist-bdist_wheel-windows-amd64
-
os: macos-11
python_version: '3.8.14' # the latest version of 3.8.x
sed: 'gsed'
setup_py_subcommand: bdist_wheel
dist_artifact_name: python-package-dist-bdist_wheel-macos-amd64
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Install gsed for macOS
if: startsWith(matrix.os, 'macos-')
shell: bash
run: brew install gnu-sed
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python_version }}
cache: 'pip'
cache-dependency-path: |
**/requirements-development.txt
- name: Install Dependencies
run: |
pip3 install -r requirements-development.txt
pip3 install wheel
- name: Replace version
shell: bash
run: |
"${{ matrix.sed }}" -i "s/__VERSION__ = '0.0.0'/__VERSION__ = '${{ env.VERSION }}'/" liveinfo/__init__.py
- name: Execute setup.py
run: python3 setup.py "${{ matrix.setup_py_subcommand }}"
- name: Upload dist/* to Artifact
uses: actions/upload-artifact@v3
with:
name: ${{ matrix.dist_artifact_name }}
path: dist/*
upload-python-package:
needs: [build-python-package]
runs-on: ubuntu-latest
steps:
- name: Restore dist/ from Python Package artifact python-package-dist-sdist
uses: actions/download-artifact@v3
with:
name: python-package-dist-sdist
path: ./dist
- name: Restore dist/ from Python Package artifact python-package-dist-bdist_wheel-linux-amd64
uses: actions/download-artifact@v3
with:
name: python-package-dist-bdist_wheel-linux-amd64
path: ./dist
- name: Restore dist/ from Python Package artifact python-package-dist-bdist_wheel-windows-amd64
uses: actions/download-artifact@v3
with:
name: python-package-dist-bdist_wheel-windows-amd64
path: ./dist
- name: Restore dist/ from Python Package artifact python-package-dist-bdist_wheel-macos-amd64
uses: actions/download-artifact@v3
with:
name: python-package-dist-bdist_wheel-macos-amd64
path: ./dist
- name: Upload Python Package to Release
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ env.VERSION }}
target_commitish: ${{ github.sha }}
files: dist/*
- name: Publish Python Package to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
name: Test
on:
push:
branches:
- 'main'
release:
types:
- created
env:
VERSION: ${{ github.event.release.tag_name != '' && github.event.release.tag_name || '0.0.0' }}
jobs:
test:
strategy:
fail-fast: false
matrix:
include:
-
os: ubuntu-20.04
python_version: '3.8.14' # the latest version of 3.8.x
sed: 'sed'
-
os: windows-2019
python_version: '3.8.10' # the latest version of 3.8.x
sed: 'sed'
-
os: macos-11
python_version: '3.8.14' # the latest version of 3.8.x
sed: 'gsed'
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Install gsed for macOS
if: startsWith(matrix.os, 'macos-')
shell: bash
run: brew install gnu-sed
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python_version }}
cache: 'pip'
cache-dependency-path: |
**/requirements-development.txt
- name: Install Dependencies
shell: bash
run: |
pip3 install -r requirements-development.txt
pip3 install wheel
- name: Replace version
shell: bash
run: |
"${{ matrix.sed }}" -i "s/__VERSION__ = '0.0.0'/__VERSION__ = '${{ env.VERSION }}'/" liveinfo/__init__.py
- name: Flake8
shell: bash
run: flake8
- name: mypy
shell: bash
run: mypy .
- name: pytest
shell: bash
run: pytest ./tests
- name: Build
shell: bash
run: python3 setup.py sdist bdist_wheel
- name: Build Binary
run: |
pyinstaller --name liveinfo --onefile main.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment