Created
March 20, 2022 12:06
-
-
Save ralvescosta/5fa7cbd981449ea2286c026c60b44d67 to your computer and use it in GitHub Desktop.
How to configure CircleCI and SonarQube for GoLang Application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: 2.1 | |
jobs: | |
lint: | |
working_directory: ~/repo | |
docker: | |
- image: golangci/golangci-lint:v1.45 | |
steps: | |
- checkout | |
- run: golangci-lint run ./... --out-format=checkstyle --print-issued-lines=false --print-linter-name=false --issues-exit-code=0 --enable=revive > golanci-report.xml | |
- persist_to_workspace: | |
root: ~/repo | |
paths: | |
- golanci-report.xml | |
test_and_coverage: | |
working_directory: ~/repo | |
docker: | |
- image: circleci/golang:1.17 | |
steps: | |
- checkout | |
- restore_cache: | |
keys: | |
- go-mod-v4-{{ checksum "go.sum" }} | |
- run: | |
name: Install Dependencies | |
command: go mod download | |
- save_cache: | |
key: go-mod-v4-{{ checksum "go.sum" }} | |
paths: | |
- "/go/pkg/mod" | |
- run: | |
name: Run unit tests | |
command: | | |
mkdir -p /tmp/test-reports | |
gotestsum --junitfile /tmp/test-reports/unit-tests.xml | |
- store_test_results: | |
path: /tmp/test-reports | |
- run: | |
name: Run coverage | |
command: | | |
go test ./... -race -coverprofile=coverage.out -json > report.json | |
- persist_to_workspace: | |
root: ~/repo | |
paths: | |
- coverage.out | |
- report.json | |
sonar: | |
working_directory: ~/repo | |
docker: | |
- image: circleci/golang:1.17 | |
steps: | |
- checkout | |
- attach_workspace: | |
at: ~/repo | |
- sonarcloud/scan: | |
sonar_token_variable_name: SONAR_TOKEN | |
build: | |
working_directory: ~/repo | |
docker: | |
- image: circleci/golang:1.17 | |
steps: | |
- checkout | |
- restore_cache: | |
keys: | |
- go-mod-v4-{{ checksum "go.sum" }} | |
- run: | |
name: Install Dependencies | |
command: go mod download | |
- save_cache: | |
key: go-mod-v4-{{ checksum "go.sum" }} | |
paths: | |
- "/go/pkg/mod" | |
- run: | |
name: Run build | |
command: | | |
mkdir -p /tmp/artifacts/build | |
go build -ldflags "-s -w" -o exec main.go | |
mv exec /tmp/artifacts/build | |
- store_artifacts: | |
path: /tmp/artifacts/build | |
orbs: | |
sonarcloud: sonarsource/sonarcloud@1.0.3 | |
workflows: | |
ci: | |
jobs: | |
- lint | |
- test_and_coverage | |
- sonar: | |
requires: | |
- lint | |
- test_and_coverage | |
- build: | |
requires: | |
- sonar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment