Skip to content

Commit 1c7d6a2

Browse files
committed
Add golang check targets
This change adds check targets for Golang to the make file. These are also added as stages to the to the Jenkinsfile definition and the GitLab CI is modified to use them too. Signed-off-by: Evan Lezar <[email protected]>
1 parent 635aeb8 commit 1c7d6a2

File tree

3 files changed

+124
-40
lines changed

3 files changed

+124
-40
lines changed

.gitlab-ci.yml

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
# Build packages for all supported OS / ARCH combinations
216

317
stages:
@@ -37,42 +51,40 @@ lint:
3751
<<: *tests-setup
3852
stage: tests
3953
script:
40-
- go get -u golang.org/x/lint/golint
41-
- golint -set_exit_status ${PROJECT_GOPATH}/pkg
54+
- GO111MODULE=off go get -u golang.org/x/lint/golint
55+
- make lint
4256

4357
vet:
4458
<<: *tests-setup
4559
stage: tests
4660
script:
47-
- go vet ${PROJECT_GOPATH}/pkg
61+
- make vet
4862

4963
unit_test:
5064
<<: *tests-setup
5165
stage: tests
5266
script:
53-
- go test ${PROJECT_GOPATH}/pkg
67+
- make test
5468

5569
fmt:
5670
<<: *tests-setup
5771
stage: tests
5872
script:
59-
- res=$(gofmt -l pkg/*.go)
60-
- echo "$res"
61-
- test -z "$res"
73+
- make assert-fmt
6274

6375
ineffassign:
6476
<<: *tests-setup
6577
stage: tests
6678
script:
67-
- go get -u github.com/gordonklaus/ineffassign
68-
- ineffassign pkg/*.go
79+
- GO111MODULE=off go get -u github.com/gordonklaus/ineffassign
80+
- make ineffassign
6981

7082
misspell:
7183
<<: *tests-setup
7284
stage: tests
7385
script:
74-
- go get -u github.com/client9/misspell/cmd/misspell
75-
- misspell pkg/*.go
86+
- GO111MODULE=off go get -u github.com/client9/misspell/cmd/misspell
87+
- make misspell
7688

7789
# build-one jobs build packages for a single OS / ARCH combination.
7890
#

Jenkinsfile

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,53 @@
1414
# limitations under the License.
1515
*/
1616

17-
def getBuildClosure(def architecture, def makeCommand, def makeTarget) {
18-
return {
19-
container('docker') {
20-
stage(architecture) {
21-
sh "${makeCommand} ${makeTarget}"
17+
podTemplate (cloud:'sw-gpu-cloudnative',
18+
containers: [
19+
containerTemplate(name: 'docker', image: 'docker:dind', ttyEnabled: true, privileged: true),
20+
containerTemplate(name: 'golang', image: 'golang:1.14.2', ttyEnabled: true)
21+
]) {
22+
node(POD_LABEL) {
23+
stage('checkout') {
24+
checkout scm
25+
}
26+
stage('dependencies') {
27+
container('golang') {
28+
sh 'GO111MODULE=off go get -u github.com/client9/misspell/cmd/misspell'
29+
sh 'GO111MODULE=off go get -u github.com/gordonklaus/ineffassign'
30+
sh 'GO111MODULE=off go get -u golang.org/x/lint/golint'
2231
}
32+
container('docker') {
33+
sh 'apk add --no-cache make bash'
34+
}
35+
}
36+
stage('check') {
37+
parallel (
38+
getGolangStages(["assert-fmt", "lint", "vet", "ineffassign", "misspell"])
39+
)
40+
}
41+
stage('test') {
42+
parallel (
43+
getGolangStages(["test"])
44+
)
45+
}
46+
stage('build-one') {
47+
parallel (
48+
getSingleBuildForArchitectures(["amd64", "ppc64le", "arm64"])
49+
)
50+
}
51+
stage('build-all') {
52+
parallel (
53+
getAllBuildForArchitectures(["amd64", "ppc64le", "arm64", "x86_64", "aarch64"])
54+
)
2355
}
2456
}
2557
}
2658

27-
def getBuildStagesForArchitectures(def architectures, def makeCommand, def makeTargetPrefix) {
59+
def getGolangStages(def targets) {
2860
stages = [:]
2961

30-
for (a in architectures) {
31-
stages[a] = getBuildClosure(a, makeCommand, "${makeTargetPrefix}-${a}")
62+
for (t in targets) {
63+
stages[t] = getLintClosure(t)
3264
}
3365

3466
return stages
@@ -43,28 +75,32 @@ def getAllBuildForArchitectures(def architectures) {
4375
return getBuildStagesForArchitectures(architectures, "echo make", "docker")
4476
}
4577

46-
podTemplate (cloud:'sw-gpu-cloudnative',
47-
containers: [
48-
containerTemplate(name: 'docker', image: 'docker:dind', ttyEnabled: true, privileged: true)
49-
]) {
50-
node(POD_LABEL) {
51-
stage('checkout') {
52-
checkout scm
53-
}
54-
stage('dependencies') {
55-
container('docker') {
56-
sh 'apk add --no-cache make bash'
78+
def getBuildStagesForArchitectures(def architectures, def makeCommand, def makeTargetPrefix) {
79+
stages = [:]
80+
81+
for (a in architectures) {
82+
stages[a] = getBuildClosure(a, makeCommand, "${makeTargetPrefix}-${a}")
83+
}
84+
85+
return stages
86+
}
87+
88+
def getBuildClosure(def architecture, def makeCommand, def makeTarget) {
89+
return {
90+
container('docker') {
91+
stage(architecture) {
92+
sh "${makeCommand} ${makeTarget}"
5793
}
5894
}
59-
stage('build-one') {
60-
parallel (
61-
getSingleBuildForArchitectures(["amd64", "ppc64le", "arm64"])
62-
)
63-
}
64-
stage('build-all') {
65-
parallel (
66-
getAllBuildForArchitectures(["amd64", "ppc64le", "arm64", "x86_64", "aarch64"])
67-
)
95+
}
96+
}
97+
98+
def getLintClosure(def target) {
99+
return {
100+
container('golang') {
101+
stage(target) {
102+
sh "make ${target}"
103+
}
68104
}
69105
}
70-
}
106+
}

Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,39 @@ include $(CURDIR)/docker/docker.mk
2929

3030
binary:
3131
go build -ldflags "-s -w" -o "$(LIB_NAME)" $(GOLANG_PKG_PATH)
32+
33+
# Define the check targets for the Golang codebase
34+
MODULE := .
35+
.PHONY: check fmt assert-fmt ineffassign lint misspell vet
36+
check: assert-fmt lint misspell vet
37+
fmt:
38+
go list -f '{{.Dir}}' $(MODULE)/... \
39+
| xargs gofmt -s -l -w
40+
41+
assert-fmt:
42+
go list -f '{{.Dir}}' $(MODULE)/... \
43+
| xargs gofmt -s -l > fmt.out
44+
@if [ -s fmt.out ]; then \
45+
echo "\nERROR: The following files are not formatted:\n"; \
46+
cat fmt.out; \
47+
rm fmt.out; \
48+
exit 1; \
49+
else \
50+
rm fmt.out; \
51+
fi
52+
53+
ineffassign:
54+
ineffassign $(MODULE)/...
55+
56+
lint:
57+
# We use `go list -f '{{.Dir}}' $(GOLANG_PKG_PATH)/...` to skip the `vendor` folder.
58+
go list -f '{{.Dir}}' $(MODULE)/... | xargs golint -set_exit_status
59+
60+
misspell:
61+
misspell $(MODULE)/...
62+
63+
vet:
64+
go vet $(MODULE)/...
65+
66+
test:
67+
go test $(MODULE)/...

0 commit comments

Comments
 (0)