Skip to content

Commit 75a9c9c

Browse files
authored
github: Enable trusted publishing (#1230)
* github: Rewrite publish workflow * github: Fix dist path * github: Make publish_examples work with workflow_call * github: Fix quoting
1 parent 3da9aed commit 75a9c9c

File tree

2 files changed

+158
-120
lines changed

2 files changed

+158
-120
lines changed

.github/workflows/Publish_NIMS.yml

Lines changed: 0 additions & 120 deletions
This file was deleted.

.github/workflows/publish.yml

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
name: Publish NIMS
2+
3+
on:
4+
release:
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
17+
18+
env:
19+
environment: ${{ github.event_name == 'release' && 'pypi' || inputs.environment }}
20+
environment-info: |
21+
{
22+
"pypi": {
23+
"base-url": "https://pypi.org",
24+
"upload-url": "https://upload.pypi.org/legacy/"
25+
},
26+
"testpypi": {
27+
"base-url": "https://test.pypi.org",
28+
"upload-url": "https://test.pypi.org/legacy/"
29+
}
30+
}
31+
package-name: |
32+
{
33+
"generator": "ni_measurement_plugin_sdk_generator",
34+
"sdk": "ni_measurement_plugin_sdk",
35+
"service": "ni_measurement_plugin_sdk_service"
36+
}
37+
38+
jobs:
39+
# Do not call check_examples.yml because the examples may depend on the version we are releasing.
40+
check_nimg:
41+
name: Check generator
42+
uses: ./.github/workflows/check_nimg.yml
43+
check_nims:
44+
name: Check service
45+
uses: ./.github/workflows/check_nims.yml
46+
build_package:
47+
name: Build ${{ matrix.package }}
48+
runs-on: ubuntu-latest
49+
needs: [check_nimg, check_nims]
50+
strategy:
51+
matrix:
52+
package: [generator, sdk, service]
53+
steps:
54+
- name: Check out repo
55+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
56+
- name: Set up Python
57+
uses: ni/python-actions/setup-python@f0276f7f58868ec0d0d1a86377287c9e6fe0c6e7 # v0.5.0
58+
- name: Set up Poetry
59+
uses: ni/python-actions/setup-poetry@f0276f7f58868ec0d0d1a86377287c9e6fe0c6e7 # v0.5.0
60+
- name: Check project version
61+
if: github.event_name == 'release'
62+
uses: ni/python-actions/check-project-version@f0276f7f58868ec0d0d1a86377287c9e6fe0c6e7 # v0.5.0
63+
with:
64+
project-directory: ./packages/${{ matrix.package }}
65+
- name: Build distribution packages
66+
run: poetry build
67+
working-directory: ./packages/${{ matrix.package }}
68+
- name: Upload build artifacts
69+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
70+
with:
71+
name: ${{ matrix.package }}-distribution-packages
72+
path: ./packages/${{ matrix.package }}/dist/*
73+
publish_package:
74+
name: Publish ${{ matrix.package }} to PyPI
75+
if: github.event_name == 'release' || inputs.environment != 'none'
76+
runs-on: ubuntu-latest
77+
needs: [build_package]
78+
environment:
79+
# This logic is duplicated because `name` doesn't support the `env` context.
80+
name: ${{ github.event_name == 'release' && 'pypi' || inputs.environment }}
81+
url: ${{ fromJson(env.environment-info)[env.environment].base-url }}/p/${{ fromJson(env.package-name)[matrix.package] }}
82+
permissions:
83+
id-token: write
84+
strategy:
85+
matrix:
86+
package: [generator, sdk, service]
87+
steps:
88+
- name: Download build artifacts
89+
uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
90+
with:
91+
name: ${{ matrix.package }}-distribution-packages
92+
path: dist/
93+
- run: ls -lR
94+
- name: Upload to ${{ env.environment }}
95+
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
96+
with:
97+
repository-url: ${{ fromJson(env.environment-info)[env.environment].upload-url }}
98+
publish_examples:
99+
name: Publish examples
100+
runs-on: ubuntu-latest
101+
needs: [check_nimg, check_nims]
102+
steps:
103+
- name: Check out repo
104+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
105+
- name: Get version
106+
id: get-version
107+
run: echo "version=$(poetry version --short)" >> "$GITHUB_OUTPUT"
108+
working-directory: ./packages/service
109+
- name: Create archives of the examples
110+
env:
111+
EXAMPLE_ARCHIVE: measurement-plugin-python-examples-${{ steps.get-version.outputs.version }}
112+
run: |
113+
# Use --prefix for the tarball but not the zip file. Windows zip tools often create a directory automatically.
114+
rm -rf dist
115+
mkdir -p dist
116+
git archive -o dist/${EXAMPLE_ARCHIVE}.zip ${GITHUB_REF}:examples/
117+
git archive -o dist/${EXAMPLE_ARCHIVE}.tar.gz --prefix ${EXAMPLE_ARCHIVE}/ ${GITHUB_REF}:examples/
118+
- name: Upload release assets
119+
if: ${{ startsWith(github.event.release.target_commitish, 'main') || startsWith(github.event.release.target_commitish, 'releases/') }}
120+
uses: ncipollo/release-action@bcfe5470707e8832e12347755757cec0eb3c22af # v1.18.0
121+
with:
122+
artifacts: "dist/measurement-plugin-python-examples-*"
123+
allowUpdates: true
124+
omitBodyDuringUpdate: true
125+
omitDraftDuringUpdate: true
126+
omitNameDuringUpdate: true
127+
omitPrereleaseDuringUpdate: true
128+
updateOnlyUnreleased: false
129+
update_versions:
130+
name: Update package versions
131+
runs-on: ubuntu-latest
132+
needs: [build_package]
133+
permissions:
134+
contents: write
135+
pull-requests: write
136+
steps:
137+
- name: Check out repo
138+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
139+
- name: Set up Python
140+
uses: ni/python-actions/setup-python@f0276f7f58868ec0d0d1a86377287c9e6fe0c6e7 # v0.5.0
141+
- name: Set up Poetry
142+
uses: ni/python-actions/setup-poetry@f0276f7f58868ec0d0d1a86377287c9e6fe0c6e7 # v0.5.0
143+
# Create one pull request that updates all three packages.
144+
- name: Update generator project version
145+
uses: ni/python-actions/update-project-version@f0276f7f58868ec0d0d1a86377287c9e6fe0c6e7 # v0.5.0
146+
with:
147+
project-directory: ./packages/generator
148+
create-pull-request: false
149+
- name: Update sdk project version
150+
uses: ni/python-actions/update-project-version@f0276f7f58868ec0d0d1a86377287c9e6fe0c6e7 # v0.5.0
151+
with:
152+
project-directory: ./packages/sdk
153+
create-pull-request: false
154+
- name: Update service project version
155+
uses: ni/python-actions/update-project-version@f0276f7f58868ec0d0d1a86377287c9e6fe0c6e7 # v0.5.0
156+
with:
157+
project-directory: ./packages/service
158+
create-pull-request: true

0 commit comments

Comments
 (0)