Skip to content

Instantly share code, notes, and snippets.

@AlanPadi95
Last active March 3, 2021 07:35
Show Gist options
  • Save AlanPadi95/6b17ee51c0ad65bf13e561792a5ea429 to your computer and use it in GitHub Desktop.
Save AlanPadi95/6b17ee51c0ad65bf13e561792a5ea429 to your computer and use it in GitHub Desktop.
How to manage Python releases and upload them to Test.PyPi or PyPi? To take control of the major releases, I use a '.env' file.
name: Publish Pre-Release
on:
push:
# When a new tag is created for an alpha or beta version, this workflow starts running
tags:
- 'v*alpha*'
- 'v*beta*'
jobs:
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
# Gets the release version from the tag
- name: Get the version
id: get_version
run: echo ::set-output name=VERSION::$(echo $GITHUB_REF | cut -d / -f 3)
# Installs build using pip
- name: Install pypa/build
run: python -m pip install build
# Builds the package assigning the result og the get_version step to an environment variable called VERSION
- name: Build a binary wheel and a source tarball
run: python -m build
env:
VERSION: ${{ steps.get_version.outputs.VERSION }}
# Publish the package in Test PyPi using your token stored in GitHub Secrets
- name: Publish distribution 📦 to Test PyPI
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository_url: https://test.pypi.org/legacy/
# Creates the pre-release in GitHub
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
with:
tag_name: ${{ github.ref }}
release_name: PyOpenProject ${{ github.ref }}
body_path: CHANGELOG.md
draft: false
prerelease: true
# If something goes wrong, deletes the tag created in GitHub
- name: Delete tag
run: git push --delete origin ${{ github.ref }}
if: ${{ failure() }}
name: Publish Release
on:
workflow_run:
workflows: [ "Run Test Cases" ]
branches: [ main ]
types:
- completed
jobs:
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
# Gets the version of the release from the .env file, setting the result as a step output
- name: Get Version
id: get_version
run: echo "##[set-output name=version;]$(source ./.env && export VERSION && echo $VERSION)"
# Installs build using pip
- name: Install pypa/build
run: python -m pip install build
# Builds the package assigning the result og the get_version step to an environment variable called VERSION
- name: Build a binary wheel and a source tarball
run: python -m build
env:
VERSION: ${{ steps.get_version.outputs.version }}
# Publish the package in PyPi using your token stored in GitHub Secrets
- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.PYPI_API_TOKEN }}
# Creates the major release in GitHub
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
VERSION: ${{ steps.get_version.outputs.version }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
with:
tag_name: ${{ steps.get_version.outputs.version }}
release_name: PyOpenProject ${{ steps.get_version.outputs.version }}
body_path: CHANGELOG.md
draft: false
prerelease: false
# If something goes wrong, deletes the tag created in GitHub
- name: Delete tag
run: |
git push --delete origin "${{ steps.get_version.outputs.version }}"
if: ${{ failure() }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment