|
| 1 | +name: Publish PyPI Package |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + tag: |
| 7 | + description: 'Tag to publish' |
| 8 | + required: true |
| 9 | + environment: |
| 10 | + description: 'PyPI environment' |
| 11 | + required: true |
| 12 | + type: choice |
| 13 | + options: |
| 14 | + - test |
| 15 | + - prod |
| 16 | + default: 'test' |
| 17 | + |
| 18 | +jobs: |
| 19 | + verify-builds: |
| 20 | + name: Verify build workflows passed |
| 21 | + runs-on: ubuntu-latest |
| 22 | + steps: |
| 23 | + - name: Verify build workflows |
| 24 | + uses: actions/github-script@v6 |
| 25 | + with: |
| 26 | + script: | |
| 27 | + const { owner, repo } = context.repo; |
| 28 | + const tag = "${{ github.event.inputs.tag }}"; |
| 29 | + const requiredWorkflows = ['Windows Distribution', 'Python Distribution']; |
| 30 | + let workflowConclusions = {}; |
| 31 | +
|
| 32 | + console.log(`Checking for successful workflow runs for tag: ${tag}`); |
| 33 | +
|
| 34 | + const { data: response } = await github.rest.actions.listWorkflowRunsForRepo({ |
| 35 | + owner, |
| 36 | + repo, |
| 37 | + event: 'push', |
| 38 | + }); |
| 39 | +
|
| 40 | + const runsForTag = response.workflow_runs.filter(run => run.head_branch === tag); |
| 41 | +
|
| 42 | + for (const run of runsForTag) { |
| 43 | + if (requiredWorkflows.includes(run.name)) { |
| 44 | + if (!workflowConclusions[run.name] || new Date(run.created_at) > new Date(workflowConclusions[run.name].created_at)) { |
| 45 | + workflowConclusions[run.name] = { |
| 46 | + conclusion: run.conclusion, |
| 47 | + created_at: run.created_at, |
| 48 | + html_url: run.html_url, |
| 49 | + }; |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +
|
| 54 | + let allSuccess = true; |
| 55 | + for (const workflowName of requiredWorkflows) { |
| 56 | + if (!workflowConclusions[workflowName]) { |
| 57 | + core.setFailed(`Workflow "${workflowName}" was not found for tag ${tag}.`); |
| 58 | + allSuccess = false; |
| 59 | + } else if (workflowConclusions[workflowName].conclusion !== 'success') { |
| 60 | + core.setFailed(`Workflow "${workflowName}" did not succeed for tag ${tag}. Conclusion was "${workflowConclusions[workflowName].conclusion}". See: ${workflowConclusions[workflowName].html_url}`); |
| 61 | + allSuccess = false; |
| 62 | + } else { |
| 63 | + console.log(`✅ Workflow "${workflowName}" succeeded for tag ${tag}.`); |
| 64 | + } |
| 65 | + } |
| 66 | +
|
| 67 | + if (!allSuccess) { |
| 68 | + throw new Error("One or more required build workflows did not succeed."); |
| 69 | + } |
| 70 | +
|
| 71 | + build-and-publish: |
| 72 | + name: Build and publish Python distributions to ${{ github.event.inputs.environment }} PyPI |
| 73 | + runs-on: ubuntu-latest |
| 74 | + needs: verify-builds |
| 75 | + |
| 76 | + |
| 77 | + environment: |
| 78 | + name: ${{ github.event.inputs.environment == 'test' && 'testpypi' || 'pypi' }} |
| 79 | + url: ${{ github.event.inputs.environment == 'test' && 'https://test.pypi.org/p/scenedetect' || 'https://pypi.org/p/scenedetect' }} |
| 80 | + |
| 81 | + permissions: |
| 82 | + id-token: write # IMPORTANT: mandatory for trusted publishing |
| 83 | + |
| 84 | + steps: |
| 85 | + - name: Checkout code |
| 86 | + uses: actions/checkout@v3 |
| 87 | + with: |
| 88 | + ref: ${{ github.event.inputs.tag }} |
| 89 | + |
| 90 | + - name: Set up Python |
| 91 | + uses: actions/setup-python@v3 |
| 92 | + with: |
| 93 | + python-version: "3.x" |
| 94 | + |
| 95 | + - name: Install dependencies |
| 96 | + run: | |
| 97 | + python -m pip install --upgrade pip |
| 98 | + pip install build twine |
| 99 | +
|
| 100 | + - name: Build package |
| 101 | + run: python -m build |
| 102 | + |
| 103 | + - name: Publish package |
| 104 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 105 | + with: |
| 106 | + repository-url: ${{ github.event.inputs.environment == 'test' && 'https://test.pypi.org/legacy/' || 'https://upload.pypi.org/legacy/' }} |
0 commit comments