Skip to content

Commit b53c739

Browse files
committed
refactor the publish pypi worlfow for trusted publisher
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent 0bb4024 commit b53c739

3 files changed

Lines changed: 66 additions & 23 deletions

File tree

.github/workflows/pr-quality.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ permissions:
66
pull-requests: write
77

88
on:
9+
# pull_request_target is required so the action can close/comment on fork PRs.
10+
# This is safe because: no untrusted code is checked out, and no attacker-controlled
11+
# values are interpolated into shell commands. All action inputs are hardcoded.
912
pull_request_target:
1013
types: [opened, reopened]
1114

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
name: Build Python distributions and publish on PyPI
1+
name: Build Python distributions, publish on PyPI, and create a GH release
22

33
on:
44
workflow_dispatch:
55
push:
66
tags:
77
- "v*.*.*"
88

9+
env:
10+
PYPI_PROJECT_URL: "https://pypi.org/p/scancodeio"
11+
912
jobs:
10-
build-and-publish:
11-
name: Build and publish library to PyPI
13+
build-python-dist:
14+
name: Build Python distributions
1215
runs-on: ubuntu-24.04
16+
permissions:
17+
contents: read
1318

1419
steps:
1520
- name: Checkout repository
@@ -23,26 +28,61 @@ jobs:
2328
python-version: 3.14
2429

2530
- name: Install pypa/build
26-
run: python -m pip install build==1.4.0 --user
31+
run: python -m pip install build --user
2732

2833
- name: Build a binary wheel and a source tarball
29-
run: python -m build --sdist --wheel --outdir dist/ .
34+
run: python -m build --sdist --wheel --outdir dist/
3035

31-
- name: Publish to PyPI
32-
if: startsWith(github.ref, 'refs/tags')
33-
uses: pypa/gh-action-pypi-publish@release/v1
36+
- name: Upload package distributions as GitHub workflow artifacts
37+
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
3438
with:
35-
password: ${{ secrets.PYPI_API_TOKEN }}
39+
name: python-package-distributions
40+
path: dist/
3641

37-
- name: Upload built archives
38-
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
42+
# Only set the id-token: write permission in the job that does publishing, not globally.
43+
# Also, separate building from publishing — this makes sure that any scripts
44+
# maliciously injected into the build or test environment won't be able to elevate
45+
# privileges while flying under the radar.
46+
pypi-publish:
47+
name: Upload package distributions to PyPI
48+
if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes
49+
needs:
50+
- build-python-dist
51+
runs-on: ubuntu-24.04
52+
environment:
53+
name: pypi
54+
url: ${{ env.PYPI_PROJECT_URL }}
55+
permissions:
56+
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
57+
58+
steps:
59+
- name: Download package distributions
60+
uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0
61+
with:
62+
name: python-package-distributions
63+
path: dist/
64+
65+
- name: Publish to PyPI
66+
uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0
67+
68+
create-gh-release:
69+
name: Create GitHub release
70+
needs:
71+
- build-python-dist
72+
runs-on: ubuntu-24.04
73+
permissions:
74+
contents: write
75+
76+
steps:
77+
- name: Download package distributions
78+
uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0
3979
with:
40-
name: pypi_archives
41-
path: dist/*
80+
name: python-package-distributions
81+
path: dist/
4282

43-
- name: Create a GitHub release
44-
uses: softprops/action-gh-release@v2
83+
- name: Create GH release
84+
uses: softprops/action-gh-release@a06a81a03ee405af7f2048a818ed3f03bbf83c7b # v2.5.0
4585
with:
46-
generate_release_notes: true
4786
draft: false
87+
generate_release_notes: true
4888
files: dist/*

.github/workflows/sca-integration-ort-package-file.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,25 @@ jobs:
3434

3535
- name: Copy package-list.yml to workspace root
3636
run: |
37-
FILE=$(ls ${{ env.PROJECT_WORK_DIRECTORY }}/output/*.package-list.yml | head -n 1)
38-
sudo mkdir -p ${GITHUB_WORKSPACE}/ort-data/
37+
FILE=$(ls "${PROJECT_WORK_DIRECTORY}/output/"*.package-list.yml | head -n 1)
38+
sudo mkdir -p "${GITHUB_WORKSPACE}/ort-data/"
3939
sudo cp "$FILE" "${GITHUB_WORKSPACE}/ort-data/package-list.yml"
40-
sudo chmod -R 777 ${GITHUB_WORKSPACE}/ort-data/
40+
sudo chmod -R 777 "${GITHUB_WORKSPACE}/ort-data/"
4141
ls -lh "${GITHUB_WORKSPACE}/ort-data/"
4242
4343
- name: Generates an ORT analyzer-result.yml file
4444
run: |
45-
docker run --rm -v ${GITHUB_WORKSPACE}/ort-data:/data \
45+
docker run --rm -v "${GITHUB_WORKSPACE}/ort-data:/data" \
4646
--entrypoint /opt/ort/bin/orth \
47-
ghcr.io/oss-review-toolkit/ort:${{ env.ORT_VERSION }} \
47+
"ghcr.io/oss-review-toolkit/ort:${ORT_VERSION}" \
4848
create-analyzer-result-from-package-list \
4949
--package-list-file /data/package-list.yml \
5050
--ort-file /data/analyzer-result.yml
5151
5252
- name: Report as CycloneDX and SPDX using the analyzer-result.yml file
5353
run: |
54-
docker run --rm -v ${GITHUB_WORKSPACE}/ort-data:/data \
55-
ghcr.io/oss-review-toolkit/ort:${{ env.ORT_VERSION }} \
54+
docker run --rm -v "${GITHUB_WORKSPACE}/ort-data:/data" \
55+
"ghcr.io/oss-review-toolkit/ort:${ORT_VERSION}" \
5656
report \
5757
--ort-file /data/analyzer-result.yml \
5858
--output-dir /data/results/ \

0 commit comments

Comments
 (0)