Skip to content

Commit b8480d5

Browse files
committed
ci: migrate PyPI publishing workflow to OSS Exit Gate
1 parent 2ef32ab commit b8480d5

1 file changed

Lines changed: 74 additions & 12 deletions

File tree

‎.github/workflows/publish-python.yml‎

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
name: Build and Publish Python Package
22

33
# Be very careful granting extra permissions here, as this workflow uses third party actions.
4-
# Prefer to pin to exact commits for third-party actions. I'm making an exception for actions
5-
# maintained by GitHub itself.
4+
# Prefer to pin to exact commits for third-party actions.
65

76
# For now, just trigger this workflow manually.
87
on:
@@ -20,6 +19,9 @@ on:
2019
- testpypi
2120
- pypi
2221

22+
permissions:
23+
contents: read
24+
2325
jobs:
2426
build_sdist:
2527
name: Build Python sdist
@@ -28,15 +30,17 @@ jobs:
2830
contents: read
2931

3032
steps:
31-
- uses: actions/checkout@v4
32-
- uses: actions/setup-python@v5
33+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
34+
with:
35+
persist-credentials: false
36+
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
3337
with:
3438
python-version: "3.8"
3539
cache: "pip"
3640
- run: pip install 'build==1.2.2'
3741
- name: Build sdist
3842
run: python3 -m build --sdist
39-
- uses: actions/upload-artifact@v4
43+
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
4044
with:
4145
name: sdist
4246
path: ./dist/*.gz
@@ -54,7 +58,9 @@ jobs:
5458
contents: read
5559

5660
steps:
57-
- uses: actions/checkout@v4
61+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
62+
with:
63+
persist-credentials: false
5864

5965
- name: Build wheels
6066
uses: pypa/cibuildwheel@298ed2fb2c105540f5ed055e8a6ad78d82dd3a7e # v3.3.1
@@ -66,7 +72,7 @@ jobs:
6672
CIBW_TEST_COMMAND: >
6773
python {package}/python/_jsonnet_test.py
6874
69-
- uses: actions/upload-artifact@v4
75+
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
7076
with:
7177
name: cibw-wheels-${{ matrix.os }}
7278
path: ./wheelhouse/*.whl
@@ -79,16 +85,16 @@ jobs:
7985
if: "${{ inputs.upload_to_pypi }}"
8086
permissions:
8187
contents: read
82-
id-token: write # Needed for PyPI Trusted Publishing
88+
id-token: write # Needed for OIDC authentication
8389
environment:
8490
name: "${{ inputs.pypi_environment }}"
8591
url: "${{ inputs.pypi_environment == 'pypi' && 'https://pypi.org/p/jsonnet' || 'https://test.pypi.org/p/jsonnet' }}"
8692
steps:
87-
- uses: actions/download-artifact@v4
93+
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
8894
with:
8995
path: cibw-wheels
9096
pattern: cibw-wheels-*
91-
- uses: actions/download-artifact@v4
97+
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
9298
with:
9399
path: sdist
94100
name: sdist
@@ -97,9 +103,65 @@ jobs:
97103
mkdir dist
98104
find cibw-wheels/ -type f -name '*.whl' -exec mv '{}' ./dist/ ';'
99105
find sdist/ -type f -name '*.gz' -exec mv '{}' ./dist/ ';'
100-
- name: Publish to PyPI
106+
107+
- name: Publish to TestPyPI
108+
if: inputs.pypi_environment == 'testpypi'
101109
uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0
102110
with:
103111
verbose: true
104112
print-hash: true
105-
repository-url: "${{ inputs.pypi_environment == 'testpypi' && 'https://test.pypi.org/legacy/' || '' }}"
113+
repository-url: 'https://test.pypi.org/legacy/'
114+
115+
- name: Set up Python for twine
116+
if: inputs.pypi_environment == 'pypi'
117+
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
118+
with:
119+
python-version: "3.12"
120+
121+
- name: Install twine
122+
if: inputs.pypi_environment == 'pypi'
123+
run: python -m pip install --upgrade pip twine
124+
125+
- name: Authenticate to Google Cloud (OSS Exit Gate)
126+
if: inputs.pypi_environment == 'pypi'
127+
uses: google-github-actions/auth@c200f3691d83b41bf9bbd8638997a462592937ed # v2
128+
with:
129+
workload_identity_provider: 'projects/305452601764/locations/global/workloadIdentityPools/builders/providers/github'
130+
131+
- name: Set up Cloud SDK
132+
if: inputs.pypi_environment == 'pypi'
133+
uses: google-github-actions/setup-gcloud@e427ad8a34f8676edf47cf7d7925499adf3eb74f # v2
134+
135+
- name: Clean up stale releases from staging repo
136+
if: inputs.pypi_environment == 'pypi'
137+
run: |
138+
packages=$(gcloud artifacts packages list \
139+
--project="oss-exit-gate-prod" \
140+
--location="us" \
141+
--repository="jsonnet--pypi" \
142+
--format="value(name)" 2>/dev/null)
143+
144+
echo "${packages}" | xargs -r -I {} sh -c '
145+
package_name=$(basename "{}")
146+
echo "Deleting package: $package_name..."
147+
gcloud artifacts packages delete "{}" \
148+
--project="'"oss-exit-gate-prod"'" \
149+
--location="'"us"'" \
150+
--repository="'"jsonnet--pypi"'" \
151+
--quiet
152+
'
153+
154+
- name: Stage to OSS Exit Gate and upload manifest
155+
if: inputs.pypi_environment == 'pypi'
156+
env:
157+
GH_RUN_ID: ${{ github.run_id }}
158+
GH_RUN_ATTEMPT: ${{ github.run_attempt }}
159+
run: |
160+
python -m twine upload \
161+
--repository-url "https://us-python.pkg.dev/oss-exit-gate-prod/jsonnet--pypi/" \
162+
-u oauth2accesstoken \
163+
-p "$(gcloud auth print-access-token)" \
164+
dist/*
165+
echo '{"publish_all": true}' > manifest.json
166+
gcloud storage cp manifest.json \
167+
"gs://oss-exit-gate-prod-projects-bucket/jsonnet/pypi/manifests/manifest-${GH_RUN_ID}-${GH_RUN_ATTEMPT}.json"

0 commit comments

Comments
 (0)