Skip to content

Commit

Permalink
.github: separate release creation from uploading assets (#792)
Browse files Browse the repository at this point in the history
Before this commit, the publish-release script handled both:

- creating the draft release

- uploading assets to the release

with the intention that the first build job to finish would create the
release with one asset, and each later job would upload an asset.

The logic was like:

    if ! gh release view "${build_tag}"; then
      gh release create [...]
    else
      gh release upload [...]
    fi

But this had a race. For example, if two build jobs finished at nearly
the same moment, they could each create a draft release. This race was
almost never encountered in practice, and was easily resolved by
deleting a draft release and restarting a build job, but it's worth
resolving.

Add an initial build job to create a draft release that has no assets,
and make the later build jobs only upload to that release.

This fixes the race, and makes it easier to share logic between the
native build jobs and the cross-compiling build jobs.

Fixes: #551
Refs: #789
  • Loading branch information
ee7 authored Aug 16, 2023
1 parent 0e8d665 commit 88f367f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/bin/create-artifact
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ case "${OS}" in
;;
esac

echo "ARTIFACT_FILE=${artifact_file}" >> "${GITHUB_ENV}"
gh release upload "${build_tag}" "${artifact_file}"
39 changes: 17 additions & 22 deletions .github/bin/publish-release
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,22 @@

build_tag="${GITHUB_REF_NAME}"

if ! gh release view "${build_tag}"; then
# Generate a list of commits for the release notes, without assuming that the
# tag for the previous official configlet release:
# - is the most recent (or second most recent) local tag.
# - exists locally (so we support creating test releases on a forked repo).
# The `gh release create` command does have a `--generate-notes` option,
# but the below gives full control over the format, and includes commit refs.
previous_release_sha="$(gh api graphql --jq 'recurse | strings' -f query='
{
repository(owner: "exercism", name: "configlet") {
latestRelease {
tagCommit {
oid
}
# Generate a list of commits for the release notes, without assuming that the
# tag for the previous official configlet release:
# - is the most recent (or second most recent) local tag.
# - exists locally (so we support creating test releases on a forked repo).
# The `gh release create` command does have a `--generate-notes` option,
# but the below gives full control over the format, and includes commit refs.
previous_release_sha="$(gh api graphql --jq 'recurse | strings' -f query='
{
repository(owner: "exercism", name: "configlet") {
latestRelease {
tagCommit {
oid
}
}
}')"
commits="$(git log --format='- %h %s' "${previous_release_sha}..${build_tag}")"
body="$(printf '### Changes\n\n%s' "${commits}")"
gh release create --draft --title "${build_tag}" --notes "${body}" \
"${build_tag}" "${ARTIFACT_FILE}"
else
gh release upload "${build_tag}" "${ARTIFACT_FILE}"
fi
}
}')"
commits="$(git log --format='- %h %s' "${previous_release_sha}..${build_tag}")"
body="$(printf '### Changes\n\n%s' "${commits}")"
gh release create --draft --title "${build_tag}" --notes "${body}" "${build_tag}"
24 changes: 17 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,24 @@ on:
workflow_dispatch:

jobs:
create-empty-release:
runs-on: ubuntu-22.04
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
with:
fetch-depth: 0 # Allows using `git log` to set initial release notes.

- name: Create empty release
shell: bash
run: ./.github/bin/publish-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

build:
needs: [create-empty-release]
strategy:
fail-fast: false
matrix:
Expand All @@ -31,8 +48,6 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
with:
fetch-depth: 0 # Allows using `git log` to set initial release notes.

- name: On Linux, install musl
if: matrix.os == 'linux'
Expand All @@ -55,11 +70,6 @@ jobs:
env:
OS: ${{ matrix.os }}
ARCH: ${{ matrix.arch }}

- name: Publish release
shell: bash
run: ./.github/bin/publish-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

cross-compile:
Expand Down

0 comments on commit 88f367f

Please sign in to comment.