-
Notifications
You must be signed in to change notification settings - Fork 6
326 lines (279 loc) · 9.36 KB
/
Copy pathtest-and-build.yml
File metadata and controls
326 lines (279 loc) · 9.36 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
name: Build and Release
on:
push:
branches: [ main ]
tags: [ 'v*' ]
paths-ignore:
- '**/README.md'
- 'examples/**'
- 'docs/**'
- 'config/**'
- 'deploy/**'
pull_request:
branches: [ main ]
paths-ignore:
- '**/README.md'
- 'examples/**'
- 'docs/**'
- 'config/**'
- 'deploy/**'
permissions:
contents: write
packages: write
env:
GO_VERSION: '1.25'
GOEXPERIMENT: 'jsonv2'
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
unit-tests:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/main')
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 1
submodules: recursive
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
- name: Cache Go modules
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run unit tests
run: |
# Run all tests except functional and integration tests
go test -v -short $(go list ./... | grep -v '/test/functional' | grep -v '/test/integration')
functional-tests:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/main')
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 1
submodules: recursive
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
- name: Cache Go modules
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-functional-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-functional-
${{ runner.os }}-go-
- name: Run functional tests
run: |
# Run functional tests (these use testcontainers/Docker)
# Docker is available by default on ubuntu-latest runners
cd test && go test -v -timeout 10m ./functional/...
integration-tests:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/main')
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 1
submodules: recursive
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
- name: Cache Go modules
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-integration-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-integration-
${{ runner.os }}-go-
- name: Run integration tests
run: |
# Run integration tests (if any exist)
if [ -n "$(find ./test/integration -name '*_test.go' 2>/dev/null)" ]; then
cd test && go test -v -timeout 15m ./integration/...
else
echo "No integration tests found, skipping..."
fi
auto-tag:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
outputs:
new-tag: ${{ steps.bump.outputs.new-version }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Get latest tag
id: latest-tag
run: |
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
echo "latest-tag=$LATEST_TAG" >> $GITHUB_OUTPUT
- name: Bump version
id: bump
run: |
LATEST_TAG=${{ steps.latest-tag.outputs.latest-tag }}
# Remove 'v' prefix and split version
VERSION=${LATEST_TAG#v}
IFS='.' read -ra VERSION_PARTS <<< "$VERSION"
MAJOR=${VERSION_PARTS[0]:-0}
MINOR=${VERSION_PARTS[1]:-0}
PATCH=${VERSION_PARTS[2]:-0}
# Check commit messages for version bump indicators
if git log --format=%B -n 1 | grep -qi "BREAKING CHANGE\|major:"; then
NEW_VERSION="v$((MAJOR + 1)).0.0"
elif git log --format=%B -n 1 | grep -qi "feat:\|feature:\|minor:"; then
NEW_VERSION="v$MAJOR.$((MINOR + 1)).0"
else
NEW_VERSION="v$MAJOR.$MINOR.$((PATCH + 1))"
fi
echo "new-version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "Bumping from $LATEST_TAG to $NEW_VERSION"
- name: Create and push tag
run: |
NEW_VERSION=${{ steps.bump.outputs.new-version }}
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag -a $NEW_VERSION -m "Auto-release $NEW_VERSION"
git push origin $NEW_VERSION
build:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
needs: [auto-tag, unit-tests, functional-tests, integration-tests]
strategy:
matrix:
include:
- goos: linux
goarch: amd64
- goos: linux
goarch: arm64
- goos: darwin
goarch: amd64
- goos: darwin
goarch: arm64
- goos: windows
goarch: amd64
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 1
submodules: recursive
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
- name: Cache Go modules
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Get version from auto-tag
id: version
run: |
VERSION="${{ needs.auto-tag.outputs.new-tag }}"
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "commit=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
- name: Build for ${{ matrix.goos }}/${{ matrix.goarch }}
run: |
VERSION=${{ steps.version.outputs.version }}
COMMIT=${{ steps.version.outputs.commit }}
GOOS=${{ matrix.goos }}
GOARCH=${{ matrix.goarch }}
# Create dist directory
mkdir -p dist
# Set output name
output_name="agent"
if [ "$GOOS" = "windows" ]; then
output_name="agent.exe"
fi
echo "Building for $GOOS/$GOARCH..."
GOEXPERIMENT=jsonv2 CGO_ENABLED=0 GOOS=$GOOS GOARCH=$GOARCH go build -a -installsuffix cgo \
-ldflags "-X github.com/thand-io/agent/internal/common.Version=$VERSION -X github.com/thand-io/agent/internal/common.GitCommit=$COMMIT" \
-o "dist/$output_name" .
# Create archive
if [ "$GOOS" = "windows" ]; then
cd dist && zip "agent-${GOOS}-${GOARCH}.zip" "$output_name" && cd ..
else
cd dist && tar -czf "agent-${GOOS}-${GOARCH}.tar.gz" "$output_name" && cd ..
fi
# Also keep the binary for direct download
mv "dist/$output_name" "dist/agent-${GOOS}-${GOARCH}$([ "$GOOS" = "windows" ] && echo ".exe" || echo "")"
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: binaries-${{ matrix.goos }}-${{ matrix.goarch }}-${{ steps.version.outputs.version }}
path: dist/
docker-release:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
needs: [auto-tag, build]
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Get version from auto-tag
id: version
run: |
VERSION="${{ needs.auto-tag.outputs.new-tag }}"
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "commit=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
pattern: binaries-*-${{ steps.version.outputs.version }}
path: dist/
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata for release
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=dev
type=raw,value=${{ steps.version.outputs.version }}
- name: Build and push Docker image (dev + version tags)
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile.cicd
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VERSION=${{ steps.version.outputs.version }}
COMMIT=${{ steps.version.outputs.commit }}
cache-from: type=gha
cache-to: type=gha,mode=max