Repository navigation
86 lines (79 loc) · 3.44 KB
/
Copy pathrelease.yaml
File metadata and controls
86 lines (79 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
name: Release
on:
push:
tags:
- "v*"
permissions:
contents: write
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v7
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v7
with:
go-version-file: src/go.mod
# The workshop ships a single moving v0.3.0 tag, so each tag move
# re-releases onto the same GitHub release. GitHub ties a release to the
# tag *name*, not the commit, so the release object survives a tag move
# with its old assets attached. GoReleaser's `release.mode: replace`
# only clears assets when it recreates the release (e.g. a draft); on a
# reused *published* release it leaves the old assets in place and the
# upload then fails with "already_exists" (HTTP 422). So we delete the
# tag's existing release assets here, right before GoReleaser uploads the
# new ones. This is a controlled re-release (binaries rebuild in minutes),
# not an automatic push, so the brief window with no downloadable asset is
# acceptable; relying on `mode: replace` alone fails every time GitHub
# reuses the release. The release object itself is left
# in place, so there is never a 404 on the release, only a short gap in
# its assets while GoReleaser re-uploads.
- name: Clear existing release assets for this tag
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ github.ref_name }}
run: |
release_id=$(gh api --paginate "repos/$GITHUB_REPOSITORY/releases" \
--jq ".[] | select(.tag_name == \"$TAG\" and .draft == false) | .id" \
| head -n1)
if [ -z "$release_id" ]; then
echo "No published release for $TAG yet; nothing to clear."
exit 0
fi
echo "Clearing assets on release $release_id for $TAG"
gh api --paginate "repos/$GITHUB_REPOSITORY/releases/$release_id/assets" \
--jq '.[].id' \
| while read -r asset_id; do
echo "Deleting asset $asset_id"
gh api -X DELETE "repos/$GITHUB_REPOSITORY/releases/assets/$asset_id"
done
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v7
with:
version: "~> v2"
args: release --clean
workdir: src
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Because v0.3.0 is a moving tag, GitHub allows only one *published*
# release per tag: when this run publishes its release, GitHub demotes the
# tag's previous release to a draft rather than deleting it, so drafts
# accumulate one per release. Delete every draft release for this tag,
# keeping the just-published one. Runs after GoReleaser so the published
# release always exists first (no empty window). Deletes the release
# object only, never the git tag.
- name: Remove stale draft releases for this tag
if: success()
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ github.ref_name }}
run: |
gh api --paginate "repos/$GITHUB_REPOSITORY/releases" \
--jq ".[] | select(.tag_name == \"$TAG\" and .draft == true) | .id" \
| while read -r id; do
echo "Deleting stale draft release $id for $TAG"
gh api -X DELETE "repos/$GITHUB_REPOSITORY/releases/$id" || true
done