diff --git a/.github/scripts/nightly-publish.sh b/.github/scripts/nightly-publish.sh index 0350848..fadf704 100755 --- a/.github/scripts/nightly-publish.sh +++ b/.github/scripts/nightly-publish.sh @@ -1,8 +1,13 @@ #!/usr/bin/env bash # nightly-publish.sh SHA NOTES DIR # -# Replaces the "nightly" pre-release with a new one: its tag at SHA, its -# text from the file NOTES, its files all the files in DIR. +# Updates the "nightly" pre-release: its tag to SHA, its text from the +# file NOTES, and its files to all the files in DIR. If there is no +# release, it makes one. +# +# The script does not delete the release. If a step fails, the old +# release stays (with its old files or some new files), and the next run +# tries again. # # It needs GH_TOKEN and GH_REPO. set -euo pipefail @@ -11,12 +16,45 @@ sha=$1 notes=$2 dir=$3 shopt -s nullglob files=("$dir"/*) [ ${#files[@]} -gt 0 ] || { echo "::error::No files to publish in $dir"; exit 1; } +title="Nightly build ($(date -u +%Y-%m-%d), ${sha:0:7})" -gh release delete nightly --yes --cleanup-tag 2>/dev/null || true -# (a tag that has no release) -gh api -X DELETE "repos/$GH_REPO/git/refs/tags/nightly" 2>/dev/null || true +# release_id: prints the ID of the "nightly" release, or nothing +release_id() { + gh api "repos/$GH_REPO/releases/tags/nightly" --jq .id 2>/dev/null || true +} -gh release create nightly "${files[@]}" \ - --prerelease --latest=false --target "$sha" \ - --title "Nightly build ($(date -u +%Y-%m-%d), ${sha:0:7})" \ - --notes-file "$notes" +# move_tag: puts the "nightly" tag at $sha, and makes the tag if it is +# not there +move_tag() { + if gh api "repos/$GH_REPO/git/ref/tags/nightly" --jq .object.sha >/dev/null 2>&1; then + gh api -X PATCH "repos/$GH_REPO/git/refs/tags/nightly" \ + -f sha="$sha" -F force=true >/dev/null + else + gh api -X POST "repos/$GH_REPO/git/refs" \ + -f ref=refs/tags/nightly -f sha="$sha" >/dev/null + fi +} + +id=$(release_id) +if [ -z "$id" ]; then + # (no release: a new one, its tag made at $sha) + gh release create nightly "${files[@]}" \ + --prerelease --latest=false --target "$sha" \ + --title "$title" --notes-file "$notes" + exit 0 +fi + +move_tag +gh release edit nightly --prerelease --latest=false \ + --title "$title" --notes-file "$notes" +gh release upload nightly "${files[@]}" --clobber + +# the old files that this build did not make +keep=$(for f in "${files[@]}"; do basename "$f"; done) +gh api "repos/$GH_REPO/releases/$id/assets" --paginate --jq '.[] | "\(.id) \(.name)"' | + while read -r aid name; do + if ! grep -qxF "$name" <<<"$keep"; then + echo "Removing the old file $name" + gh api -X DELETE "repos/$GH_REPO/releases/assets/$aid" + fi + done diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 4935980..c1edaff 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -1,15 +1,21 @@ # Nightly: the "nightly" pre-release of the apps, from the artifacts of # the newest complete run of build.yml on main. It does not build the -# apps again. Each run replaces the release and moves its tag. When -# main has no new commits, the run stops and the release does not -# change. A manual run (Actions > Nightly > Run workflow) can publish -# again with "force". +# apps again. It runs each time a run of build.yml on main completes, +# and once a day. Each run updates the release in place and moves its +# tag: a run that fails does not delete the release. When main has no +# new commits, the run stops and the release does not change. A manual +# run (Actions > Nightly > Run workflow) can publish again with "force". # # The files are not signed: Windows SmartScreen and macOS Gatekeeper # show a warning for them. name: Nightly on: + # (each change to main: when its build completes) + workflow_run: + workflows: [Build] + types: [completed] + branches: [main] schedule: - cron: "23 5 * * *" workflow_dispatch: @@ -91,13 +97,21 @@ jobs: fi } > notes.md - # The GITHUB_TOKEN of this workflow can delete the release but gets - # "HTTP 403: Resource not accessible by integration" when it creates - # the release. Thus the step uses the DMODEM_BUMP_TOKEN organization - # secret (Contents: read and write on D-Modem/gui, refer to - # d-modem-bump.yml) if it is available. + # The step uses the DMODEM_BUMP_TOKEN organization secret + # (Contents: read and write on D-Modem/gui, refer to + # d-modem-bump.yml) if it is available. If that token fails (for + # example "HTTP 403: Resource not accessible by personal access + # token"), the next step tries again with the GITHUB_TOKEN of this + # workflow. The script updates the release in place, so a failed + # try does not delete it. - name: Publish - if: steps.pick.outputs.skip == 'false' + id: publish + if: steps.pick.outputs.skip == 'false' && env.HAS_BUMP_TOKEN == 'true' + continue-on-error: true env: - GH_TOKEN: ${{ secrets.DMODEM_BUMP_TOKEN || github.token }} + HAS_BUMP_TOKEN: ${{ secrets.DMODEM_BUMP_TOKEN != '' }} + GH_TOKEN: ${{ secrets.DMODEM_BUMP_TOKEN }} + run: .github/scripts/nightly-publish.sh "${{ steps.pick.outputs.sha }}" notes.md out + - name: Publish (GITHUB_TOKEN) + if: steps.pick.outputs.skip == 'false' && steps.publish.outcome != 'success' run: .github/scripts/nightly-publish.sh "${{ steps.pick.outputs.sha }}" notes.md out