-
Notifications
You must be signed in to change notification settings - Fork 8
205 lines (195 loc) · 8.46 KB
/
release.yml
File metadata and controls
205 lines (195 loc) · 8.46 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
name: release
run-name: "${{ github.event.repository.name }} ${{ inputs.tag || 'auto' }}"
on:
workflow_dispatch:
inputs:
tag:
description: 'Tag (e.g., v0.0.1). Empty = use package.json version.'
required: false
default: ''
permissions:
contents: write
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
- name: Fetch tags
run: git fetch --tags --force
- uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'npm'
- name: Resolve release tag
id: resolve
env:
INPUT_TAG: ${{ inputs.tag }}
run: |
node - <<'NODE'
const { execSync } = require('child_process');
const fs = require('fs');
const inputTagRaw = (process.env.INPUT_TAG || '').trim();
const normalizeTag = (tag) => tag.startsWith('v') ? tag.slice(1) : tag;
const isSemver = (version) => /^\d+\.\d+\.\d+$/.test(version);
const parseSemver = (version) => {
if (!isSemver(version)) return null;
const [major, minor, patch] = version.split('.').map(n => Number(n));
return { major, minor, patch };
};
const compareSemver = (a, b) => {
const pa = parseSemver(a);
const pb = parseSemver(b);
if (!pa || !pb) return 0;
if (pa.major !== pb.major) return pa.major - pb.major;
if (pa.minor !== pb.minor) return pa.minor - pb.minor;
return pa.patch - pb.patch;
};
const hasTag = (tag) => {
if (!tag) return false;
try {
execSync(`git show-ref --tags --verify --quiet refs/tags/${tag}`, { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
};
let latestTag = '';
try {
const tagOutput = execSync("git tag --list v* --sort=-v:refname", { encoding: 'utf8' }).trim();
latestTag = tagOutput.split(/\r?\n/).find(Boolean) || '';
} catch (e) {
latestTag = '';
}
const pkg = require('./package.json');
const pkgVersion = pkg.version;
if (!isSemver(pkgVersion)) {
console.error(`package.json version ${pkgVersion} is not a valid semver.`);
process.exit(1);
}
const latestVersion = latestTag ? normalizeTag(latestTag) : '';
const latestSemver = latestVersion ? parseSemver(latestVersion) : null;
let resolvedTag = '';
let expectedVersion = '';
let mode = '';
let baseVersion = '';
let baseSource = '';
let tagExists = false;
if (inputTagRaw) {
if (!/^v?\d+\.\d+\.\d+$/.test(inputTagRaw)) {
console.error('Invalid tag format. Use vX.Y.Z or X.Y.Z.');
process.exit(1);
}
resolvedTag = inputTagRaw.startsWith('v') ? inputTagRaw : `v${inputTagRaw}`;
expectedVersion = normalizeTag(resolvedTag);
mode = 'manual';
tagExists = hasTag(resolvedTag);
baseVersion = tagExists ? expectedVersion : '';
baseSource = tagExists ? 'input_tag' : 'input_tag_new';
} else {
mode = 'auto';
if (latestTag && !latestSemver) {
console.error(`Latest tag ${latestTag} is not a valid semver.`);
process.exit(1);
}
if (latestSemver && compareSemver(pkgVersion, latestVersion) < 0) {
console.error(`package.json version ${pkgVersion} is lower than latest tag ${latestTag}.`);
process.exit(1);
}
resolvedTag = `v${pkgVersion}`;
expectedVersion = pkgVersion;
tagExists = hasTag(resolvedTag);
baseVersion = latestVersion;
baseSource = tagExists ? 'package_tag' : 'package_version';
}
const envLines = [
`RELEASE_TAG=${resolvedTag}`,
`RELEASE_VERSION=${expectedVersion}`,
`RELEASE_MODE=${mode}`,
`LATEST_TAG=${latestTag}`,
`PACKAGE_VERSION=${pkgVersion}`,
`BASE_VERSION=${baseVersion}`,
`BASE_SOURCE=${baseSource}`,
`TAG_EXISTS=${tagExists ? 'true' : 'false'}`
].join('\n') + '\n';
fs.appendFileSync(process.env.GITHUB_ENV, envLines);
const outputLines = [
`release_tag=${resolvedTag}`,
`release_version=${expectedVersion}`,
`release_mode=${mode}`,
`latest_tag=${latestTag}`,
`package_version=${pkgVersion}`,
`base_version=${baseVersion}`,
`base_source=${baseSource}`,
`tag_exists=${tagExists ? 'true' : 'false'}`
].join('\n') + '\n';
fs.appendFileSync(process.env.GITHUB_OUTPUT, outputLines);
const summaryLines = [
'### Release Preview',
`- mode: ${mode}`,
`- input_tag: ${inputTagRaw || '(empty)'}`,
`- latest_tag: ${latestTag || '(none)'}`,
`- package_version: ${pkgVersion}`,
`- base_version: ${baseVersion || '(none)'}`,
`- base_source: ${baseSource || '(none)'}`,
`- resolved_tag: ${resolvedTag}`,
`- tag_exists: ${tagExists ? 'yes' : 'no'}`,
`- expected_version: ${expectedVersion}`
].join('\n');
fs.appendFileSync(process.env.GITHUB_STEP_SUMMARY, summaryLines + '\n');
console.log(`::notice title=Resolved Tag::${resolvedTag}`);
NODE
- name: Checkout target tag
if: ${{ steps.resolve.outputs.tag_exists == 'true' }}
env:
RELEASE_TAG: ${{ steps.resolve.outputs.release_tag }}
run: |
git rev-parse "refs/tags/${RELEASE_TAG}" >/dev/null 2>&1
git checkout "${RELEASE_TAG}"
- name: Verify tag matches package.json version
if: ${{ steps.resolve.outputs.tag_exists == 'true' }}
env:
RELEASE_TAG: ${{ steps.resolve.outputs.release_tag }}
run: |
node -e "const pkg=require('./package.json'); const tag=process.env.RELEASE_TAG; const expected='v'+pkg.version; if(tag!==expected){ console.error('Tag '+tag+' does not match package.json version '+expected); process.exit(1);} console.log('Tag matches '+expected);"
- name: Verify package.json matches release tag
if: ${{ steps.resolve.outputs.tag_exists != 'true' }}
env:
RELEASE_TAG: ${{ steps.resolve.outputs.release_tag }}
run: |
node -e "const pkg=require('./package.json'); const tag=process.env.RELEASE_TAG; const expected='v'+pkg.version; if(tag!==expected){ console.error('Current commit package.json '+expected+' does not match resolved release tag '+tag); process.exit(1);} console.log('Current package matches '+expected);"
- name: Compute release name
env:
RELEASE_TAG: ${{ steps.resolve.outputs.release_tag }}
run: |
node -e "const p=require('./package.json'); const tag=process.env.RELEASE_TAG; const name=p.name.includes('/')? p.name.split('/')[1]: p.name; const value=name+' '+tag; console.log('RELEASE_NAME='+value);" >> "$GITHUB_ENV"
- name: Pack npm artifact
run: |
name=$(node -e "const p=require('./package.json'); const n=p.name.replace('@','').replace('/','-'); process.stdout.write(n+'-'+p.version+'.tgz');")
npm pack
test -f "$name"
echo "PACKAGE_TGZ=$name" >> "$GITHUB_ENV"
- name: Pack standalone tarball
run: |
version=$(node -e "process.stdout.write(require('./package.json').version)")
name="codexmate-${version}-standalone.tar.gz"
npm install --omit=dev --no-fund --no-audit
tar czf "$name" \
cli.js cli/ lib/ plugins/ web-ui.html web-ui/ \
node_modules/ package.json LICENSE README.md README.zh.md
echo "STANDALONE_TGZ=$name" >> "$GITHUB_ENV"
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.resolve.outputs.release_tag }}
target_commitish: ${{ github.sha }}
name: ${{ env.RELEASE_NAME }}
prerelease: false
draft: false
files: |
${{ env.PACKAGE_TGZ }}
${{ env.STANDALONE_TGZ }}
generate_release_notes: true