-
Notifications
You must be signed in to change notification settings - Fork 168
321 lines (283 loc) · 9.96 KB
/
main.yml
File metadata and controls
321 lines (283 loc) · 9.96 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
name: Main
on:
push:
branches: [ main ]
tags:
- 'v*'
pull_request:
branches: [ main ]
env:
# Reuse consistent image names across packaging and manifests.
GHCR_IMAGE: ghcr.io/${{ github.repository_owner }}/goflow2
DOCKERHUB_IMAGE: docker.io/${{ github.repository_owner }}/goflow2
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v6
# Full history ensures tag-aware versioning for release builds.
with:
fetch-depth: 0
- name: Set up Go 1.x
uses: actions/setup-go@v6
with:
go-version: ^1.24
cache: true
cache-dependency-path: |
go.sum
go.mod
- name: Tidy Go Modules
run: go mod tidy
- name: Install linters
run: |
go install honnef.co/go/tools/cmd/staticcheck@latest
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- name: Test
run: make test
- name: Vet
run: make vet
- name: Test (race)
continue-on-error: true
run: make test-race
- name: Test (coverage)
continue-on-error: true
run: make test-cover
- name: Lint (golangci-lint)
continue-on-error: true
uses: golangci/golangci-lint-action@v9
with:
version: latest
- name: Staticcheck
continue-on-error: true
run: make staticcheck
matrix:
name: Select Matrix
runs-on: ubuntu-latest
outputs:
include: ${{ steps.set-matrix.outputs.include }}
steps:
- name: Set matrix for event
id: set-matrix
shell: bash
run: |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
echo 'include=[{"goos":"linux","goarch":"amd64","package":true}]' >> "$GITHUB_OUTPUT"
else
echo 'include=[{"goos":"linux","goarch":"amd64","package":true},{"goos":"linux","goarch":"arm64","package":true},{"goos":"linux","goarch":"arm","goarm":"7","package":true},{"goos":"darwin","goarch":"amd64","package":false},{"goos":"darwin","goarch":"arm64","package":false},{"goos":"windows","goarch":"amd64","extension":".exe","package":false},{"goos":"windows","goarch":"arm64","extension":".exe","package":false}]' >> "$GITHUB_OUTPUT"
fi
build:
name: Build (${{ matrix.goos }}-${{ matrix.goarch }})
needs:
- test
- matrix
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include: ${{ fromJSON(needs.matrix.outputs.include) }}
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v6
# Fetch tags so release builds can resolve VERSION.
with:
fetch-depth: 0
- name: Set up Go 1.x
uses: actions/setup-go@v6
with:
go-version: ^1.24
cache: true
cache-dependency-path: |
go.sum
go.mod
- name: Set VERSION for tag builds
if: startsWith(github.ref, 'refs/tags/')
run: echo "VERSION=$(git describe --tags --abbrev=0 HEAD)" >> $GITHUB_ENV
- name: Build
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
GOARM: ${{ matrix.goarm }}
EXTENSION: ${{ matrix.extension }}
run: |
make build
echo "BINARY_PATH=$(make print-output)" >> "$GITHUB_ENV"
- name: Set docker packaging vars
if: matrix.package
shell: bash
# Package Docker images only for supported linux arch targets.
run: |
if [[ "${{ matrix.goarch }}" == "amd64" ]]; then
echo "DOCKER_PLATFORM=linux/amd64" >> "$GITHUB_ENV"
echo "DOCKER_PACKAGE=true" >> "$GITHUB_ENV"
elif [[ "${{ matrix.goarch }}" == "arm64" ]]; then
echo "DOCKER_PLATFORM=linux/arm64/v8" >> "$GITHUB_ENV"
echo "DOCKER_PACKAGE=true" >> "$GITHUB_ENV"
else
echo "Unsupported GOARCH for docker packaging: ${{ matrix.goarch }}" >&2
echo "DOCKER_PACKAGE=false" >> "$GITHUB_ENV"
exit 0
fi
echo "PREBUILT_BINARY=${BINARY_PATH}" >> "$GITHUB_ENV"
- name: Set up QEMU
if: matrix.package && env.DOCKER_PACKAGE == 'true'
uses: docker/setup-qemu-action@v4
- name: Set up Docker Buildx
if: matrix.package && env.DOCKER_PACKAGE == 'true'
uses: docker/setup-buildx-action@v4
- name: Login to DockerHub
if: matrix.package && env.DOCKER_PACKAGE == 'true' && github.event_name != 'pull_request'
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Log in to GHCR
if: matrix.package && env.DOCKER_PACKAGE == 'true' && github.event_name != 'pull_request'
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin
- name: Docker package
if: matrix.package && env.DOCKER_PACKAGE == 'true'
# PRs load the image locally; main pushes to registries.
run: |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
DOCKER_PUSH_FLAG="--load"
else
DOCKER_PUSH_FLAG="--push"
fi
DOCKER_SUFFIX=-${{ matrix.goarch }} \
DOCKER_IMAGE_PREFIXES="${{ env.DOCKERHUB_IMAGE }} ${{ env.GHCR_IMAGE }}" \
DOCKER_CMD="buildx build $DOCKER_PUSH_FLAG --platform $DOCKER_PLATFORM --build-arg BIN_SOURCE=prebuilt --build-arg PREBUILT_BINARY=$PREBUILT_BINARY" \
make docker
- name: Cache user gems
if: matrix.package
uses: actions/cache@v5
with:
path: |
~/.gem
~/.local/share/gem
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-gems-
# - name: Cache apt lists and archives
# if: matrix.package
# id: cache-apt
# uses: actions/cache@v5
# with:
# path: |
# /var/cache/apt/archives
# /var/lib/apt/lists
# key: ${{ runner.os }}-apt-${{ hashFiles('.github/workflows/main.yml') }}
# restore-keys: |
# ${{ runner.os }}-apt-
- name: Install fpm
if: matrix.package
# fpm builds both rpm and deb artifacts from the Go binaries.
run: |
# if [[ "${{ steps.cache-apt.outputs.cache-hit }}" != "true" ]]; then
# sudo apt-get update
# fi
sudo apt-get update
sudo apt-get install -y rpm ruby ruby-dev
gem install --user-install fpm
echo "$(ruby -e 'print Gem.user_dir')/bin" >> $GITHUB_PATH
- name: Package
if: matrix.package
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
GOARM: ${{ matrix.goarm }}
run: make package-deb package-rpm
- name: Upload Artifact
uses: actions/upload-artifact@v6
with:
name: dist-${{ matrix.goos }}-${{ matrix.goarch }}
path: dist/*
retention-days: 14
manifest:
name: Docker Manifests
runs-on: ubuntu-latest
needs: build
if: github.event_name != 'pull_request'
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Login to DockerHub
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Log in to GHCR
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin
- name: Set VERSION for tag builds
if: startsWith(github.ref, 'refs/tags/')
run: echo "VERSION=$(git describe --tags --abbrev=0 HEAD)" >> $GITHUB_ENV
- name: Publish manifests (branch)
if: "!startsWith(github.ref, 'refs/tags/')"
shell: bash
# Publish branch-specific and latest tags to both registries.
run: |
images=("${{ env.DOCKERHUB_IMAGE }}" "${{ env.GHCR_IMAGE }}")
for image in "${images[@]}"; do
DOCKER_IMAGE="$image" make docker-manifest
DOCKER_IMAGE="$image" DOCKER_MANIFEST_TAG=latest make docker-manifest
done
- name: Publish manifests (tag)
if: startsWith(github.ref, 'refs/tags/')
shell: bash
# Tag releases get a versioned manifest plus latest.
run: |
images=("${{ env.DOCKERHUB_IMAGE }}" "${{ env.GHCR_IMAGE }}")
for image in "${images[@]}"; do
DOCKER_IMAGE="$image" DOCKER_MANIFEST_TAG="${VERSION}" make docker-manifest
DOCKER_IMAGE="$image" DOCKER_MANIFEST_TAG=latest make docker-manifest
done
release:
name: Release
needs: build
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Download Artifacts
uses: actions/download-artifact@v8
with:
path: dist
pattern: dist-*
merge-multiple: true
- name: Create Release
id: create_release
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const tag = process.env.GITHUB_REF_NAME;
const { data } = await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: tag,
name: `Release ${tag}`,
draft: false,
prerelease: false,
});
core.setOutput('upload_url', data.upload_url);
- name: Upload Release Asset
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs').promises;
const path = require('path');
const uploadUrl = '${{ steps.create_release.outputs.upload_url }}';
for (const file of await fs.readdir('./dist')) {
console.log('uploading', file);
await github.rest.repos.uploadReleaseAsset({
url: uploadUrl,
name: file,
data: await fs.readFile(path.join('dist', file)),
});
}