|
| 1 | +name: Publish package (manual) |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + confirm: |
| 7 | + description: "Confirm publish package" |
| 8 | + required: true |
| 9 | + type: boolean |
| 10 | + |
| 11 | +jobs: |
| 12 | + guard-ci-success: |
| 13 | + runs-on: windows-2022 |
| 14 | + permissions: |
| 15 | + contents: read |
| 16 | + statuses: read |
| 17 | + actions: read |
| 18 | + steps: |
| 19 | + - name: Ensure CI for this commit succeeded |
| 20 | + shell: pwsh |
| 21 | + env: |
| 22 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 23 | + run: | |
| 24 | + $ownerRepo = $env:GITHUB_REPOSITORY |
| 25 | + $sha = $env:GITHUB_SHA |
| 26 | + $headers = @{ |
| 27 | + Authorization = "token $env:GITHUB_TOKEN" |
| 28 | + Accept = 'application/vnd.github+json' |
| 29 | + 'X-GitHub-Api-Version' = '2022-11-28' |
| 30 | + } |
| 31 | +
|
| 32 | + # Query specific CI workflow runs and require a successful run for this commit |
| 33 | + $workflow = 'ci.yml' |
| 34 | + $branch = $env:GITHUB_REF_NAME |
| 35 | + $url = "https://api.github.com/repos/$ownerRepo/actions/workflows/$workflow/runs?branch=$branch&per_page=20" |
| 36 | +
|
| 37 | + try { |
| 38 | + $resp = Invoke-RestMethod -Method Get -Uri $url -Headers $headers -ErrorAction Stop |
| 39 | + } catch { |
| 40 | + Write-Error "Blocking publish: failed to query CI workflow runs ($($_.Exception.Message))" |
| 41 | + exit 1 |
| 42 | + } |
| 43 | +
|
| 44 | + if (-not $resp.workflow_runs) { |
| 45 | + Write-Error "Blocking publish: no CI runs found on branch '$branch'" |
| 46 | + exit 1 |
| 47 | + } |
| 48 | +
|
| 49 | + $matching = $resp.workflow_runs | Where-Object { $_.head_sha -eq $sha -and $_.status -eq 'completed' } |
| 50 | + if (-not $matching) { |
| 51 | + Write-Error "Blocking publish: no completed CI run found for commit $sha" |
| 52 | + exit 1 |
| 53 | + } |
| 54 | +
|
| 55 | + $success = $matching | Where-Object { $_.conclusion -eq 'success' } | Select-Object -First 1 |
| 56 | + if (-not $success) { |
| 57 | + $concl = ($matching | Select-Object -First 1).conclusion |
| 58 | + Write-Error "Blocking publish: CI conclusion for $sha is '$concl'" |
| 59 | + exit 1 |
| 60 | + } |
| 61 | +
|
| 62 | + build-and-publish: |
| 63 | + needs: guard-ci-success |
| 64 | + if: ${{ github.event.inputs.confirm == 'true' && startsWith(github.ref_name, 'release/') }} |
| 65 | + runs-on: windows-2022 |
| 66 | + permissions: |
| 67 | + contents: write |
| 68 | + pages: write |
| 69 | + id-token: write |
| 70 | + |
| 71 | + steps: |
| 72 | + - name: Checkout |
| 73 | + uses: actions/checkout@v5 |
| 74 | + |
| 75 | + - name: Set up Python |
| 76 | + uses: actions/setup-python@v5 |
| 77 | + with: |
| 78 | + python-version: '3.13' |
| 79 | + |
| 80 | + - name: Check if version exists on PyPI |
| 81 | + id: pypi_check |
| 82 | + shell: pwsh |
| 83 | + run: | |
| 84 | + $packageName = 'moldflow' |
| 85 | +
|
| 86 | + try { |
| 87 | + $resp = Invoke-WebRequest -Uri "https://test.pypi.org/pypi/$packageName/json" -UseBasicParsing -ErrorAction Stop |
| 88 | + $data = $resp.Content | ConvertFrom-Json |
| 89 | + $versions = @($data.releases.PSObject.Properties.Name) |
| 90 | + } catch { |
| 91 | + $versions = @() |
| 92 | + } |
| 93 | +
|
| 94 | + $versionJson = Get-Content -Raw -Path version.json | ConvertFrom-Json |
| 95 | + $patch = if ($env:BUILD_NUMBER) { $env:BUILD_NUMBER } else { "$($versionJson.patch)" } |
| 96 | + $version = "$($versionJson.major).$($versionJson.minor).$patch" |
| 97 | + $exists = if ($versions -contains $version) { 'true' } else { 'false' } |
| 98 | + Write-Output "Release $version exists on PyPI: $exists" |
| 99 | + "exists=$exists" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8 |
| 100 | + "version=$version" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8 |
| 101 | +
|
| 102 | + - name: Skip publish, version already exists |
| 103 | + if: steps.pypi_check.outputs.exists == 'true' |
| 104 | + run: Write-Output "Version ${{ steps.pypi_check.outputs.version }} already exists on PyPI. Skipping publish." |
| 105 | + |
| 106 | + - name: Install build dependencies |
| 107 | + if: steps.pypi_check.outputs.exists == 'false' |
| 108 | + run: | |
| 109 | + python -m pip install --upgrade pip |
| 110 | + pip install -r requirements.txt |
| 111 | +
|
| 112 | + - name: Build package |
| 113 | + if: steps.pypi_check.outputs.exists == 'false' |
| 114 | + run: | |
| 115 | + python run.py build |
| 116 | +
|
| 117 | + - name: Publish to PyPI |
| 118 | + if: steps.pypi_check.outputs.exists == 'false' |
| 119 | + env: |
| 120 | + TWINE_USERNAME: __token__ |
| 121 | + TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }} |
| 122 | + run: | |
| 123 | + python run.py publish --skip-build --testpypi |
| 124 | +
|
| 125 | + - name: Create GitHub Release |
| 126 | + if: steps.pypi_check.outputs.exists == 'false' |
| 127 | + run: | |
| 128 | + python run.py release |
| 129 | + env: |
| 130 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 131 | + |
| 132 | + - name: Configure Pages |
| 133 | + uses: actions/configure-pages@v5 |
| 134 | + |
| 135 | + - name: Build documentation |
| 136 | + run: python run.py build-docs |
| 137 | + |
| 138 | + - name: Upload Pages artifact |
| 139 | + uses: actions/upload-pages-artifact@v4 |
| 140 | + with: |
| 141 | + path: ./docs/build/html |
| 142 | + |
| 143 | + - name: Deploy to GitHub Pages |
| 144 | + id: deployment |
| 145 | + uses: actions/deploy-pages@v4 |
0 commit comments