Add CI checks for Go code. (#17066)
Co-authored-by: Ilya Mashchenko <ilya@netdata.cloud>
Austin S. Hemmelgarn committed
Feb 27, 2024 at 14:11 UTC
da5d8ff202a8bbe7e900181d8e052168d7b1cf89
2 files changed
+163
.github/scripts/get-go-version.py
new
+39
@@ -0,0 +1,39 @@
1
+#!/usr/bin/env python3
2
+
3
+import json
4
+import os
5
+import pathlib
6
+
7
+from packaging.version import parse
8
+
9
+SCRIPT_PATH = pathlib.Path(__file__).parents[0]
10
+REPO_ROOT = SCRIPT_PATH.parents[1]
11
+GO_SRC = REPO_ROOT / 'src' / 'go'
12
+
13
+GITHUB_OUTPUT = pathlib.Path(os.environ['GITHUB_OUTPUT'])
14
+
15
+version = parse('1.0.0')
16
+modules = []
17
+
18
+for modfile in GO_SRC.glob('**/go.mod'):
19
+ moddata = modfile.read_text()
20
+
21
+ for line in moddata.splitlines():
22
+ if line.startswith('go '):
23
+ version = max(version, parse(line.split()[1]))
24
+ break
25
+
26
+ for main in modfile.parent.glob('**/main.go'):
27
+ mainpath = main.relative_to(modfile.parent).parent
28
+
29
+ if 'examples' in mainpath.parts:
30
+ continue
31
+
32
+ modules.append({
33
+ 'module': str(modfile.parent),
34
+ 'version': str(version),
35
+ 'build_target': f'github.com/netdata/netdata/go/{ modfile.parts[-2] }/{ str(mainpath) }/',
36
+ })
37
+
38
+with GITHUB_OUTPUT.open('a') as f:
39
+ f.write(f'matrix={ json.dumps({"include": modules}) }\n')
.github/workflows/go-tests.yml
new
+124
@@ -0,0 +1,124 @@
1
+---
2
+# Ci code for building release artifacts.
3
+name: Go Tests
4
+on:
5
+ push: # Master branch checks only validate the build and generate artifacts for testing.
6
+ branches:
7
+ - master
8
+ pull_request: null # PR checks only validate the build and generate artifacts for testing.
9
+concurrency: # This keeps multiple instances of the job from running concurrently for the same ref and event type.
10
+ group: go-test-${{ github.ref }}-${{ github.event_name }}
11
+ cancel-in-progress: true
12
+jobs:
13
+ file-check: # Check what files changed if we’re being run in a PR or on a push.
14
+ name: Check Modified Files
15
+ runs-on: ubuntu-latest
16
+ outputs:
17
+ run: ${{ steps.check-run.outputs.run }}
18
+ steps:
19
+ - name: Checkout
20
+ id: checkout
21
+ uses: actions/checkout@v4
22
+ with:
23
+ fetch-depth: 0
24
+ submodules: recursive
25
+ - name: Check files
26
+ id: check-files
27
+ uses: tj-actions/changed-files@v42
28
+ with:
29
+ since_last_remote_commit: ${{ github.event_name != 'pull_request' }}
30
+ files: |
31
+ **/*.cmake
32
+ CMakeLists.txt
33
+ .github/workflows/go-tests.yml
34
+ packaging/cmake/
35
+ src/go/**
36
+ files_ignore: |
37
+ **/*.md
38
+ src/go/**/metadata.yaml
39
+ - name: List all changed files in pattern
40
+ continue-on-error: true
41
+ env:
42
+ ALL_CHANGED_FILES: ${{ steps.check-files.outputs.all_changed_files }}
43
+ run: |
44
+ for file in ${ALL_CHANGED_FILES}; do
45
+ echo "$file was changed"
46
+ done
47
+ - name: Check Run
48
+ id: check-run
49
+ run: |
50
+ if [ "${{ steps.check-files.outputs.any_modified }}" == "true" ] || [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
51
+ echo 'run=true' >> "${GITHUB_OUTPUT}"
52
+ else
53
+ echo 'run=false' >> "${GITHUB_OUTPUT}"
54
+ fi
55
+
56
+ matrix:
57
+ name: Generate Build Matrix
58
+ runs-on: ubuntu-latest
59
+ outputs:
60
+ matrix: ${{ steps.get-version.outputs.matrix }}
61
+ steps:
62
+ - name: Checkout
63
+ uses: actions/checkout@v4
64
+ - name: Install dependencies
65
+ run: sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get install -y python3-packaging
66
+ - name: Get Go version and modules
67
+ id: get-version
68
+ run: .github/scripts/get-go-version.py
69
+
70
+ tests:
71
+ name: Go toolchain tests
72
+ runs-on: ubuntu-latest
73
+ needs:
74
+ - file-check
75
+ - matrix
76
+ strategy:
77
+ fail-fast: false
78
+ matrix: ${{ fromJson(needs.matrix.outputs.matrix) }}
79
+ steps:
80
+ - name: Skip Check
81
+ id: skip
82
+ if: needs.file-check.outputs.run != 'true'
83
+ run: echo "SKIPPED"
84
+ - name: Install Go
85
+ uses: actions/setup-go@v5
86
+ with:
87
+ go-version: ${{ matrix.version }}
88
+ - name: Checkout
89
+ if: needs.file-check.outputs.run == 'true'
90
+ uses: actions/checkout@v4
91
+ with:
92
+ submodules: recursive
93
+ - name: Go mod download
94
+ if: needs.file-check.outputs.run == 'true'
95
+ run: go mod download
96
+ working-directory: ${{ matrix.module }}
97
+ - name: Compile
98
+ if: needs.file-check.outputs.run == 'true'
99
+ run: |
100
+ CGO_ENABLED=0 go build -o /tmp/go-test-build ${{ matrix.build_target }}
101
+ /tmp/go-test-build --help || true
102
+ working-directory: ${{ matrix.module }}
103
+ - name: Go fmt
104
+ if: needs.file-check.outputs.run == 'true'
105
+ run: |
106
+ go fmt ./... | tee modified-files
107
+ [ "$(wc -l modified-files | cut -f 1 -d ' ')" -eq 0 ] || exit 1
108
+ working-directory: ${{ matrix.module }}
109
+ - name: Go vet
110
+ if: needs.file-check.outputs.run == 'true'
111
+ run: go vet ./...
112
+ working-directory: ${{ matrix.module }}
113
+ - name: Set up gotestfmt
114
+ if: needs.file-check.outputs.run == 'true'
115
+ uses: GoTestTools/gotestfmt-action@v2
116
+ with:
117
+ token: ${{ secrets.GITHUB_TOKEN }}
118
+ version: v2.0.0
119
+ - name: Go test
120
+ if: needs.file-check.outputs.run == 'true'
121
+ run: |
122
+ set -euo pipefail
123
+ go test -json ./... -race -count=1 2>&1 | gotestfmt -hide all
124
+ working-directory: ${{ matrix.module }}