-
Notifications
You must be signed in to change notification settings - Fork 0
133 lines (113 loc) · 4.28 KB
/
node-github-releases.yml
File metadata and controls
133 lines (113 loc) · 4.28 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
# .github/workflows/release.yml
name: Auto Release
on:
push:
branches:
- "main"
paths:
- "package.json"
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Generate GitHub App token
id: app-token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ vars.AUTOMATED_RELEASE_APP_ID }}
private-key: ${{ secrets.AUTOMATED_RELEASE_APP_KEY }}
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2
token: ${{ steps.app-token.outputs.token }}
- name: Check if version changed
id: version
run: |
# Get current version
CURRENT_VERSION=$(jq -r '.version' package.json)
# Get previous version
git show HEAD~1:package.json > /tmp/old-package.json 2>/dev/null || echo '{"version":"0.0.0"}' > /tmp/old-package.json
PREVIOUS_VERSION=$(jq -r '.version' /tmp/old-package.json)
echo "current=$CURRENT_VERSION" >> $GITHUB_OUTPUT
echo "previous=$PREVIOUS_VERSION" >> $GITHUB_OUTPUT
if [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then
echo "changed=true" >> $GITHUB_OUTPUT
else
echo "changed=false" >> $GITHUB_OUTPUT
fi
# Check if prerelease (contains hyphen)
if [[ "$CURRENT_VERSION" == *-* ]]; then
echo "prerelease=true" >> $GITHUB_OUTPUT
else
echo "prerelease=false" >> $GITHUB_OUTPUT
fi
- name: Validate version increment
if: steps.version.outputs.changed == 'true'
run: |
CURRENT="${{ steps.version.outputs.current }}"
PREVIOUS="${{ steps.version.outputs.previous }}"
# Function to compare semver versions
version_compare() {
# Strip prerelease suffix for base comparison
local v1_base="${1%%-*}"
local v2_base="${2%%-*}"
local v1_pre="${1#*-}"
local v2_pre="${2#*-}"
# If no prerelease, set to empty
[[ "$v1_base" == "$1" ]] && v1_pre=""
[[ "$v2_base" == "$2" ]] && v2_pre=""
# Compare major.minor.patch
IFS='.' read -ra V1 <<< "$v1_base"
IFS='.' read -ra V2 <<< "$v2_base"
for i in 0 1 2; do
local n1="${V1[$i]:-0}"
local n2="${V2[$i]:-0}"
if (( n1 > n2 )); then
echo "greater"
return
elif (( n1 < n2 )); then
echo "lesser"
return
fi
done
# Base versions equal, compare prerelease
# No prerelease > prerelease (1.0.0 > 1.0.0-beta)
if [[ -z "$v1_pre" && -n "$v2_pre" ]]; then
echo "greater"
elif [[ -n "$v1_pre" && -z "$v2_pre" ]]; then
echo "lesser"
elif [[ "$v1_pre" > "$v2_pre" ]]; then
echo "greater"
elif [[ "$v1_pre" < "$v2_pre" ]]; then
echo "lesser"
else
echo "equal"
fi
}
RESULT=$(version_compare "$CURRENT" "$PREVIOUS")
if [[ "$RESULT" == "lesser" ]]; then
echo "::error::Version downgrade detected: $PREVIOUS → $CURRENT"
echo "Version must be incremented, not decremented."
exit 1
fi
echo "✓ Version increment valid: $PREVIOUS → $CURRENT"
- name: Create Release
if: steps.version.outputs.changed == 'true'
uses: actions/github-script@v7
with:
github-token: ${{ steps.app-token.outputs.token }}
script: |
const version = '${{ steps.version.outputs.current }}';
const isPrerelease = ${{ steps.version.outputs.prerelease }};
const tagName = `v${version}`;
const release = await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: tagName,
name: tagName,
target_commitish: context.sha,
generate_release_notes: true,
prerelease: isPrerelease
});
console.log(`Created ${isPrerelease ? 'prerelease' : 'release'}: ${release.data.html_url}`);