Skip to content

Commit c7c7b46

Browse files
authored
Set explicit workflow permissions and pin down actions (#227)
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent ebe8f54 commit c7c7b46

File tree

3 files changed

+111
-66
lines changed

3 files changed

+111
-66
lines changed

.github/workflows/ci.yml

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
name: Python CI
22

3-
on: [push, pull_request]
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
push:
7+
branches:
8+
- main
49

510
jobs:
611
lint-and-mypy:
712
name: Lint & mypy
8-
runs-on: ubuntu-latest
13+
runs-on: ubuntu-24.04
14+
permissions:
15+
contents: read
916
timeout-minutes: 5
1017
steps:
11-
- name: Checkout
12-
uses: actions/checkout@v4
18+
- name: Checkout repository
19+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
1320
with:
14-
submodules: recursive
21+
persist-credentials: false # do not keep the token around
1522

16-
- name: Setup Python environment
17-
uses: actions/setup-python@v5
23+
- name: Set up Python
24+
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
1825
with:
19-
python-version: "3.9"
26+
python-version: 3.9
2027

2128
- name: Install
2229
run: |
@@ -44,13 +51,14 @@ jobs:
4451
- "3.9"
4552

4653
steps:
47-
- name: Checkout
48-
uses: actions/checkout@v4
54+
- name: Checkout repository
55+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
4956
with:
5057
submodules: recursive
58+
persist-credentials: false # do not keep the token around
5159

52-
- name: Setup Python environment
53-
uses: actions/setup-python@v5
60+
- name: Set up Python ${{ matrix.python-version }}
61+
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
5462
with:
5563
python-version: ${{ matrix.python-version }}
5664

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
name: Build Python distributions, publish on PyPI, and create a GitHub release
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
tags:
7+
- "v*.*.*"
8+
9+
env:
10+
PYPI_PROJECT_URL: "https://pypi.org/p/packageurl-python"
11+
12+
jobs:
13+
build-python-dist:
14+
name: Build Python distributions
15+
runs-on: ubuntu-24.04
16+
permissions:
17+
contents: read
18+
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
22+
with:
23+
persist-credentials: false # do not keep the token around
24+
25+
- name: Set up Python
26+
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
27+
with:
28+
python-version: 3.14
29+
30+
- name: Install pypa/build
31+
run: python -m pip install build==1.4.0 --user
32+
33+
- name: Build a binary wheel and a source tarball
34+
run: python -m build --sdist --wheel --outdir dist/
35+
36+
- name: Upload package distributions as GitHub workflow artifacts
37+
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
38+
with:
39+
name: python-package-distributions
40+
path: dist/
41+
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: Checkout repository
78+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
79+
with:
80+
persist-credentials: false
81+
82+
- name: Download package distributions
83+
uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0
84+
with:
85+
name: python-package-distributions
86+
path: dist/
87+
88+
- name: Create GitHub release
89+
run: gh release create "$GITHUB_REF_NAME" dist/* --generate-notes
90+
env:
91+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/pypi-release.yml

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

0 commit comments

Comments
 (0)