Skip to content

Commit

Permalink
GHA: Use GHA environments to select PyPI target (#487)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jorisroovers authored Apr 13, 2023
1 parent 562703f commit 8a38a58
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ jobs:
uses: ./.github/workflows/publish-release.yml
secrets: inherit # pass all secrets (required to access secrets in a called workflow)
with:
pypi_target: "pypi.org"
environment: "production"
repo_release_ref: "main"
docker_image_tag: "latest_dev"
53 changes: 15 additions & 38 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
name: Publish Release
run-name: "Publish Release (pypi_target=${{ inputs.pypi_target }}, repo_release_ref=${{ inputs.repo_release_ref }})"
run-name: "Publish Release (environment=${{ inputs.environment }}, repo_release_ref=${{ inputs.repo_release_ref }})"

on:
# Trigger release workflow from other workflows (e.g. release dev build as part of CI)
workflow_call:
inputs:
pypi_target:
description: "PyPI repository to publish to"
required: true
environment:
description: "Target environment"
type: string
default: "test.pypi.org"
required: true
repo_release_ref:
description: "Gitlint git reference to publish release for"
type: string
Expand All @@ -23,14 +22,10 @@ on:
# Manually trigger a release
workflow_dispatch:
inputs:
pypi_target:
description: "PyPI repository to publish to"
environment:
description: "Target environment"
type: environment
required: true
type: choice
options:
- "pypi.org"
- "test.pypi.org"
default: "test.pypi.org"
repo_release_ref:
description: "Gitlint git reference to publish release for"
type: string
Expand All @@ -49,6 +44,7 @@ jobs:
publish:
timeout-minutes: 15
runs-on: "ubuntu-latest"
environment: ${{ inputs.environment }}
permissions:
# Required for trusted publishing to PyPI
id-token: write
Expand Down Expand Up @@ -110,27 +106,16 @@ jobs:
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: gitlint-core/dist/
if: inputs.pypi_target == 'pypi.org'
repository-url: ${{ vars.PYPI_TARGET }} # PYPI_TARGET is defined in the GHA environment

- name: Publish gitlint 🐍📦 to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
if: inputs.pypi_target == 'pypi.org'

- name: Publish gitlint-core 🐍📦 to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: gitlint-core/dist/
repository-url: https://test.pypi.org/legacy/
if: inputs.pypi_target == 'test.pypi.org'

- name: Publish gitlint 🐍📦 to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/
if: inputs.pypi_target == 'test.pypi.org'
repository-url: ${{ vars.PYPI_TARGET }} # PYPI_TARGET is defined in the GHA environment

# Wait for gitlint package to be available in PyPI for installation
wait-for-package:
environment: ${{ inputs.environment }}
needs:
- publish
runs-on: "ubuntu-latest"
Expand All @@ -140,18 +125,10 @@ jobs:
with:
timeout_minutes: 1
max_attempts: 10
# We need to add the --extra-index-url to the pip install command to deal with PYPI_TARGET=https://test.pypi.org/legacy,
# because gitlint's dependencies are not available on Test PyPI
command: |
python -m pip install gitlint==${{ needs.publish.outputs.gitlint_version }}
if: inputs.pypi_target == 'pypi.org'

- name: Install gitlint (test.pypi.org)
uses: nick-fields/[email protected]
with:
timeout_minutes: 1
max_attempts: 10
command: |
pip install --no-cache-dir -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple gitlint==${{ needs.publish.outputs.gitlint_version }}
if: inputs.pypi_target == 'test.pypi.org'
python -m pip install --no-cache-dir -i ${{ vars.PYPI_TARGET }} --extra-index-url https://pypi.org/simple gitlint==${{ needs.publish.outputs.gitlint_version }}
- name: gitlint --version
run: |
Expand All @@ -171,7 +148,7 @@ jobs:
uses: ./.github/workflows/test-release.yml
with:
gitlint_version: ${{ needs.publish.outputs.gitlint_version }}
pypi_source: ${{ inputs.pypi_target }}
environment: ${{ inputs.environment }}
repo_test_ref: ${{ inputs.repo_release_ref }}

publish-docker:
Expand Down
29 changes: 11 additions & 18 deletions .github/workflows/test-release.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Test Release
run-name: "Test Release (${{ inputs.gitlint_version }}, pypi_source=${{ inputs.pypi_source }}, repo_test_ref=${{ inputs.repo_test_ref }})"
run-name: "Test Release (${{ inputs.gitlint_version }}, environment=${{ inputs.environment }}, repo_test_ref=${{ inputs.repo_test_ref }})"
on:
workflow_call:
inputs:
Expand All @@ -8,10 +8,10 @@ on:
required: true
default: "0.18.0"
type: string
pypi_source:
description: "PyPI repository to use"
required: true
environment:
description: 'Gitlint package environment'
type: string
required: true
repo_test_ref:
description: "Git reference to checkout for integration tests"
default: "main"
Expand All @@ -22,21 +22,18 @@ on:
description: "Gitlint version to test"
required: true
default: "0.18.0"
pypi_source:
description: "PyPI repository to use"
environment:
description: 'Gitlint package environment'
type: environment
required: true
type: choice
options:
- "pypi.org"
- "test.pypi.org"
default: "pypi.org"
repo_test_ref:
description: "Git reference to checkout for integration tests"
default: "main"

jobs:
test-release:
timeout-minutes: 10
environment: ${{ inputs.environment }}
runs-on: "ubuntu-latest"
strategy:
matrix:
Expand All @@ -52,14 +49,10 @@ jobs:
run: python -m pip install hatch==1.6.3

- name: Install gitlint
# We need to add the --extra-index-url to the pip install command to deal with PYPI_TARGET=https://test.pypi.org/legacy,
# because gitlint's dependencies are not available on Test PyPI
run: |
python -m pip install gitlint==${{ inputs.gitlint_version }}
if: inputs.pypi_source == 'pypi.org'

- name: Install gitlint (test.pypi.org)
run: |
pip install --no-cache-dir -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple gitlint==${{ inputs.gitlint_version }}
if: inputs.pypi_source == 'test.pypi.org'
python -m pip install --no-cache-dir -i ${{ vars.PYPI_TARGET }} --extra-index-url https://pypi.org/simple gitlint==${{ inputs.gitlint_version }}
- name: gitlint --version
run: |
Expand Down

0 comments on commit 8a38a58

Please sign in to comment.