Skip to content

Commit 162f107

Browse files
authored
Merge pull request #28 from bkeryan/users/bkeryan/trusted-publisher
github: Get Trusted Publisher workflow working
2 parents e8be01f + bb1de82 commit 162f107

File tree

2 files changed

+83
-52
lines changed

2 files changed

+83
-52
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: 81 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +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-
42+
- name: Check out repo
43+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2344
- name: Set up Python
24-
uses: ni/python-actions/setup-python@97860b52be87c788fb6df812bd8d1ca68c7aa885 # v0.3.0
45+
uses: ni/python-actions/setup-python@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1
2546
- name: Set up Poetry
26-
uses: ni/python-actions/setup-poetry@97860b52be87c788fb6df812bd8d1ca68c7aa885 # v0.3.0
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
2751
- name: Check project version
2852
if: github.event_name == 'release'
29-
uses: ni/python-actions/check-project-version@97860b52be87c788fb6df812bd8d1ca68c7aa885 # v0.3.0
53+
uses: ni/python-actions/check-project-version@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1
3054
- name: Build distribution packages
3155
run: poetry build
3256
- name: Upload build artifacts
3357
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
3458
with:
3559
name: ${{ env.dist-artifact-name }}
3660
path: dist/*
37-
# @TODO: This is a workaround for there not being a way to check the lock file
38-
# See: https://github.com/python-poetry/poetry/issues/453
39-
- name: Check for lock changes
40-
run: |
41-
poetry lock --check
42-
- uses: actions/cache@v4
43-
with:
44-
path: ~/.cache/pypoetry/virtualenvs
45-
key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }}
46-
- name: Install the Package
47-
run: poetry install
48-
- name: Lint the Code
49-
run: poetry run ni-python-styleguide lint
50-
51-
- name: Run tests
52-
run: poetry run pytest -v
53-
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
5497
# If the version is 0.1.0-alpha.0, this will set the version to 0.1.0
5598
- name: Promote package version to release
56-
uses: ni/python-actions/update-project-version@97860b52be87c788fb6df812bd8d1ca68c7aa885 # v0.3.0
57-
with:
58-
create-pull-request: false
59-
commit-message: "Bump package version to minor release version"
60-
version-rule: "minor"
61-
- name: Build Python package
62-
if: ${{ github.event.release.target_commitish == 'main' || startsWith(github.event.release.target_commitish, 'releases/')}}
63-
run: |
64-
poetry build
65-
66-
- name: Upload Python package
67-
if: ${{ github.event.release.target_commitish == 'main' || startsWith(github.event.release.target_commitish, 'releases/')}}
68-
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
69-
with:
70-
packages-dir: dist/
71-
72-
- name: Bump poetry version to next alpha version
73-
uses: ni/python-actions/update-project-version@97860b52be87c788fb6df812bd8d1ca68c7aa885 # v0.3.0
99+
run: poetry version patch
100+
- name: Update project version
101+
uses: ni/python-actions/update-project-version@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1
74102
with:
75-
create-pull-request: true
76-
commit-message: "Bump package version"
103+
# The default GITHUB_TOKEN cannot trigger PR workflows.
104+
token: ${{ secrets.ADMIN_PAT }}
77105
version-rule: "prepatch"
106+
use-dev-suffix: false

0 commit comments

Comments
 (0)