-
Notifications
You must be signed in to change notification settings - Fork 0
155 lines (135 loc) · 5.22 KB
/
Copy pathrelease.yml
File metadata and controls
155 lines (135 loc) · 5.22 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
name: Release
on:
push:
branches: [main]
concurrency:
group: release
cancel-in-progress: false
jobs:
check-commit:
runs-on: ubuntu-latest
outputs:
bump: ${{ steps.parse.outputs.bump }}
description: ${{ steps.parse.outputs.description }}
steps:
- name: Parse commit message
id: parse
env:
COMMIT_MSG: ${{ github.event.head_commit.message }}
run: |
if echo "$COMMIT_MSG" | grep -qE '^(feat|fix)(\(.*\))?!:'; then
echo "bump=major" >> "$GITHUB_OUTPUT"
elif echo "$COMMIT_MSG" | grep -qE '^feat(\(.*\))?:'; then
echo "bump=minor" >> "$GITHUB_OUTPUT"
elif echo "$COMMIT_MSG" | grep -qE '^fix(\(.*\))?:'; then
echo "bump=patch" >> "$GITHUB_OUTPUT"
else
echo "bump=none" >> "$GITHUB_OUTPUT"
fi
DESCRIPTION=$(echo "$COMMIT_MSG" | sed -E 's/^(feat|fix)(\(.*\))?!?:[[:space:]]*//')
echo "description<<EOF" >> "$GITHUB_OUTPUT"
echo "$DESCRIPTION" >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
release:
needs: check-commit
if: needs.check-commit.outputs.bump != 'none'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.RELEASE_TOKEN }}
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Run tests
run: go test ./... -v -count=1
- name: Compute new version
id: version
run: |
CURRENT=$(grep 'const VERSION =' internal/version.go | sed -E 's/.*"([^"]+)".*/\1/')
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT"
case "${{ needs.check-commit.outputs.bump }}" in
major) MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0 ;;
minor) MINOR=$((MINOR + 1)); PATCH=0 ;;
patch) PATCH=$((PATCH + 1)) ;;
esac
NEW="$MAJOR.$MINOR.$PATCH"
echo "version=$NEW" >> "$GITHUB_OUTPUT"
echo "tag=V$NEW" >> "$GITHUB_OUTPUT"
echo "previous_tag=V$CURRENT" >> "$GITHUB_OUTPUT"
- name: Update internal/version.go
run: |
sed -i 's/const VERSION = "[^"]*"/const VERSION = "${{ steps.version.outputs.version }}"/' internal/version.go
- name: Update CHANGE_LOGS.md
env:
NEW_VERSION: ${{ steps.version.outputs.version }}
DESCRIPTION: ${{ needs.check-commit.outputs.description }}
run: |
python3 << 'PYEOF'
import os
version = os.environ['NEW_VERSION']
description = os.environ['DESCRIPTION']
with open('CHANGE_LOGS.md') as f:
content = f.read()
lines = content.split('\n')
new_entry = [f'### Version {version}', f'> {description}', '']
new_lines = lines[:2] + new_entry + lines[2:]
with open('CHANGE_LOGS.md', 'w') as f:
f.write('\n'.join(new_lines))
PYEOF
- name: Commit and tag
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add internal/version.go CHANGE_LOGS.md
git commit -m "chore: release ${{ steps.version.outputs.tag }}"
git tag ${{ steps.version.outputs.tag }}
git push
git push origin ${{ steps.version.outputs.tag }}
- name: Build binaries
run: |
mkdir -p bin
VERSION="${{ steps.version.outputs.version }}"
GOOS=linux go build -o bin/g-linux-v$VERSION
GOOS=darwin go build -o bin/g-macos-v$VERSION
GOOS=windows go build -o bin/g-win-v$VERSION.exe
- name: Generate release notes
env:
NEW_TAG: ${{ steps.version.outputs.tag }}
PREV_TAG: ${{ steps.version.outputs.previous_tag }}
DESCRIPTION: ${{ needs.check-commit.outputs.description }}
run: |
python3 << 'PYEOF'
import os, re
description = os.environ['DESCRIPTION']
new_tag = os.environ['NEW_TAG']
prev_tag = os.environ['PREV_TAG']
with open('README.md') as f:
readme = f.read()
match = re.search(r'(^## Overview.*?)(?=^## Examples)', readme, re.MULTILINE | re.DOTALL)
readme_section = match.group(1).rstrip() if match else ''
notes = (
f"## What's Changed\n"
f"- {description}\n\n"
f"**Full Changelog**: https://github.com/cyrus2281/gitBranchTool/compare/{prev_tag}...{new_tag}\n\n"
f"---\n\n"
f"{readme_section}\n"
)
with open('release_notes.md', 'w') as f:
f.write(notes)
PYEOF
- name: Create GitHub release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.version.outputs.tag }}
name: ${{ steps.version.outputs.tag }}
body_path: release_notes.md
files: |
bin/g-linux-v${{ steps.version.outputs.version }}
bin/g-macos-v${{ steps.version.outputs.version }}
bin/g-win-v${{ steps.version.outputs.version }}.exe