Skip to content

Commit bf81e2d

Browse files
fix(ci): dispatch frozen-line builds with GITHUB_TOKEN
The CI app lacks Actions write access, so freezing a new release line leaves its initial packages build undispatched. Use the repository token for workflow_dispatch without expanding the app installation permissions. Signed-off-by: Myasnikov Daniil <[email protected]> Assisted-by: LLM (cherry picked from commit e05cef4) Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent 673620c commit bf81e2d

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

‎.github/workflows/cut-prerelease.yaml‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ concurrency:
4848
group: cut-prerelease
4949
cancel-in-progress: false
5050

51-
# The tag write happens via the app token, not GITHUB_TOKEN, so the job needs
52-
# only read scope on the default token.
51+
# Tag and branch writes use the app token so their push events trigger CI.
5352
permissions:
5453
contents: read
5554

@@ -58,6 +57,9 @@ jobs:
5857
name: Cut ${{ inputs.tag }}
5958
runs-on: ubuntu-latest
6059
timeout-minutes: 10
60+
permissions:
61+
contents: read
62+
actions: write
6163

6264
steps:
6365
# Pure-input validation first — no token minted, no checkout, so a bad tag
@@ -345,13 +347,14 @@ jobs:
345347
- name: Build the frozen line's packages artifact
346348
if: steps.freeze.outputs.created == 'true'
347349
env:
348-
GH_TOKEN: ${{ steps.app-token.outputs.token }}
350+
# workflow_dispatch can trigger a run with GITHUB_TOKEN.
351+
GH_TOKEN: ${{ github.token }}
349352
BRANCH: release-${{ steps.parse.outputs.line }}
350353
run: |
351354
if gh workflow run build-release.yaml --ref "$BRANCH"; then
352355
echo "Dispatched build-release.yaml for $BRANCH; it publishes cozystack-packages:$BRANCH."
353356
else
354-
echo "::warning::Could not dispatch build-release.yaml for $BRANCH (the CI app may lack 'actions: write')."
357+
echo "::warning::Could not dispatch build-release.yaml for $BRANCH."
355358
echo "::warning::Until it runs, cozystack-packages:$BRANCH does not exist, and PRs onto $BRANCH test the image refs committed in the tree rather than the line's own build."
356359
echo "::warning::Start it by hand: gh workflow run build-release.yaml --ref $BRANCH"
357360
fi

‎hack/release-freeze-contract.bats‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,39 @@ step_line() {
204204
[ "${count:-0}" -eq 1 ]
205205
}
206206

207+
@test "initial line build dispatch has Actions access and preserves failure recovery" {
208+
step="$(STEP_NAME="Build the frozen line's packages artifact" yq -o=json '.jobs.cut.steps[] | select(.name == strenv(STEP_NAME))' "$CUT")"
209+
script="$(printf '%s\n' "$step" | yq -r '.run')"
210+
token="$(printf '%s\n' "$step" | yq -r '.env.GH_TOKEN')"
211+
[ -n "$script" ] && [ "$script" != null ]
212+
case "$token" in
213+
'${{ github.token }}'|'${{ secrets.GITHUB_TOKEN }}') token=workflow-token ;;
214+
'${{ steps.app-token.outputs.token }}') token=app-token ;;
215+
*) echo "Unknown dispatch credential: $token"; return 1 ;;
216+
esac
217+
actions="$(yq -r '(.jobs.cut.permissions // .permissions).actions // "none"' "$CUT")"
218+
219+
# The installed CI app has no Actions permission; the workflow token only
220+
# gets it when the effective job permissions request it.
221+
gh() {
222+
[ "$*" = 'workflow run build-release.yaml --ref release-1.7' ] || return 2
223+
[ "$GH_TOKEN" = workflow-token ] && [ "$ACTIONS_PERMISSION" = write ] || return 1
224+
return "$DISPATCH_STATUS"
225+
}
226+
export -f gh
227+
228+
run env GH_TOKEN="$token" ACTIONS_PERMISSION="$actions" BRANCH=release-1.7 \
229+
DISPATCH_STATUS=0 bash -e -c "$script"
230+
[ "$status" -eq 0 ]
231+
[[ "$output" == *'Dispatched build-release.yaml for release-1.7;'* ]]
232+
233+
run env GH_TOKEN="$token" ACTIONS_PERMISSION="$actions" BRANCH=release-1.7 \
234+
DISPATCH_STATUS=1 bash -e -c "$script"
235+
[ "$status" -eq 0 ]
236+
[[ "$output" == *'::warning::Could not dispatch build-release.yaml for release-1.7'* ]]
237+
[[ "$output" == *'Start it by hand: gh workflow run build-release.yaml --ref release-1.7'* ]]
238+
}
239+
207240
@test "build-release refuses a dispatch from anything but a line branch" {
208241
BUILD_RELEASE="$REPO_ROOT/.github/workflows/build-release.yaml"
209242
[ -f "$BUILD_RELEASE" ]

0 commit comments

Comments
 (0)