diff options
author | Austin S. Hemmelgarn <austin@netdata.cloud> | 2024-02-27 14:11:43 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-27 21:11:43 +0200 |
commit | da5d8ff202a8bbe7e900181d8e052168d7b1cf89 (patch) | |
tree | b639f390faaa12de6bb109a43bd0966301cebd1d | |
parent | 700d77b5b9939671e77b789d07ed35419f7db369 (diff) |
Add CI checks for Go code. (#17066)
Co-authored-by: Ilya Mashchenko <ilya@netdata.cloud>
-rwxr-xr-x | .github/scripts/get-go-version.py | 39 | ||||
-rw-r--r-- | .github/workflows/go-tests.yml | 124 |
2 files changed, 163 insertions, 0 deletions
diff --git a/.github/scripts/get-go-version.py b/.github/scripts/get-go-version.py new file mode 100755 index 0000000000..105c537c8c --- /dev/null +++ b/.github/scripts/get-go-version.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import json +import os +import pathlib + +from packaging.version import parse + +SCRIPT_PATH = pathlib.Path(__file__).parents[0] +REPO_ROOT = SCRIPT_PATH.parents[1] +GO_SRC = REPO_ROOT / 'src' / 'go' + +GITHUB_OUTPUT = pathlib.Path(os.environ['GITHUB_OUTPUT']) + +version = parse('1.0.0') +modules = [] + +for modfile in GO_SRC.glob('**/go.mod'): + moddata = modfile.read_text() + + for line in moddata.splitlines(): + if line.startswith('go '): + version = max(version, parse(line.split()[1])) + break + + for main in modfile.parent.glob('**/main.go'): + mainpath = main.relative_to(modfile.parent).parent + + if 'examples' in mainpath.parts: + continue + + modules.append({ + 'module': str(modfile.parent), + 'version': str(version), + 'build_target': f'github.com/netdata/netdata/go/{ modfile.parts[-2] }/{ str(mainpath) }/', + }) + +with GITHUB_OUTPUT.open('a') as f: + f.write(f'matrix={ json.dumps({"include": modules}) }\n') diff --git a/.github/workflows/go-tests.yml b/.github/workflows/go-tests.yml new file mode 100644 index 0000000000..531c290ae9 --- /dev/null +++ b/.github/workflows/go-tests.yml @@ -0,0 +1,124 @@ +--- +# Ci code for building release artifacts. +name: Go Tests +on: + push: # Master branch checks only validate the build and generate artifacts for testing. + branches: + - master + pull_request: null # PR checks only validate the build and generate artifacts for testing. +concurrency: # This keeps multiple instances of the job from running concurrently for the same ref and event type. + group: go-test-${{ github.ref }}-${{ github.event_name }} + cancel-in-progress: true +jobs: + file-check: # Check what files changed if we’re being run in a PR or on a push. + name: Check Modified Files + runs-on: ubuntu-latest + outputs: + run: ${{ steps.check-run.outputs.run }} + steps: + - name: Checkout + id: checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + submodules: recursive + - name: Check files + id: check-files + uses: tj-actions/changed-files@v42 + with: + since_last_remote_commit: ${{ github.event_name != 'pull_request' }} + files: | + **/*.cmake + CMakeLists.txt + .github/workflows/go-tests.yml + packaging/cmake/ + src/go/** + files_ignore: | + **/*.md + src/go/**/metadata.yaml + - name: List all changed files in pattern + continue-on-error: true + env: + ALL_CHANGED_FILES: ${{ steps.check-files.outputs.all_changed_files }} + run: | + for file in ${ALL_CHANGED_FILES}; do + echo "$file was changed" + done + - name: Check Run + id: check-run + run: | + if [ "${{ steps.check-files.outputs.any_modified }}" == "true" ] || [ "${{ github.event_name }}" == "workflow_dispatch" ]; then + echo 'run=true' >> "${GITHUB_OUTPUT}" + else + echo 'run=false' >> "${GITHUB_OUTPUT}" + fi + + matrix: + name: Generate Build Matrix + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.get-version.outputs.matrix }} + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Install dependencies + run: sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get install -y python3-packaging + - name: Get Go version and modules + id: get-version + run: .github/scripts/get-go-version.py + + tests: + name: Go toolchain tests + runs-on: ubuntu-latest + needs: + - file-check + - matrix + strategy: + fail-fast: false + matrix: ${{ fromJson(needs.matrix.outputs.matrix) }} + steps: + - name: Skip Check + id: skip + if: needs.file-check.outputs.run != 'true' + run: echo "SKIPPED" + - name: Install Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.version }} + - name: Checkout + if: needs.file-check.outputs.run == 'true' + uses: actions/checkout@v4 + with: + submodules: recursive + - name: Go mod download + if: needs.file-check.outputs.run == 'true' + run: go mod download + working-directory: ${{ matrix.module }} + - name: Compile + if: needs.file-check.outputs.run == 'true' + run: | + CGO_ENABLED=0 go build -o /tmp/go-test-build ${{ matrix.build_target }} + /tmp/go-test-build --help || true + working-directory: ${{ matrix.module }} + - name: Go fmt + if: needs.file-check.outputs.run == 'true' + run: | + go fmt ./... | tee modified-files + [ "$(wc -l modified-files | cut -f 1 -d ' ')" -eq 0 ] || exit 1 + working-directory: ${{ matrix.module }} + - name: Go vet + if: needs.file-check.outputs.run == 'true' + run: go vet ./... + working-directory: ${{ matrix.module }} + - name: Set up gotestfmt + if: needs.file-check.outputs.run == 'true' + uses: GoTestTools/gotestfmt-action@v2 + with: + token: ${{ secrets.GITHUB_TOKEN }} + version: v2.0.0 + - name: Go test + if: needs.file-check.outputs.run == 'true' + run: | + set -euo pipefail + go test -json ./... -race -count=1 2>&1 | gotestfmt -hide all + working-directory: ${{ matrix.module }} |