Publish PyPI Package #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish PyPI Package | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| tag: | |
| description: 'Tag To Publish' | |
| required: true | |
| environment: | |
| description: 'PyPI Environment' | |
| required: true | |
| type: choice | |
| options: | |
| - test | |
| - release | |
| default: 'test' | |
| jobs: | |
| verify: | |
| name: Verify Build | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check workflows | |
| uses: actions/github-script@v6 | |
| with: | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const tag = "${{ github.event.inputs.tag }}"; | |
| const requiredWorkflows = ['Windows Distribution', 'Python Distribution']; | |
| let workflowConclusions = {}; | |
| console.log(`Checking for successful workflow runs for tag: ${tag}`); | |
| const { data: response } = await github.rest.actions.listWorkflowRunsForRepo({ | |
| owner, | |
| repo, | |
| event: 'push', | |
| }); | |
| const runsForTag = response.workflow_runs.filter(run => run.head_branch === tag); | |
| for (const run of runsForTag) { | |
| if (requiredWorkflows.includes(run.name)) { | |
| if (!workflowConclusions[run.name] || new Date(run.created_at) > new Date(workflowConclusions[run.name].created_at)) { | |
| workflowConclusions[run.name] = { | |
| conclusion: run.conclusion, | |
| created_at: run.created_at, | |
| html_url: run.html_url, | |
| }; | |
| } | |
| } | |
| } | |
| let allSuccess = true; | |
| for (const workflowName of requiredWorkflows) { | |
| if (!workflowConclusions[workflowName]) { | |
| core.setFailed(`Workflow "${workflowName}" was not found for tag ${tag}.`); | |
| allSuccess = false; | |
| } else if (workflowConclusions[workflowName].conclusion !== 'success') { | |
| core.setFailed(`Workflow "${workflowName}" did not succeed for tag ${tag}. Conclusion was "${workflowConclusions[workflowName].conclusion}". See: ${workflowConclusions[workflowName].html_url}`); | |
| allSuccess = false; | |
| } else { | |
| console.log(`✅ Workflow "${workflowName}" succeeded for tag ${tag}.`); | |
| } | |
| } | |
| if (!allSuccess) { | |
| throw new Error("One or more required build workflows did not succeed."); | |
| } | |
| publish: | |
| env: | |
| UV_SYSTEM_PYTHON: 1 | |
| name: Publishing DVR-Scan to ${{ github.event.inputs.environment }} PyPI | |
| runs-on: ubuntu-latest | |
| needs: verify | |
| environment: | |
| name: ${{ github.event.inputs.environment == 'test' && 'test' || 'release' }} | |
| permissions: | |
| id-token: write # IMPORTANT: mandatory for trusted publishing | |
| steps: | |
| - name: Checkout ${{ github.event.inputs.tag }} | |
| uses: actions/checkout@v3 | |
| with: | |
| ref: ${{ github.event.inputs.tag }} | |
| - name: Set up Python | |
| uses: actions/setup-python@v3 | |
| with: | |
| python-version: 3.13 | |
| - name: Install uv and set the python version | |
| uses: astral-sh/setup-uv@v4 | |
| with: | |
| version: "0.5.11" | |
| python-version: 3.13 | |
| - name: Install Dependencies | |
| run: | | |
| uv pip install --upgrade build wheel virtualenv | |
| uv pip install opencv-python-headless opencv-contrib-python-headless --only-binary :all: | |
| uv pip install -r requirements_headless.txt -r docs/requirements.txt | |
| - name: Build Package | |
| run: | | |
| python dist/pre_release.py | |
| python -m build | |
| python dist/tweak_setup_cfg_for_headless.py | |
| python -m build | |
| mkdir pkg | |
| mv dist/*.tar.gz pkg/ | |
| mv dist/*.whl pkg/ | |
| - name: Upload Package | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: dvr-scan-dist | |
| path: | | |
| pkg/*.tar.gz | |
| pkg/*.whl | |
| - name: Publish Package | |
| uses: pypa/gh-action-pypi-publish@release/v1 | |
| with: | |
| repository-url: ${{ github.event.inputs.environment == 'test' && 'https://test.pypi.org/legacy/' || 'https://upload.pypi.org/legacy/' }} | |
| packages-dir: pkg/ | |
| print-hash: true | |