-
Notifications
You must be signed in to change notification settings - Fork 20
141 lines (125 loc) · 4.48 KB
/
Copy pathrelease.yml
File metadata and controls
141 lines (125 loc) · 4.48 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
name: Build, tag, and release
on:
pull_request:
types: [closed]
branches: ["main"]
permissions:
contents: write
pull-requests: read
jobs:
build-and-release:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache: true
- name: Verify build
run: |
go vet ./...
go build -v ./cmd/golearn
- name: Determine next version from labels
id: bump
run: |
labels='${{ toJson(github.event.pull_request.labels) }}'
lower=$(echo "$labels" | tr '[:upper:]' '[:lower:]')
bump="patch"
if echo "$lower" | grep -q '"name"\s*:\s*"major"'; then
bump="major"
elif echo "$lower" | grep -q '"name"\s*:\s*"minor"'; then
bump="minor"
fi
echo "bump=$bump" >> $GITHUB_OUTPUT
- name: Get latest tag
id: get_tag
run: |
latest=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
echo "latest=$latest" >> $GITHUB_OUTPUT
- name: Compute next tag
id: next
run: |
latest="${{ steps.get_tag.outputs.latest }}"
echo "Latest tag: $latest"
ver=${latest#v}
IFS='.' read -r major minor patch <<EOF
$ver
EOF
case "${{ steps.bump.outputs.bump }}" in
major)
major=$((major+1)); minor=0; patch=0;
;;
minor)
minor=$((minor+1)); patch=0;
;;
patch)
patch=$((patch+1));
;;
esac
next="v${major}.${minor}.${patch}"
echo "next=$next" >> $GITHUB_OUTPUT
- name: Create tag and release
id: create_release
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }}
run: |
next="${{ steps.next.outputs.next }}"
echo "Creating release: $next"
# Check if tag already exists
if git rev-parse "$next" >/dev/null 2>&1; then
echo "Tag $next already exists, skipping tag creation"
else
# Create and push tag
git tag "$next"
git push origin "$next"
fi
# Create release with proper error handling
if gh release view "$next" >/dev/null 2>&1; then
echo "Release $next already exists, will update it"
elif gh release create "$next" --generate-notes; then
echo "Release $next created successfully"
else
echo "Failed to create release $next"
exit 1
fi
- name: Attach binaries (Linux/macOS)
if: steps.create_release.outcome == 'success'
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }}
run: |
next="${{ steps.next.outputs.next }}"
echo "Uploading binaries for release: $next"
# Build static binaries for common platforms
mkdir -p dist
echo "Building binaries..."
GOOS=linux GOARCH=amd64 go build -o dist/golearn-linux-amd64 ./cmd/golearn
GOOS=darwin GOARCH=arm64 go build -o dist/golearn-darwin-arm64 ./cmd/golearn
GOOS=darwin GOARCH=amd64 go build -o dist/golearn-darwin-amd64 ./cmd/golearn
GOOS=windows GOARCH=amd64 go build -o dist/golearn-windows-amd64.exe ./cmd/golearn
echo "Binaries built successfully:"
ls -lh dist/
# Upload binaries to the release with retry logic
max_attempts=3
attempt=1
while [ $attempt -le $max_attempts ]; do
echo "Attempt $attempt of $max_attempts to upload binaries..."
if gh release upload "$next" dist/* --clobber; then
echo "Binaries uploaded successfully"
exit 0
else
echo "Upload attempt $attempt failed"
if [ $attempt -eq $max_attempts ]; then
echo "Failed to upload binaries after $max_attempts attempts"
exit 1
fi
attempt=$((attempt+1))
sleep 2
fi
done