Skip to content

Commit da696d8

Browse files
mshafer-NIbkeryan
andauthored
update publishing flow to use ni/python-actions (#227)
* update publishing flow to use ni/python-actions * github: Split Publish-Package.yml jobs according to security best practices * github: Restore versioning policy * github: Fix next version update --------- Co-authored-by: Brad Keryan <[email protected]>
1 parent c664cce commit da696d8

File tree

2 files changed

+93
-59
lines changed

2 files changed

+93
-59
lines changed

.github/workflows/PR.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ on:
1010
- pyproject.toml
1111
- docs/Coding-Conventions.md
1212
- .github/workflows/PR.yml
13+
workflow_call:
14+
workflow_dispatch:
1315

1416
env:
1517
POETRY_VERSION: 1.8.1

.github/workflows/Publish-Package.yml

Lines changed: 91 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,105 @@ name: Publish Package
22

33
on:
44
release:
5-
types: [released]
5+
types: [published]
6+
workflow_dispatch:
7+
inputs:
8+
environment:
9+
description: The environment to publish to.
10+
default: 'none'
11+
required: true
12+
type: choice
13+
options:
14+
- none
15+
- pypi
16+
- testpypi
617

718
env:
8-
# Versions are also listed in PR.yml
9-
POETRY_VERSION: 1.8.1
10-
PYTHON_VERSION: 3.11 # Use latest
19+
dist-artifact-name: package-distribution-packages
20+
environment: ${{ github.event_name == 'release' && 'pypi' || inputs.environment }}
21+
environment-info: |
22+
{
23+
"pypi": {
24+
"base-url": "https://pypi.org",
25+
"upload-url": "https://upload.pypi.org/legacy/"
26+
},
27+
"testpypi": {
28+
"base-url": "https://test.pypi.org",
29+
"upload-url": "https://test.pypi.org/legacy/"
30+
}
31+
}
1132
1233
jobs:
13-
publish_package:
14-
name: Publish Package
34+
check_package:
35+
name: Check package
36+
uses: ./.github/workflows/PR.yml
37+
build_package:
38+
name: Build package
1539
runs-on: ubuntu-latest
40+
needs: [check_package]
1641
steps:
17-
- uses: actions/checkout@v2
18-
with:
19-
ref: ${{ github.event.release.target_commitish }} # This is the branch the release was created from. Normally main, but can be a dev branch
20-
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token
21-
fetch-depth: 0 # otherwise, you will failed to push refs to dest repo
22-
23-
- uses: actions/setup-python@v2
24-
with:
25-
python-version: ${{ env.PYTHON_VERSION }}
26-
- uses: Gr1N/setup-poetry@v8
27-
with:
28-
poetry-version: ${{ env.POETRY_VERSION }}
29-
# @TODO: This is a workaround for there not being a way to check the lock file
30-
# See: https://github.com/python-poetry/poetry/issues/453
31-
- name: Check for lock changes
32-
run: |
33-
poetry lock --check
34-
- uses: actions/cache@v4
42+
- name: Check out repo
43+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
44+
- name: Set up Python
45+
uses: ni/python-actions/setup-python@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1
46+
- name: Set up Poetry
47+
uses: ni/python-actions/setup-poetry@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1
48+
# If the version is 0.1.0-alpha.0, this will set the version to 0.1.0
49+
- name: Promote package version to release
50+
run: poetry version patch
51+
- name: Check project version
52+
if: github.event_name == 'release'
53+
uses: ni/python-actions/check-project-version@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1
54+
- name: Build distribution packages
55+
run: poetry build
56+
- name: Upload build artifacts
57+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
3558
with:
36-
path: ~/.cache/pypoetry/virtualenvs
37-
key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }}
38-
- name: Install the Package
39-
run: poetry install
40-
- name: Lint the Code
41-
run: poetry run ni-python-styleguide lint
42-
43-
- name: Run tests
44-
run: poetry run pytest -v
45-
59+
name: ${{ env.dist-artifact-name }}
60+
path: dist/*
61+
publish_to_pypi:
62+
name: Publish package to PyPI
63+
if: github.event_name == 'release' || inputs.environment != 'none'
64+
runs-on: ubuntu-latest
65+
needs: [build_package]
66+
environment:
67+
# This logic is duplicated because `name` doesn't support the `env` context.
68+
name: ${{ github.event_name == 'release' && 'pypi' || inputs.environment }}
69+
url: ${{ fromJson(env.environment-info)[env.environment].base-url }}/p/ni-python-styleguide
70+
permissions:
71+
id-token: write
72+
steps:
73+
- name: Download build artifacts
74+
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
75+
with:
76+
name: ${{ env.dist-artifact-name }}
77+
path: dist/
78+
- run: ls -lR
79+
- name: Upload to ${{ env.environment }}
80+
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
81+
with:
82+
repository-url: ${{ fromJson(env.environment-info)[env.environment].upload-url }}
83+
update_version:
84+
name: Update package version to next alpha version
85+
runs-on: ubuntu-latest
86+
needs: [build_package]
87+
permissions:
88+
contents: write
89+
pull-requests: write
90+
steps:
91+
- name: Check out repo
92+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
93+
- name: Set up Python
94+
uses: ni/python-actions/setup-python@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1
95+
- name: Set up Poetry
96+
uses: ni/python-actions/setup-poetry@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1
4697
# If the version is 0.1.0-alpha.0, this will set the version to 0.1.0
4798
- name: Promote package version to release
48-
run: |
49-
poetry version patch
50-
51-
- name: Build Python package and publish to PyPI
52-
if: ${{ github.event.release.target_commitish == 'main' }}
53-
run: |
54-
poetry publish --build --username __token__ --password ${{ secrets.PYPI_TOKEN}}
55-
56-
- name: Bump poetry version to next alpha version
57-
run: |
58-
poetry version prepatch
59-
60-
- name: Commit files
61-
if: ${{ github.event.release.target_commitish == 'main' }}
62-
run: |
63-
git config --local user.email "[email protected]"
64-
git config --local user.name "GitHub Action"
65-
git pull --tags -f
66-
git commit -m "Bump package version" -a
67-
68-
- name: Push changes
69-
if: ${{ github.event.release.target_commitish == 'main' }}
70-
uses: CasperWA/push-protected@v2
99+
run: poetry version patch
100+
- name: Update project version
101+
uses: ni/python-actions/update-project-version@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1
71102
with:
103+
# The default GITHUB_TOKEN cannot trigger PR workflows.
72104
token: ${{ secrets.ADMIN_PAT }}
73-
branch: ${{ github.event.release.target_commitish }}
74-
unprotect_reviews: true
105+
version-rule: "prepatch"
106+
use-dev-suffix: false

0 commit comments

Comments
 (0)