Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PyPI upload actions #13

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .github/workflows/pip-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Reusable conda package builder

on:
workflow_call:
inputs:
package_name:
description: "Name of Python package name being built"
required: true
type: string
version:
description: "Version to be packaged and uploaded"
required: false
type: string
default: ${{ github.ref_name }}
secrets:
TEST_PYPI_API_TOKEN:
required: true

defaults:
run:
shell: bash -l {0}

jobs:
pip-build:
runs-on: ubuntu-latest
steps:
- name: check if TEST_PYPI_API_TOKEN exists
env:
# cannot use secrets directly in conditionals
# see https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#using-secrets-in-a-workflow
test_pypi_token: ${{ secrets.TEST_PYPI_API_TOKEN }}
if: env.test_pypi_token == ''
run: |
echo "the secret \"TEST_PYPI_API_TOKEN\" is not available, so the pypi package cannot be uploaded to the test index site."
echo "Please go to \"settings \> secrets \> actions\" to create it before trying to build the pip package."
exit 1

- uses: actions/checkout@v3
- uses: mamba-org/setup-micromamba@v1
with:
micromamba-version: latest
environment-name: pipbuild
create-args: >-
python=3.11
pip
build
post-cleanup: all
cache-environment: true

- name: Build package
run: python -m build

- name: Publish distribution 📦 to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository-url: https://test.pypi.org/legacy/

- name: Get version without the v
run: |
TAG=${{ inputs.version }}
echo "VERSION=${TAG#v}" >> $GITHUB_ENV

- name: try installing from TestPyPI
run: |
cd ${{ runner.temp }}
pip install -i https://test.pypi.org/simple/ --no-deps ${{ inputs.package_name }}==${{ env.VERSION }}

- name: Create built package artifact ready for upload on release
uses: actions/upload-artifact@v3
with:
name: pip-build-${{ inputs.package_name }}-${{ inputs.version }}
path: dist/*
retention-days: 7
62 changes: 62 additions & 0 deletions .github/workflows/pip-upload.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
on:
workflow_call:
inputs:
package_name:
description: "Name of Python package name being uploaded"
required: true
type: string
build_workflow:
description: "Name of workflow file used to build the package initially, if different from current workflow"
required: false
type: string
default: ""
version:
description: "Version to be packaged and uploaded"
required: false
type: string
default: ${{ github.ref_name }}
secrets:
PYPI_API_TOKEN:
required: true

defaults:
run:
shell: bash -l {0}

jobs:
pip-publish:
runs-on: ubuntu-latest
env:
PACKAGENAME: "pip-build-${{ inputs.package_name }}-${{ inputs.version }}"

steps:
- name: check if PYPI_API_TOKEN exists
env:
# cannot use secrets directly in conditionals
# see https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#using-secrets-in-a-workflow
pypi_token: ${{ secrets.PYPI_API_TOKEN }}
if: env.pypi_token == ''
run: |
echo "the secret \"PYPI_API_TOKEN\" is not available, so the pypi package cannot be uploaded to the test index site."
echo "Please go to \"settings \> secrets \> actions\" to create it before trying to build the pip package."
exit 1

- name: Download built package from another workflow
if: inputs.build_workflow != ''
uses: dawidd6/action-download-artifact@v2
with:
name: ${{ env.PACKAGENAME }}
workflow: ${{ inputs.build_workflow }}
path: dist/

- name: Download built package from same workflow
if: inputs.build_workflow == ''
uses: actions/download-artifact@v3
with:
name: ${{ env.PACKAGENAME }}
path: dist/

- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}