-
Notifications
You must be signed in to change notification settings - Fork 0
181 lines (158 loc) · 6.58 KB
/
release.yml
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
name: build and release
on:
push:
tags:
- "v*"
branches:
- main # Add main branch to trigger on commits
jobs:
check_commit_message:
runs-on: ubuntu-latest
if: "!startsWith(github.ref, 'refs/tags/')"
outputs:
should_build: ${{ steps.check_message.outputs.should_build }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 2 # Need to fetch the commit before the last one
- name: Check if commit message contains [ci]
id: check_message
run: |
commit_message=$(git log -1 --pretty=format:"%s")
echo "Commit message: $commit_message"
if [[ "$commit_message" == *"[ci]"* ]]; then
echo "should_build=true" >> $GITHUB_OUTPUT
else
echo "should_build=false" >> $GITHUB_OUTPUT
fi
build_tag:
name: Build and Create Release
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/')
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.23.1"
- name: Build Linux binary
run: |
GOOS=linux GOARCH=amd64 go build -o pubsub
chmod +x pubsub
- name: Upload Release Binary
uses: softprops/action-gh-release@v1
with:
files: pubsub
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
build_ci:
name: Build and Update Latest Release
runs-on: ubuntu-latest
needs: check_commit_message
if: needs.check_commit_message.outputs.should_build == 'true'
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.23.1"
- name: Build Linux binary
run: |
GOOS=linux GOARCH=amd64 go build -o pubsub-latest
chmod +x pubsub-latest
- name: Check if latest release exists
id: check_release
run: |
latest_release=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/releases/tags/latest" | \
jq -r '.id // "null"')
if [[ "$latest_release" == "null" ]]; then
echo "exists=false" >> $GITHUB_OUTPUT
echo "No latest release found. Will create a new one."
else
echo "exists=true" >> $GITHUB_OUTPUT
echo "id=$latest_release" >> $GITHUB_OUTPUT
echo "Found latest release with ID: $latest_release"
# Check if asset exists and get its ID
asset_id=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/releases/$latest_release/assets" | \
jq '.[] | select(.name=="pubsub") | .id // "null"')
if [[ "$asset_id" != "null" ]]; then
echo "asset_exists=true" >> $GITHUB_OUTPUT
echo "asset_id=$asset_id" >> $GITHUB_OUTPUT
echo "Found existing asset with ID: $asset_id"
else
echo "asset_exists=false" >> $GITHUB_OUTPUT
echo "No existing asset found."
fi
# Get upload URL
upload_url=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/releases/$latest_release" | \
jq -r '.upload_url')
# Clean up the upload URL (remove {?name,label} part)
upload_url=${upload_url%\{*}
echo "upload_url=$upload_url" >> $GITHUB_OUTPUT
echo "Upload URL: $upload_url"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create latest release if it doesn't exist
if: steps.check_release.outputs.exists == 'false'
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: latest
release_name: Latest Build
body: |
This is an automatically updated release containing the latest build.
Last updated on ${{ github.event.head_commit.timestamp }}
Commit: ${{ github.event.head_commit.message }}
draft: false
prerelease: true
- name: Update latest release if it exists
if: steps.check_release.outputs.exists == 'true'
run: |
RELEASE_ID="${{ steps.check_release.outputs.id }}"
curl -X PATCH \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/releases/$RELEASE_ID" \
-d "{
\"body\": \"This is an automatically updated release containing the latest build.\\nLast updated: $(date -u +'%Y-%m-%d %H:%M:%S UTC')\\nCommit: ${{ github.event.head_commit.message }}\"
}"
echo "Updated release description"
- name: Delete existing asset if exists
if: steps.check_release.outputs.exists == 'true' && steps.check_release.outputs.asset_exists == 'true'
run: |
curl -X DELETE \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/releases/assets/${{ steps.check_release.outputs.asset_id }}"
echo "Deleted existing asset"
# Sleep briefly to ensure the deletion is processed
sleep 2
- name: Upload asset for new release
if: steps.check_release.outputs.exists == 'false'
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./pubsub-latest
asset_name: pubsub
asset_content_type: application/octet-stream
- name: Upload asset for existing release
if: steps.check_release.outputs.exists == 'true'
run: |
curl -X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Content-Type: application/octet-stream" \
-H "Accept: application/vnd.github.v3+json" \
--data-binary @pubsub-latest \
"${{ steps.check_release.outputs.upload_url }}?name=pubsub"
echo "Uploaded new asset"