|
| 1 | +name: Create GitHub Release (manual) |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + confirm: |
| 7 | + description: "Confirm create release" |
| 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 | + $workflow = 'ci.yml' |
| 33 | + $branch = $env:GITHUB_REF_NAME |
| 34 | + $url = "https://api.github.com/repos/$ownerRepo/actions/workflows/$workflow/runs?branch=$branch&per_page=20" |
| 35 | +
|
| 36 | + try { |
| 37 | + $resp = Invoke-RestMethod -Method Get -Uri $url -Headers $headers -ErrorAction Stop |
| 38 | + } catch { |
| 39 | + Write-Error "Blocking release: failed to query CI workflow runs ($($_.Exception.Message))" |
| 40 | + exit 1 |
| 41 | + } |
| 42 | +
|
| 43 | + if (-not $resp.workflow_runs) { |
| 44 | + Write-Error "Blocking release: no CI runs found on branch '$branch'" |
| 45 | + exit 1 |
| 46 | + } |
| 47 | +
|
| 48 | + $matching = $resp.workflow_runs | Where-Object { $_.head_sha -eq $sha -and $_.status -eq 'completed' } |
| 49 | + if (-not $matching) { |
| 50 | + Write-Error "Blocking release: no completed CI run found for commit $sha" |
| 51 | + exit 1 |
| 52 | + } |
| 53 | +
|
| 54 | + $success = $matching | Where-Object { $_.conclusion -eq 'success' } | Select-Object -First 1 |
| 55 | + if (-not $success) { |
| 56 | + $concl = ($matching | Select-Object -First 1).conclusion |
| 57 | + Write-Error "Blocking release: CI conclusion for $sha is '$concl'" |
| 58 | + exit 1 |
| 59 | + } |
| 60 | +
|
| 61 | + build-and-release: |
| 62 | + needs: guard-ci-success |
| 63 | + if: ${{ github.event.inputs.confirm == 'true' && startsWith(github.ref_name, 'release/') }} |
| 64 | + runs-on: windows-2022 |
| 65 | + permissions: |
| 66 | + contents: write |
| 67 | + |
| 68 | + steps: |
| 69 | + - name: Checkout |
| 70 | + uses: actions/checkout@v5 |
| 71 | + |
| 72 | + - name: Set up Python |
| 73 | + uses: actions/setup-python@v5 |
| 74 | + with: |
| 75 | + python-version: '3.13' |
| 76 | + |
| 77 | + - name: Install dependencies |
| 78 | + run: | |
| 79 | + python -m pip install --upgrade pip |
| 80 | + pip install -r requirements.txt |
| 81 | +
|
| 82 | + - name: Check release tag does not already exist |
| 83 | + shell: pwsh |
| 84 | + env: |
| 85 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 86 | + run: | |
| 87 | + $ownerRepo = $env:GITHUB_REPOSITORY |
| 88 | + $versionJson = Get-Content -Raw -Path version.json | ConvertFrom-Json |
| 89 | + $tag = "v$($versionJson.major).$($versionJson.minor).$($versionJson.patch)" |
| 90 | + $headers = @{ |
| 91 | + Authorization = "token $env:GITHUB_TOKEN" |
| 92 | + Accept = 'application/vnd.github+json' |
| 93 | + 'X-GitHub-Api-Version' = '2022-11-28' |
| 94 | + } |
| 95 | +
|
| 96 | + $url = "https://api.github.com/repos/$ownerRepo/releases/tags/$tag" |
| 97 | + try { |
| 98 | + $release = Invoke-RestMethod -Method Get -Uri $url -Headers $headers -ErrorAction Stop |
| 99 | + Write-Error "Release tag '$tag' already exists (id: $($release.id)). Bump version.json before creating a new release." |
| 100 | + exit 1 |
| 101 | + } catch { |
| 102 | + $statusCode = $null |
| 103 | + if ($_.Exception.Response) { |
| 104 | + $statusCode = [int]$_.Exception.Response.StatusCode |
| 105 | + } |
| 106 | + if ($statusCode -eq 404) { |
| 107 | + Write-Output "Release tag '$tag' not found — proceeding." |
| 108 | + } else { |
| 109 | + Write-Error "Failed to check for existing release tag '$tag': $($_.Exception.Message)" |
| 110 | + exit 1 |
| 111 | + } |
| 112 | + } |
| 113 | +
|
| 114 | + - name: Build package |
| 115 | + run: python run.py build |
| 116 | + |
| 117 | + - name: Create GitHub Release |
| 118 | + run: python run.py release |
| 119 | + env: |
| 120 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments