Jenkins 之 Go 项目编译与代码静态检查

2017-04-25  籽藤 

公司后端代码主要是 Go + PHP。我这两天把 Go 项目静态检查的工作搬到 Jenkins 上了,省去了本地 download 项目,整理报告的时间。

实现场景

  1. Jenkins 从 Gitlab 上拉取最新代码

  2. 编译项目,下载  gometalinter :https://github.com/alecthomas/gometalinter

  3. 执行 gometalinter 命令,进行源码分析,生成 xml 格式的 checkstyle 报告

  4. 在 Jenkins 上查看 checkstyle 报告

Go 小白记录一下 Jenkins 配置过程。

第一步:安装 Go 环境插件和 CheckStyle 报告解析插件。

Go 插件源码:https://github.com/jenkinsci/golang-plugin

Go 插件介绍页:https://wiki.jenkins-ci.org/display/JENKINS/Go+Plugin

CheckStyle 插件介绍页:https://wiki.jenkins-ci.org/display/JENKINS/Checkstyle+Plugin

Go 插件安装完成后,要完成某个 Go 版本的安装。需要注意的是:官方介绍是在 Manage Jenkins →Configure System 中配置 Go installations,但实际上是在 GlobalTool Configuration 中配置。

第二步:Go 项目配置如下:

  • Source Code Management - Git :略。

  • Build Environment 中,勾选“Set up Go programming language tools”,选择合适的 Go 版本

  • 通过以下 Shell 脚本完成项目构建,gometalinter 下载和执行。需要说明的是,$JENKINS_HOME 是事先在 Jenkins 的 Configure System - Global properties 中配置的变量。而 $GOROOT 是 Go 插件自动完成的变量配置。

# print the env variables

echo $JENKINS_HOME

echo $GOROOT


# prepare the dependencies 

mkdir -p $GOROOT/src/git.applewu.com/API/

cp -r $WORKSPACE/vendor/* $GOROOT/src/

ln -Ffs $WORKSPACE $GOROOT/src/git.applewu.com/API/

cd $GOROOT/src/git.applewu.com/API/pingpp-async-service

go install 


go get -u gopkg.in/alecthomas/gometalinter.v1

export PATH=$PATH:$JENKINS_HOME/go/bin

gometalinter.v1 --install

gometalinter.v1 --disable-all --enable=interfacer --enable=staticcheck --enable=gas --enable=gocyclo --enable=ineffassign --enable=lll --enable=dupl --enable=deadcode --enable=goimports --enable=unconvert --enable=unused --enable=varcheck --enable=aligncheck --enable=goconst --enable=gosimple --enable=misspell --enable=structcheck --enable=errcheck --enable=gotype --checkstyle >> checkstyle.xml

为了避免第三方包导入的问题,shell 脚本中将 Go 项目的 vendor 目录内容拷贝到 GOROOT 中。并将当前项目作为软链也加入到 GOROOT 中。

  • 在 Post-build Actions 中选择“Publish Checkstyle analysis results”配置为:checkstyle.xml

第三步:build 项目,查看结果。以下是 checkstyle 报告效果。

1502°/14939 人阅读/9 条评论 发表评论

雪儿欢  2018-01-05

大神,为什么我检查本地代码的时候,checkstyle 报告可以报错,但是从github下拉下来代码 检查报告也生成了 也解析成功了 但是展示的时候报错了:报 Using GitBlamer to create author and commit information for all warnings. GIT_COMMIT=38b6ba90052105f0f4ffe2f589ddaa5c89d7c7e5, workspace=/Users/xueerhuan/go/src/vipsvr.yq.tc > /usr/local/git/libexec/git-core/git rev-parse 38b6ba90052105f0f4ffe2f589ddaa5c89d7c7e5^{commit} # timeout=10 ERROR: Build step failed with exception org.eclipse.jgit.errors.MissingObjectException: Missing commit d1ea43a23ff01346087d77da1eb6b6e3a7a4ae64 at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:159) at org.eclipse.jgit.revwalk.RevWalk.getCachedBytes(RevWalk.java:903) at org.eclipse.jgit.revwalk.RevCommit.parseHeaders(RevCommit.java:155) at org.eclipse.jgit.revwalk.RevWalk.parseHeaders(RevWalk.java:1012) at org.eclipse.jgit.blame.BlameGenerator.processOne(BlameGenerator.java:601) at org.eclipse.jgit.blame.BlameGenerator.next(BlameGenerator.java:496) at org.eclipse.jgit.blame.BlameResult.computeAll(BlameResult.java:239) at org.eclipse.jgit.blame.BlameGenerator.computeBlameResult(BlameGenerator.java:450) at org.eclipse.jgit.api.BlameCommand.call(BlameCommand.java:230) Caused: org.eclipse.jgit.api.errors.JGitInternalException: Missing commit d1ea43a23ff01346087d77da1eb6b6e3a7a4ae64 at org.eclipse.jgit.api.BlameCommand.call(BlameCommand.java:232) at hudson.plugins.analysis.util.GitBlamer$BlameCallback.invoke(GitBlamer.java:130) at hudson.plugins.analysis.util.GitBlamer$BlameCallback.invoke(GitBlamer.java:104) at org.jenkinsci.plugins.gitclient.AbstractGitAPIImpl.withRepository(AbstractGitAPIImpl.java:29) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.withRepository(CliGitAPIImpl.java:71) at hudson.plugins.analysis.util.GitBlamer.loadBlameResultsForFiles(GitBlamer.java:61) at hudson.plugins.analysis.util.GitBlamer.blame(GitBlamer.java:54) at hudson.plugins.analysis.util.AbstractBlamer$1.invoke(AbstractBlamer.java:62) at hudson.plugins.analysis.util.AbstractBlamer$1.invoke(AbstractBlamer.java:59) at hudson.FilePath.act(FilePath.java:997) at hudson.FilePath.act(FilePath.java:975) at hudson.plugins.analysis.util.AbstractBlamer.invokeBlamer(AbstractBlamer.java:59) at hudson.plugins.analysis.util.AbstractBlamer.blame(AbstractBlamer.java:45) at hudson.plugins.analysis.core.HealthAwarePublisher.blame(HealthAwarePublisher.java:109) at hudson.plugins.checkstyle.CheckStylePublisher.perform(CheckStylePublisher.java:81) at hudson.plugins.analysis.core.HealthAwarePublisher.perform(HealthAwarePublisher.java:69) at hudson.plugins.analysis.core.HealthAwareRecorder.perform(HealthAwareRecorder.java:298) at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:81) at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20) at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744) at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690) at hudson.model.Build$BuildExecution.post2(Build.java:186) at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635) at hudson.model.Run.execute(Run.java:1752) at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43) at hudson.model.ResourceController.execute(ResourceController.java:97) at hudson.model.Executor.run(Executor.java:429) Build step 'Publish Checkstyle analysis results' marked build as failure错如下:


籽藤  2018-01-09

@雪儿欢 hello, 我看你的报错,应该还是解析结果有问题,为何你认为“解析成功了 但是展示的时候报错了”?是不是有直接 remote 到 Jenkins 服务器上去执行,才得出这个结论?如果你本地执行无误的话,建议你 remote 到 Jenkins 服务器上执行命令。若执行成功,再通过 Jenkins Job 的方式构建。若执行失败,把你 Jenkins 上的 log 再贴出来吧。


雪儿欢  2018-01-19

已经解决了,是因为我从github上拉取代码,生成的报告在github的本地路径,所以读取报告的时候报错了


雪儿欢  2018-01-19

你有没有遇到过一个问题,gometalinter报告里一直会报could not import XXX/AAA/XXX(包名) 这种错误,然而这个包命名是存在的,本地编译也是没有问题,为什么gometalinter的静态检查会有这种错误呢


籽藤  2018-01-23

@雪儿欢 gometalinter 其实是个工具集,你知道具体是里面哪个工具报出来的吗?


我狠在乎  2018-05-07

大神 在吗?新手,最近在部署Jenkins代码静态检查,感觉一直出问题,能加个wx请教一下吗?


我狠在乎  2018-05-09

gometalinter里面的工具会重复检查,报出很多重复警告,大神知道有什么好的办法筛选一下吗 ?


我狠在乎  2018-05-09

account/accounts.go:21:2:warning: unused variable or constant could not import github.com/hpb-project/ghpb/common (cannot find package "github.com/hpb-project/ghpb/common" in any of: (varcheck) account/accounts.go:22:2:warning: unused variable or constant could not import github.com/hpb-project/ghpb/core/types (cannot find package "github.com/hpb-project/ghpb/core/types" in any of: (varcheck) account/accounts.go:23:2:warning: unused variable or constant could not import github.com/hpb-project/ghpb/core/event (cannot find package "github.com/hpb-project/ghpb/core/event" in any of: (varcheck) account/accounts.go:40:8:warning: unused variable or constant undeclared name: URL (varcheck) account/accounts.go:30:10:warning: unused variable or constant undeclared name: common (varcheck) account/accounts.go:31:10:warning: unused variable or constant undeclared name: URL (varcheck) account/accounts.go:95:30:warning: unused variable or constant undeclared name: types (varcheck) account/accounts.go:95:69:warning: unused variable or constant undeclared name: types (varcheck) account/accounts.go:109:63:warning: unused variable or constant undeclared name: types (varcheck) account/accounts.go:109:102:warning: unused variable or constant undeclared name: types (varcheck) account/accounts.go:129:37:warning: unused variable or constant undeclared name: event (varcheck) account/accounts.go:21:2:warning: could not import github.com/hpb-project/ghpb/common (cannot find package "github.com/hpb-project/ghpb/common" in any of: (megacheck) account/accounts.go:21:2:error: could not import github.com/hpb-project/ghpb/common (can't find import: ) (gotype) account/accounts.go:22:2:error: could not import github.com/hpb-project/ghpb/core/types (can't find import: ) (gotype) account/accounts.go:23:2:error: could not import github.com/hpb-project/ghpb/core/event (can't find import: ) (gotype) account/accounts.go:40:8:error: undeclared name: URL (gotype) account/accounts.go:30:10:error: undeclared name: common (gotype) account/accounts.go:31:10:error: undeclared name: URL (gotype) account/accounts.go:95:30:error: undeclared name: types (gotype) account/accounts.go:95:69:error: undeclared name: types (gotype) account/accounts.go:109:63:error: undeclared name: types (gotype) account/accounts.go:109:102:error: undeclared name: types (gotype) account/accounts.go:129:37:error: undeclared name: event (gotype)


籽藤  2018-06-05

@我狠在乎 报错信息 "could not import" 看起来是 go install 有问题。项目依赖的 github 库在 gopath 中找不到。这些库一般会在开发项目的 vendor 目录中,你可以拷贝到 gopath 里面去。有问题可以邮件联系我,会比论坛回复及时一些:wuziteng2006@163.com


登录 后发表评论