Skip to content

Commit 8a38a58

Browse files
authored
GHA: Use GHA environments to select PyPI target (#487)
Replace if statements with ${{ vars.PYPI_TARGET }}, defined in GHA environments, to select the target PyPI repository when publishing packages. Relates to #467 Implements #466
1 parent 562703f commit 8a38a58

File tree

3 files changed

+27
-57
lines changed

3 files changed

+27
-57
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,6 @@ jobs:
134134
uses: ./.github/workflows/publish-release.yml
135135
secrets: inherit # pass all secrets (required to access secrets in a called workflow)
136136
with:
137-
pypi_target: "pypi.org"
137+
environment: "production"
138138
repo_release_ref: "main"
139139
docker_image_tag: "latest_dev"

.github/workflows/publish-release.yml

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
name: Publish Release
2-
run-name: "Publish Release (pypi_target=${{ inputs.pypi_target }}, repo_release_ref=${{ inputs.repo_release_ref }})"
2+
run-name: "Publish Release (environment=${{ inputs.environment }}, repo_release_ref=${{ inputs.repo_release_ref }})"
33

44
on:
55
# Trigger release workflow from other workflows (e.g. release dev build as part of CI)
66
workflow_call:
77
inputs:
8-
pypi_target:
9-
description: "PyPI repository to publish to"
10-
required: true
8+
environment:
9+
description: "Target environment"
1110
type: string
12-
default: "test.pypi.org"
11+
required: true
1312
repo_release_ref:
1413
description: "Gitlint git reference to publish release for"
1514
type: string
@@ -23,14 +22,10 @@ on:
2322
# Manually trigger a release
2423
workflow_dispatch:
2524
inputs:
26-
pypi_target:
27-
description: "PyPI repository to publish to"
25+
environment:
26+
description: "Target environment"
27+
type: environment
2828
required: true
29-
type: choice
30-
options:
31-
- "pypi.org"
32-
- "test.pypi.org"
33-
default: "test.pypi.org"
3429
repo_release_ref:
3530
description: "Gitlint git reference to publish release for"
3631
type: string
@@ -49,6 +44,7 @@ jobs:
4944
publish:
5045
timeout-minutes: 15
5146
runs-on: "ubuntu-latest"
47+
environment: ${{ inputs.environment }}
5248
permissions:
5349
# Required for trusted publishing to PyPI
5450
id-token: write
@@ -110,27 +106,16 @@ jobs:
110106
uses: pypa/gh-action-pypi-publish@release/v1
111107
with:
112108
packages-dir: gitlint-core/dist/
113-
if: inputs.pypi_target == 'pypi.org'
109+
repository-url: ${{ vars.PYPI_TARGET }} # PYPI_TARGET is defined in the GHA environment
114110

115111
- name: Publish gitlint 🐍📦 to PyPI
116-
uses: pypa/gh-action-pypi-publish@release/v1
117-
if: inputs.pypi_target == 'pypi.org'
118-
119-
- name: Publish gitlint-core 🐍📦 to TestPyPI
120-
uses: pypa/gh-action-pypi-publish@release/v1
121-
with:
122-
packages-dir: gitlint-core/dist/
123-
repository-url: https://test.pypi.org/legacy/
124-
if: inputs.pypi_target == 'test.pypi.org'
125-
126-
- name: Publish gitlint 🐍📦 to TestPyPI
127112
uses: pypa/gh-action-pypi-publish@release/v1
128113
with:
129-
repository-url: https://test.pypi.org/legacy/
130-
if: inputs.pypi_target == 'test.pypi.org'
114+
repository-url: ${{ vars.PYPI_TARGET }} # PYPI_TARGET is defined in the GHA environment
131115

132116
# Wait for gitlint package to be available in PyPI for installation
133117
wait-for-package:
118+
environment: ${{ inputs.environment }}
134119
needs:
135120
- publish
136121
runs-on: "ubuntu-latest"
@@ -140,18 +125,10 @@ jobs:
140125
with:
141126
timeout_minutes: 1
142127
max_attempts: 10
128+
# We need to add the --extra-index-url to the pip install command to deal with PYPI_TARGET=https://test.pypi.org/legacy,
129+
# because gitlint's dependencies are not available on Test PyPI
143130
command: |
144-
python -m pip install gitlint==${{ needs.publish.outputs.gitlint_version }}
145-
if: inputs.pypi_target == 'pypi.org'
146-
147-
- name: Install gitlint (test.pypi.org)
148-
uses: nick-fields/retry@v2.8.3
149-
with:
150-
timeout_minutes: 1
151-
max_attempts: 10
152-
command: |
153-
pip install --no-cache-dir -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple gitlint==${{ needs.publish.outputs.gitlint_version }}
154-
if: inputs.pypi_target == 'test.pypi.org'
131+
python -m pip install --no-cache-dir -i ${{ vars.PYPI_TARGET }} --extra-index-url https://pypi.org/simple gitlint==${{ needs.publish.outputs.gitlint_version }}
155132
156133
- name: gitlint --version
157134
run: |
@@ -171,7 +148,7 @@ jobs:
171148
uses: ./.github/workflows/test-release.yml
172149
with:
173150
gitlint_version: ${{ needs.publish.outputs.gitlint_version }}
174-
pypi_source: ${{ inputs.pypi_target }}
151+
environment: ${{ inputs.environment }}
175152
repo_test_ref: ${{ inputs.repo_release_ref }}
176153

177154
publish-docker:

.github/workflows/test-release.yml

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Test Release
2-
run-name: "Test Release (${{ inputs.gitlint_version }}, pypi_source=${{ inputs.pypi_source }}, repo_test_ref=${{ inputs.repo_test_ref }})"
2+
run-name: "Test Release (${{ inputs.gitlint_version }}, environment=${{ inputs.environment }}, repo_test_ref=${{ inputs.repo_test_ref }})"
33
on:
44
workflow_call:
55
inputs:
@@ -8,10 +8,10 @@ on:
88
required: true
99
default: "0.18.0"
1010
type: string
11-
pypi_source:
12-
description: "PyPI repository to use"
13-
required: true
11+
environment:
12+
description: 'Gitlint package environment'
1413
type: string
14+
required: true
1515
repo_test_ref:
1616
description: "Git reference to checkout for integration tests"
1717
default: "main"
@@ -22,21 +22,18 @@ on:
2222
description: "Gitlint version to test"
2323
required: true
2424
default: "0.18.0"
25-
pypi_source:
26-
description: "PyPI repository to use"
25+
environment:
26+
description: 'Gitlint package environment'
27+
type: environment
2728
required: true
28-
type: choice
29-
options:
30-
- "pypi.org"
31-
- "test.pypi.org"
32-
default: "pypi.org"
3329
repo_test_ref:
3430
description: "Git reference to checkout for integration tests"
3531
default: "main"
3632

3733
jobs:
3834
test-release:
3935
timeout-minutes: 10
36+
environment: ${{ inputs.environment }}
4037
runs-on: "ubuntu-latest"
4138
strategy:
4239
matrix:
@@ -52,14 +49,10 @@ jobs:
5249
run: python -m pip install hatch==1.6.3
5350

5451
- name: Install gitlint
52+
# We need to add the --extra-index-url to the pip install command to deal with PYPI_TARGET=https://test.pypi.org/legacy,
53+
# because gitlint's dependencies are not available on Test PyPI
5554
run: |
56-
python -m pip install gitlint==${{ inputs.gitlint_version }}
57-
if: inputs.pypi_source == 'pypi.org'
58-
59-
- name: Install gitlint (test.pypi.org)
60-
run: |
61-
pip install --no-cache-dir -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple gitlint==${{ inputs.gitlint_version }}
62-
if: inputs.pypi_source == 'test.pypi.org'
55+
python -m pip install --no-cache-dir -i ${{ vars.PYPI_TARGET }} --extra-index-url https://pypi.org/simple gitlint==${{ inputs.gitlint_version }}
6356
6457
- name: gitlint --version
6558
run: |

0 commit comments

Comments
 (0)