-
-
Notifications
You must be signed in to change notification settings - Fork 0
347 lines (314 loc) · 15.3 KB
/
Copy pathrelease.yml
File metadata and controls
347 lines (314 loc) · 15.3 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
name: Create Release
on:
push:
tags:
- 'v*' # Triggers when a tag starting with 'v' is pushed
jobs:
release:
name: Create GitHub Release
runs-on: ubuntu-latest
permissions:
contents: write # Required to create releases
steps:
- name: Checkout code
uses: actions/checkout@v7
with:
fetch-depth: 0 # Required to get full history and tag message
- name: Get tag name
id: tag
run: echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
- name: Get tag message
id: tag_message
run: |
TAG_NAME=${GITHUB_REF#refs/tags/}
# Get annotated tag message, or use tag name if no message
TAG_MESSAGE=$(git tag -l --format='%(contents)' "$TAG_NAME" 2>/dev/null || echo "")
if [ -z "$TAG_MESSAGE" ]; then
TAG_MESSAGE="Release $TAG_NAME"
fi
# Preserve newlines using heredoc (no escaping needed)
{
echo "MESSAGE<<EOF"
echo "$TAG_MESSAGE"
echo "EOF"
} >> $GITHUB_OUTPUT
- name: Extract version from tag
id: version
run: |
TAG_NAME=${GITHUB_REF#refs/tags/}
# Remove 'v' prefix if exists
VERSION=${TAG_NAME#v}
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
- name: Get previous tag
id: previous_tag
run: |
TAG_NAME=${GITHUB_REF#refs/tags/}
# Get the previous tag (excluding the current one)
PREVIOUS_TAG=$(git tag -l 'v*' | sort -V | grep -B1 "^$TAG_NAME$" | grep -v "^$TAG_NAME$" | tail -1 || echo "")
if [ -z "$PREVIOUS_TAG" ]; then
# If no previous tag found, use the first commit
PREVIOUS_TAG=$(git rev-list --max-parents=0 HEAD 2>/dev/null || echo "")
fi
echo "PREVIOUS_TAG=$PREVIOUS_TAG" >> $GITHUB_OUTPUT
echo "Previous tag: $PREVIOUS_TAG"
- name: Get commits between tags
id: commits
run: |
TAG_NAME=${GITHUB_REF#refs/tags/}
PREVIOUS_TAG="${{ steps.previous_tag.outputs.PREVIOUS_TAG }}"
if [ -n "$PREVIOUS_TAG" ]; then
# Get commits between previous tag and current tag
COMMITS=$(git log --pretty=format:"- %s (%h)" "$PREVIOUS_TAG..$TAG_NAME" 2>/dev/null || echo "")
if [ -n "$COMMITS" ]; then
echo "HAS_COMMITS=true" >> $GITHUB_OUTPUT
{
echo "COMMITS<<EOF"
echo "$COMMITS"
echo "EOF"
} >> $GITHUB_OUTPUT
echo "Found $(echo "$COMMITS" | wc -l) commits between $PREVIOUS_TAG and $TAG_NAME"
else
echo "HAS_COMMITS=false" >> $GITHUB_OUTPUT
echo "No commits found between $PREVIOUS_TAG and $TAG_NAME"
fi
else
echo "HAS_COMMITS=false" >> $GITHUB_OUTPUT
echo "No previous tag found, skipping commits"
fi
- name: Get changelog entry
id: changelog
run: |
VERSION="${{ steps.version.outputs.VERSION }}"
CHANGELOG_FILE="docs/CHANGELOG.md"
if [ -f "$CHANGELOG_FILE" ]; then
# Search changelog section for this version
# Match patterns like: ## [0.0.4] - YYYY-MM-DD
# Use awk to extract the section, stopping at the next version header
CHANGELOG_ENTRY=$(awk -v version="$VERSION" '
BEGIN {
# Escape dots in version for regex matching
gsub(/\./, "\\.", version)
pattern = "^## \\[" version "\\]"
flag = 0
}
/^## \[/ {
if ($0 ~ pattern) {
flag = 1
next
} else if (flag) {
# Stop at next version header
exit
}
}
flag {
print
}
' "$CHANGELOG_FILE" 2>/dev/null || echo "")
# Trim leading and trailing whitespace/newlines
CHANGELOG_ENTRY=$(echo "$CHANGELOG_ENTRY" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "$CHANGELOG_ENTRY" ]; then
# Preserve newlines using heredoc (no escaping needed)
echo "HAS_CHANGELOG=true" >> $GITHUB_OUTPUT
{
echo "ENTRY<<EOF"
echo "$CHANGELOG_ENTRY"
echo "EOF"
} >> $GITHUB_OUTPUT
LINE_COUNT=$(echo "$CHANGELOG_ENTRY" | wc -l)
echo "✅ Found changelog entry for version $VERSION ($LINE_COUNT lines)"
else
echo "HAS_CHANGELOG=false" >> $GITHUB_OUTPUT
echo "⚠️ Warning: No changelog entry found for version $VERSION" >&2
fi
else
echo "HAS_CHANGELOG=false" >> $GITHUB_OUTPUT
echo "⚠️ Warning: CHANGELOG file not found at $CHANGELOG_FILE" >&2
fi
- name: Check if release exists and determine latest status
id: check-existing
uses: actions/github-script@v9
with:
script: |
const tagName = '${{ steps.tag.outputs.TAG_NAME }}';
const isPrerelease = tagName.includes('-alpha') || tagName.includes('-beta') || tagName.includes('-rc');
try {
const { data: releases } = await github.rest.repos.listReleases({
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100
});
const existingRelease = releases.find(r => r.tag_name === tagName);
// Determine if this should be the latest release
// Compare version numbers to find the newest non-prerelease version
let shouldBeLatest = false;
if (!isPrerelease) {
const version = tagName.replace(/^v/, '');
const nonPrereleases = releases.filter(r => !r.prerelease && r.tag_name.startsWith('v'));
if (nonPrereleases.length === 0) {
shouldBeLatest = true;
} else {
// Compare versions using semantic versioning
shouldBeLatest = nonPrereleases.every(r => {
const otherVersion = r.tag_name.replace(/^v/, '');
// Simple version comparison (works for x.y.z format)
const compareVersions = (v1, v2) => {
const parts1 = v1.split('.').map(Number);
const parts2 = v2.split('.').map(Number);
for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
const part1 = parts1[i] || 0;
const part2 = parts2[i] || 0;
if (part1 > part2) return 1;
if (part1 < part2) return -1;
}
return 0;
};
return compareVersions(version, otherVersion) > 0;
});
}
}
if (existingRelease) {
const hasChangelog = (existingRelease.body || '').includes('## Changelog');
core.setOutput('exists', 'true');
core.setOutput('release_id', existingRelease.id.toString());
core.setOutput('has_changelog', hasChangelog ? 'true' : 'false');
core.setOutput('should_be_latest', shouldBeLatest ? 'true' : 'false');
console.log('Release ' + tagName + ' already exists (ID: ' + existingRelease.id + ')');
console.log('Has changelog: ' + hasChangelog);
console.log('Should be latest: ' + shouldBeLatest);
} else {
core.setOutput('exists', 'false');
core.setOutput('should_be_latest', shouldBeLatest ? 'true' : 'false');
console.log('Release ' + tagName + ' does not exist, will create new one');
console.log('Should be latest: ' + shouldBeLatest);
}
} catch (error) {
console.log('Error checking existing releases:', error.message);
core.setOutput('exists', 'false');
core.setOutput('should_be_latest', 'true'); // Default to true on error
}
- name: Update existing release
if: steps.check-existing.outputs.exists == 'true'
uses: actions/github-script@v9
env:
TAG_NAME: ${{ steps.tag.outputs.TAG_NAME }}
TAG_MESSAGE: ${{ steps.tag_message.outputs.MESSAGE }}
CHANGELOG_ENTRY: ${{ steps.changelog.outputs.ENTRY }}
COMMITS: ${{ steps.commits.outputs.COMMITS }}
HAS_CHANGELOG: ${{ steps.changelog.outputs.HAS_CHANGELOG }}
HAS_COMMITS: ${{ steps.commits.outputs.HAS_COMMITS }}
RELEASE_ID: ${{ steps.check-existing.outputs.release_id }}
SHOULD_BE_LATEST: ${{ steps.check-existing.outputs.should_be_latest }}
with:
script: |
const releaseId = parseInt(process.env.RELEASE_ID);
const tagName = process.env.TAG_NAME;
const tagMessage = process.env.TAG_MESSAGE || '';
const changelogEntry = process.env.CHANGELOG_ENTRY || '';
const commits = process.env.COMMITS || '';
const hasChangelog = process.env.HAS_CHANGELOG === 'true';
const hasCommits = process.env.HAS_COMMITS === 'true';
const shouldBeLatest = process.env.SHOULD_BE_LATEST === 'true';
const isPrerelease = tagName.includes('-alpha') || tagName.includes('-beta') || tagName.includes('-rc');
// Build release body - always prioritize changelog from CHANGELOG.md
// Include tag message, changelog, and commits
let releaseBody = tagMessage;
if (hasChangelog && changelogEntry) {
releaseBody += '\n\n## Changelog\n\n' + changelogEntry;
console.log('📝 Changelog entry found (' + changelogEntry.split('\n').length + ' lines), updating release body');
} else {
console.log('⚠️ No changelog entry found for version ' + tagName + ', using tag message only');
}
if (hasCommits && commits) {
releaseBody += '\n\n## Commits\n\n' + commits;
console.log('📝 Commits found (' + commits.split('\n').length + ' commits)');
} else {
console.log('ℹ️ No commits found between tags');
}
try {
await github.rest.repos.updateRelease({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: releaseId,
body: releaseBody,
prerelease: isPrerelease,
make_latest: shouldBeLatest ? 'true' : 'false' // API expects string: 'true' | 'false' | 'legacy'
});
if (hasChangelog && changelogEntry) {
console.log('✅ Updated release ' + tagName + ' with complete changelog from CHANGELOG.md');
} else {
console.log('✅ Updated release ' + tagName + ' (no changelog available)');
}
// Verify latest status
if (shouldBeLatest && !isPrerelease) {
console.log('✅ Release ' + tagName + ' marked as latest (make_latest: true)');
} else if (!shouldBeLatest) {
console.log('ℹ️ Release ' + tagName + ' is not the latest version, not marked as latest');
}
} catch (error) {
console.error('❌ Error updating release ' + tagName + ':', error.message);
throw error;
}
- name: Create Release
if: steps.check-existing.outputs.exists != 'true'
uses: actions/github-script@v9
env:
TAG_NAME: ${{ steps.tag.outputs.TAG_NAME }}
TAG_MESSAGE: ${{ steps.tag_message.outputs.MESSAGE }}
CHANGELOG_ENTRY: ${{ steps.changelog.outputs.ENTRY }}
COMMITS: ${{ steps.commits.outputs.COMMITS }}
HAS_CHANGELOG: ${{ steps.changelog.outputs.HAS_CHANGELOG }}
HAS_COMMITS: ${{ steps.commits.outputs.HAS_COMMITS }}
SHOULD_BE_LATEST: ${{ steps.check-existing.outputs.should_be_latest }}
with:
script: |
const tagName = process.env.TAG_NAME;
const tagMessage = process.env.TAG_MESSAGE || '';
const changelogEntry = process.env.CHANGELOG_ENTRY || '';
const commits = process.env.COMMITS || '';
const hasChangelog = process.env.HAS_CHANGELOG === 'true';
const hasCommits = process.env.HAS_COMMITS === 'true';
const shouldBeLatest = process.env.SHOULD_BE_LATEST === 'true';
const isPrerelease = tagName.includes('-alpha') || tagName.includes('-beta') || tagName.includes('-rc');
// Build release body - always prioritize changelog from CHANGELOG.md
// Include tag message, changelog, and commits
let releaseBody = tagMessage;
if (hasChangelog && changelogEntry) {
releaseBody += '\n\n## Changelog\n\n' + changelogEntry;
console.log('📝 Changelog entry found (' + changelogEntry.split('\n').length + ' lines), will include in release body');
} else {
console.log('⚠️ No changelog entry found for version ' + tagName);
}
if (hasCommits && commits) {
releaseBody += '\n\n## Commits\n\n' + commits;
console.log('📝 Commits found (' + commits.split('\n').length + ' commits), will include in release body');
} else {
console.log('ℹ️ No commits found between tags');
}
try {
const { data: release } = await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: tagName,
name: 'Release ' + tagName,
body: releaseBody,
draft: false,
prerelease: isPrerelease,
make_latest: shouldBeLatest ? 'true' : 'false', // API expects string: 'true' | 'false' | 'legacy'
generate_release_notes: false // Don't generate auto notes, we have our own changelog
});
if (hasChangelog && changelogEntry) {
console.log('✅ Created release ' + tagName + ' with complete changelog from CHANGELOG.md' + (shouldBeLatest && !isPrerelease ? ' (marked as latest)' : ''));
} else {
console.log('✅ Created release ' + tagName + ' (no changelog available)' + (shouldBeLatest && !isPrerelease ? ' (marked as latest)' : ''));
}
if (shouldBeLatest && !isPrerelease) {
console.log('✅ Release ' + tagName + ' marked as latest (make_latest: true)');
} else if (!shouldBeLatest) {
console.log('ℹ️ Release ' + tagName + ' is not the latest version, not marked as latest');
}
} catch (error) {
console.error('❌ Error creating release ' + tagName + ':', error.message);
throw error;
}
# Maintainer: Héctor Franco Aceituno (@HecFranco)
# Organization: nowo-tech (https://github.com/nowo-tech)