|
| 1 | +name: Create Draft Pre-Release (on-demand) |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + upstream_workflow_name: |
| 7 | + description: "Name of the upstream build workflow to pull artifacts from" |
| 8 | + required: false |
| 9 | + default: "GitHub Actions OrgDbOrgSettings Build" |
| 10 | + upstream_branch: |
| 11 | + description: "Branch to search in the upstream workflow" |
| 12 | + required: false |
| 13 | + default: "master" |
| 14 | + dry_run: |
| 15 | + description: "If true, only preview title/notes and skip creating/editing the release" |
| 16 | + required: false |
| 17 | + default: "false" |
| 18 | + |
| 19 | +permissions: |
| 20 | + contents: write # needed to create/edit releases |
| 21 | + actions: read # needed to read runs & download artifacts from another run |
| 22 | + |
| 23 | +jobs: |
| 24 | + build_release_from_latest_success: |
| 25 | + runs-on: ubuntu-latest |
| 26 | + env: |
| 27 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 28 | + WF_NAME: ${{ inputs.upstream_workflow_name }} |
| 29 | + UPSTREAM_BRANCH: ${{ inputs.upstream_branch }} |
| 30 | + DRY_RUN: ${{ inputs.dry_run }} |
| 31 | + |
| 32 | + steps: |
| 33 | + - name: Checkout (shallow) |
| 34 | + uses: actions/checkout@v4 |
| 35 | + with: |
| 36 | + fetch-depth: 1 |
| 37 | + |
| 38 | + - name: Resolve latest successful upstream run on branch |
| 39 | + id: find_run |
| 40 | + shell: bash |
| 41 | + run: | |
| 42 | + set -euo pipefail |
| 43 | +
|
| 44 | + echo "Looking for latest successful run of workflow: '$WF_NAME' on branch: '$UPSTREAM_BRANCH' (event=push)" |
| 45 | + RUN_ID="$(gh run list \ |
| 46 | + --workflow "$WF_NAME" \ |
| 47 | + --branch "$UPSTREAM_BRANCH" \ |
| 48 | + --status success \ |
| 49 | + --event push \ |
| 50 | + --limit 1 \ |
| 51 | + --json databaseId \ |
| 52 | + --jq '.[0].databaseId')" |
| 53 | +
|
| 54 | + HEAD_SHA="$(gh run list \ |
| 55 | + --workflow "$WF_NAME" \ |
| 56 | + --branch "$UPSTREAM_BRANCH" \ |
| 57 | + --status success \ |
| 58 | + --event push \ |
| 59 | + --limit 1 \ |
| 60 | + --json headSha \ |
| 61 | + --jq '.[0].headSha')" |
| 62 | +
|
| 63 | + if [[ -z "${RUN_ID}" || "${RUN_ID}" == "null" || -z "${HEAD_SHA}" || "${HEAD_SHA}" == "null" ]]; then |
| 64 | + echo "ERROR: No successful run found for workflow '$WF_NAME' on branch '$UPSTREAM_BRANCH'." |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +
|
| 68 | + echo "run_id=${RUN_ID}" >> "$GITHUB_OUTPUT" |
| 69 | + echo "head_sha=${HEAD_SHA}" >> "$GITHUB_OUTPUT" |
| 70 | + echo "Upstream run id: ${RUN_ID}" |
| 71 | + echo "Upstream head sha: ${HEAD_SHA}" |
| 72 | +
|
| 73 | + - name: Download artifacts from that run |
| 74 | + uses: actions/download-artifact@v4 |
| 75 | + with: |
| 76 | + run-id: ${{ steps.find_run.outputs.run_id }} |
| 77 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + # Download all artifacts and filter files next |
| 79 | + path: artifacts |
| 80 | + merge-multiple: false |
| 81 | + - name: Locate the single ZIP matching OrganizationSettingsEditor_*_managed.zip |
| 82 | + id: find_zip |
| 83 | + shell: bash |
| 84 | + run: | |
| 85 | + set -euo pipefail |
| 86 | + mapfile -t matches < <(find artifacts -type f -regextype posix-extended \ |
| 87 | + -regex '.*/OrganizationSettingsEditor_[0-9]+(\.[0-9]+)*_managed\.zip' | sort) |
| 88 | +
|
| 89 | + echo "Found ${#matches[@]} candidate file(s):" |
| 90 | + printf ' - %s\n' "${matches[@]:-}" |
| 91 | +
|
| 92 | + if [[ ${#matches[@]} -eq 0 ]]; then |
| 93 | + echo "ERROR: No matching artifact file found." |
| 94 | + exit 1 |
| 95 | + elif [[ ${#matches[@]} -gt 1 ]]; then |
| 96 | + echo "ERROR: Multiple matching artifact files found; expected exactly one." |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | +
|
| 100 | + FILE="${matches[0]}" |
| 101 | + BASENAME="$(basename "$FILE")" |
| 102 | + if [[ "$BASENAME" =~ ^OrganizationSettingsEditor_([0-9]+(\.[0-9]+)*)_managed\.zip$ ]]; then |
| 103 | + VERSION="${BASH_REMATCH[1]}" |
| 104 | + else |
| 105 | + echo "ERROR: Could not parse version from $BASENAME" |
| 106 | + exit 1 |
| 107 | + fi |
| 108 | +
|
| 109 | + TAG="v${VERSION}" |
| 110 | +
|
| 111 | + echo "zip_path=$FILE" >> "$GITHUB_OUTPUT" |
| 112 | + echo "zip_name=$BASENAME" >> "$GITHUB_OUTPUT" |
| 113 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 114 | + echo "tag=$TAG" >> "$GITHUB_OUTPUT" |
| 115 | +
|
| 116 | + - name: Build release title & body from upstream commit message |
| 117 | + id: commit_msg |
| 118 | + shell: bash |
| 119 | + env: |
| 120 | + HEAD_SHA: ${{ steps.find_run.outputs.head_sha }} |
| 121 | + run: | |
| 122 | + set -euo pipefail |
| 123 | + # Retrieve canonical commit message for that SHA |
| 124 | + MSG="$(gh api repos/${GITHUB_REPOSITORY}/commits/${HEAD_SHA} --jq '.commit.message')" |
| 125 | + MSG="${MSG%\"}"; MSG="${MSG#\"}" # strip quotes if present |
| 126 | + printf '%s\n' "$MSG" > commit_message.txt |
| 127 | +
|
| 128 | + TITLE="$(head -n1 commit_message.txt || true)" |
| 129 | + BODY="$(tail -n +2 commit_message.txt || true)" |
| 130 | + BODY="$(printf '%s\n' "$BODY" | sed '1{/^$/d;}')" # trim leading blank lines |
| 131 | +
|
| 132 | + if [[ -z "$TITLE" ]]; then |
| 133 | + TITLE="Build ${{ steps.find_zip.outputs.version }}" |
| 134 | + fi |
| 135 | +
|
| 136 | + printf '%s\n' "$BODY" > release-notes.txt |
| 137 | +
|
| 138 | + { |
| 139 | + echo "title<<EOF" |
| 140 | + printf '%s\n' "$TITLE" |
| 141 | + echo "EOF" |
| 142 | + } >> "$GITHUB_OUTPUT" |
| 143 | +
|
| 144 | + - name: DRY RUN — print what would happen |
| 145 | + if: ${{ env.DRY_RUN == 'true' }} |
| 146 | + shell: bash |
| 147 | + run: | |
| 148 | + echo "DRY RUN: Would create/update release:" |
| 149 | + echo " Tag: ${{ steps.find_zip.outputs.tag }}" |
| 150 | + echo " Version: ${{ steps.find_zip.outputs.version }}" |
| 151 | + echo " Head SHA: ${{ steps.find_run.outputs.head_sha }}" |
| 152 | + echo " Title: ${{ steps.commit_msg.outputs.title }}" |
| 153 | + echo " Notes:" |
| 154 | + sed 's/^/ /' release-notes.txt |
| 155 | + echo " Asset: ${{ steps.find_zip.outputs.zip_path }}" |
| 156 | +
|
| 157 | + - name: Check if tag already has a release |
| 158 | + if: ${{ env.DRY_RUN != 'true' }} |
| 159 | + id: check_release |
| 160 | + shell: bash |
| 161 | + run: | |
| 162 | + set -euo pipefail |
| 163 | + TAG="${{ steps.find_zip.outputs.tag }}" |
| 164 | + if gh release view "$TAG" >/dev/null 2>&1; then |
| 165 | + echo "exists=true" >> "$GITHUB_OUTPUT" |
| 166 | + else |
| 167 | + echo "exists=false" >> "$GITHUB_OUTPUT" |
| 168 | + fi |
| 169 | +
|
| 170 | + - name: Create draft pre-release (first time) |
| 171 | + if: ${{ env.DRY_RUN != 'true' && steps.check_release.outputs.exists == 'false' }} |
| 172 | + shell: bash |
| 173 | + run: | |
| 174 | + set -euo pipefail |
| 175 | + TAG="${{ steps.find_zip.outputs.tag }}" |
| 176 | + ASSET="${{ steps.find_zip.outputs.zip_path }}" |
| 177 | + TITLE="${{ steps.commit_msg.outputs.title }}" |
| 178 | + HEAD_SHA="${{ steps.find_run.outputs.head_sha }}" |
| 179 | +
|
| 180 | + gh release create "$TAG" "$ASSET" \ |
| 181 | + --title "$TITLE" \ |
| 182 | + --notes-file release-notes.txt \ |
| 183 | + --draft \ |
| 184 | + --prerelease \ |
| 185 | + --latest \ |
| 186 | + --target "$HEAD_SHA" |
| 187 | +
|
| 188 | + - name: Update existing draft pre-release (idempotent) |
| 189 | + if: ${{ env.DRY_RUN != 'true' && steps.check_release.outputs.exists == 'true' }} |
| 190 | + shell: bash |
| 191 | + run: | |
| 192 | + set -euo pipefail |
| 193 | + TAG="${{ steps.find_zip.outputs.tag }}" |
| 194 | + ASSET="${{ steps.find_zip.outputs.zip_path }}" |
| 195 | + TITLE="${{ steps.commit_msg.outputs.title }}" |
| 196 | + HEAD_SHA="${{ steps.find_run.outputs.head_sha }}" |
| 197 | +
|
| 198 | + gh release edit "$TAG" \ |
| 199 | + --title "$TITLE" \ |
| 200 | + --notes-file release-notes.txt \ |
| 201 | + --draft \ |
| 202 | + --prerelease \ |
| 203 | + --latest \ |
| 204 | + --target "$HEAD_SHA" |
| 205 | +
|
| 206 | + gh release upload "$TAG" "$ASSET" --clobber |
0 commit comments