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

CI: Add publishing to PyPI and TestPyPI with trusted publishers #51

Merged
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
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dependabot config is only for updating GitHub Actions, and so the number of PRs generated should be substantially less than if Python packages were being considered.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
labels:
- "github-actions"
- "dependencies"
reviewers:
- "asmeurer"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes @asmeurer wants to be the person to get the ping.

105 changes: 105 additions & 0 deletions .github/workflows/publish-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
name: publish distributions
on:
push:
branches:
- main
tags:
- [0-9]+.[0-9]+
- [0-9]+.[0-9]+.[0-9]+
pull_request:
branches:
- main
release:
types: [published]
Comment on lines +12 to +13
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit is for publishing on GitHub Release.

workflow_dispatch:
inputs:
publish:
type: choice
description: 'Publish to TestPyPI?'
options:
- false
- true
Comment on lines +14 to +21
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would allow you to create a release to TestPyPI from workflow dispatch. If this isn't of interest this can get removed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is worth doing at least the first time around. How do you do it? Is there some option when you create the release in the GitHub interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do this through workflow dispatch by visiting the GitHub Actions tab for the repo and then selecting this workflow (https://github.com/data-apis/array-api-compat/actions/workflows/publish-package.yml) and then when you click the "Run workflow" button on the right hand side there will be the option to select false or true from a dropdown.

This is how it looks like for us on pyhf's GitHub repo:

pyhf-view

and if it helps, starting around 52 seconds into this video of me making the pyhf v0.7.1 release I show this button example: https://youtu.be/ZV20tr3EpTw?t=52


concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
name: Build Python distribution
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Install python-build and twine
run: |
python -m pip install --upgrade pip setuptools
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If array-api-compat ever moves off setuptools this setuptools bit here could be removed.

python -m pip install build twine
python -m pip list

- name: Build a wheel and a sdist
run: |
PYTHONWARNINGS=error,default::DeprecationWarning python -m build .

- name: Verify the distribution
run: twine check --strict dist/*

- name: List contents of sdist
run: python -m tarfile --list dist/array_api_compat-*.tar.gz

- name: List contents of wheel
run: python -m zipfile --list dist/array_api_compat-*.whl

- name: Upload distribution artifact
uses: actions/upload-artifact@v3
with:
name: dist-artifact
path: dist

publish:
name: Publish Python distribution to (Test)PyPI
if: github.event_name != 'pull_request' && github.repository == 'data-apis/array-api-compat'
needs: build
runs-on: ubuntu-latest
# Mandatory for publishing with a trusted publisher
# c.f. https://docs.pypi.org/trusted-publishers/using-a-publisher/
permissions:
id-token: write
# Restrict to the environment set for the trusted publisher
environment:
name: publish-package

steps:
- name: Download distribution artifact
uses: actions/download-artifact@v3
with:
name: dist-artifact
path: dist

- name: List all files
run: ls -lh dist

- name: Publish distribution 📦 to Test PyPI
# Publish to TestPyPI on tag events of if manually triggered
# Compare to 'true' string as booleans get turned into strings in the console
if: >-
(github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v'))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asmeurer Also catching now that as you use tags that match

    - [0-9]+.[0-9]+
    - [0-9]+.[0-9]+.[0-9]+

but not v* that these will never publish to TestPyPI on tag. I should fix this in a follow up PR.

|| (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true')
uses: pypa/[email protected]
with:
repository-url: https://test.pypi.org/legacy/
print-hash: true

- name: Publish distribution 📦 to PyPI
if: github.event_name == 'release' && github.event.action == 'published'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is assuming that you only want to publish to PyPI via creating a GitHub Release.

uses: pypa/[email protected]
with:
print-hash: true