|
| 1 | +--- |
| 2 | +name: Publish to pypi |
| 3 | +on: |
| 4 | + push: |
| 5 | + #On versioned releases |
| 6 | + tags: |
| 7 | + - '*.*.*' |
| 8 | + # Allows you to run this workflow manually from the Actions tab |
| 9 | + workflow_dispatch: |
| 10 | + inputs: |
| 11 | + force: |
| 12 | + type: choice |
| 13 | + description: Retry Publish Version |
| 14 | + options: |
| 15 | + - No |
| 16 | + - Yes |
| 17 | + environment: |
| 18 | + description: 'Deployment environment' |
| 19 | + required: true |
| 20 | + default: 'pypi' |
| 21 | + type: choice |
| 22 | + options: |
| 23 | + - pypi |
| 24 | + - testpypi |
| 25 | + dryRun: |
| 26 | + description: 'Dry Run deployment (set to false to deploy)' |
| 27 | + required: true |
| 28 | + type: boolean |
| 29 | + default: true |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +jobs: |
| 34 | + |
| 35 | + validateVersion: |
| 36 | + runs-on: ubuntu-latest |
| 37 | + if: github.repository == 'duttonw/ckanext-pycharm-debugger' |
| 38 | + steps: |
| 39 | + - uses: actions/checkout@v4 |
| 40 | + |
| 41 | + - uses: actions/setup-python@v5 |
| 42 | + with: |
| 43 | + python-version: '3.10' |
| 44 | + |
| 45 | + - name: Validate tag version |
| 46 | + if: ${{ startsWith(github.ref, 'refs/tags') }} |
| 47 | + run: | |
| 48 | + TAG_VALUE=${GITHUB_REF/refs\/tags\//} |
| 49 | + PYTHON_VERSION=$(grep -E '\bversion\s?=\s?"[^"]+"' pyproject.toml | awk -F '"' '{print $2}') |
| 50 | + echo "Tag version is [$TAG_VALUE], Python version is [$PYTHON_VERSION]" |
| 51 | + if [ "$TAG_VALUE" != "$PYTHON_VERSION" ]; then |
| 52 | + echo "Version mismatch; tag version is [$TAG_VALUE] but Python version is [$PYTHON_VERSION]" >> $GITHUB_STEP_SUMMARY |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | +
|
| 56 | + test: |
| 57 | + needs: validateVersion |
| 58 | + name: Test |
| 59 | + uses: ./.github/workflows/test.yml # Call the reusable workflow |
| 60 | + |
| 61 | + publishSkipped: |
| 62 | + if: github.repository != 'duttonw/ckanext-pycharm-debugger' |
| 63 | + runs-on: ubuntu-latest |
| 64 | + steps: |
| 65 | + - run: | |
| 66 | + echo "## Skipping PyPI publish on downstream repository" >> $GITHUB_STEP_SUMMARY |
| 67 | +
|
| 68 | + publish: |
| 69 | + needs: test |
| 70 | + permissions: |
| 71 | + id-token: write # IMPORTANT: this permission is mandatory for trusted publishing |
| 72 | + name: Publish Package |
| 73 | + runs-on: ubuntu-latest |
| 74 | + environment: |
| 75 | + name: ${{ github.event.inputs.environment || 'pypi' }} |
| 76 | + url: ${{ steps.version.outputs.url }} |
| 77 | + concurrency: |
| 78 | + group: ${{ github.event.inputs.environment }}-deployment |
| 79 | + cancel-in-progress: false |
| 80 | + env: |
| 81 | + ENVIRONMENT: ${{ github.event.inputs.environment || 'pypi' }} |
| 82 | + steps: |
| 83 | + - name: Get Git Tag and set url from environment |
| 84 | + id: version |
| 85 | + run: | |
| 86 | + #!/bin/bash |
| 87 | +
|
| 88 | + TAG_VALUE=${GITHUB_REF/refs\/tags\//} |
| 89 | + echo "version=${TAG_VALUE}" >> $GITHUB_OUTPUT |
| 90 | +
|
| 91 | + # Extract the repository name (minus the owner/org) |
| 92 | + reponame=$(basename $GITHUB_REPOSITORY) |
| 93 | + echo "reponame=${reponame}" >> $GITHUB_OUTPUT |
| 94 | +
|
| 95 | + if [ "$env.ENVIRONMENT" == "testpypi" ]; then |
| 96 | + url="https://test.pypi.org/project/$reponame/$TAG_VALUE/" |
| 97 | + echo "environment=${env.ENVIRONMENT}" >> $GITHUB_OUTPUT |
| 98 | + else |
| 99 | + url="https://pypi.org/project/$reponame/$TAG_VALUE/" |
| 100 | + echo "environment=pypi" >> $GITHUB_OUTPUT |
| 101 | + fi |
| 102 | +
|
| 103 | + echo "url=${url}" >> $GITHUB_OUTPUT |
| 104 | +
|
| 105 | + - name: Checkout repository |
| 106 | + uses: actions/checkout@v4 |
| 107 | + |
| 108 | + - name: Build package ${{ steps.version.outputs.reponame }} @ ${{ steps.version.outputs.version }} |
| 109 | + run: | |
| 110 | + pip install build |
| 111 | + pip install twine |
| 112 | + python -m build |
| 113 | + - name: Publish package distributions to PyPI |
| 114 | + if: ${{ startsWith(github.ref, 'refs/tags') && steps.version.outputs.environment == 'pypi' && github.event.inputs.dryRun != 'true' }} |
| 115 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 116 | +# with: |
| 117 | +# skip-existing: true |
| 118 | +# verbose: true |
| 119 | +# print-hash: true |
| 120 | + - name: Test Publish package distributions to PyPI |
| 121 | + if: ${{ startsWith(github.ref, 'refs/tags') && steps.version.outputs.environment == 'testpypi' && github.event.inputs.dryRun == 'true' }} |
| 122 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 123 | + with: |
| 124 | + repository-url: https://test.pypi.org/legacy/ |
| 125 | +# skip-existing: true |
| 126 | +# verbose: true |
| 127 | +# print-hash: true |
| 128 | + - name: Summary output |
| 129 | + if: ${{ startsWith(github.ref, 'refs/tags') && github.event.inputs.dryRun != 'true' }} |
| 130 | + run: |
| 131 | + echo "Published ${{ steps.version.outputs.repo_name }} @ ${{ steps.version.outputs.version }} to ${{ steps.version.outputs.url }}" >> $GITHUB_STEP_SUMMARY |
| 132 | + |
| 133 | + - name: (TEST RUN) Test Publish package distributions to PyPI |
| 134 | + if: ${{ github.event.inputs.dryRun == 'true' }} |
| 135 | + run: |
| 136 | + echo "Dry run deployment, did not publish" >> $GITHUB_STEP_SUMMARY |
0 commit comments