Skip to content

Commit d68dba4

Browse files
authored
Merge pull request #1148 from zhuay-a/patch-1
Jenkins插件功能优化
2 parents 5ac1479 + 9d94ba8 commit d68dba4

3 files changed

Lines changed: 73 additions & 9 deletions

File tree

doc/zh/guide/插件/Jenkins_Plugin.md

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
### 获取Jenkins插件
88
Jenkins插件有以下两种获取方式:
99

10-
**方式一**:在 TCA 源码的`plugin/jenkins_plugin`目录下,执行命令`mvm package -DskipTests`,打包完成后进入target目录会看到`Jenkins_plugin.hpi` 的安装包。
10+
**方式一**:在 TCA 源码的`plugin/jenkins_plugin`目录下,执行命令`mvn package -DskipTests`,打包完成后进入target目录会看到`tca_jenkins_plugin.hpi` 的安装包。
1111

1212
**方式二**:从TCA release 安装包中,获取`jenkins_plugin.hpi`[https://github.com/Tencent/CodeAnalysis/releases](https://github.com/Tencent/CodeAnalysis/releases)
1313

14-
1514
### 在Jenkins安装插件
1615
在Jenkins中通过【Manage Plugin】-> 【Advanced】->【Deploy plugin】的方式选择 Jenkins_plugin.hpi文件上传安装,并重启Jenkins。
1716
![](../../../images/jenkins_manage_plugins.png)
@@ -20,6 +19,12 @@ Jenkins插件有以下两种获取方式:
2019
最终在【Installed】里搜索出【TCA】代表插件安装成功。
2120
![](../../../images/jenkins_installed_plugins.png)
2221

22+
### 完善启动客户端的环境
23+
在CodeAnalysis目录下执行代码
24+
```shell
25+
bash ./scripts/base/install_bin.sh
26+
```
27+
`client`目录下的`config.ini`文件中的`<Server IP地址>`替换部署的开源版TCA的IP地址(可包含端口号)
2328

2429
## 使用插件
2530
### 在 TCA 创建团队和项目
@@ -35,24 +40,23 @@ Value:`GITPATH` Value:xxxx(路径不包含git)
3540
![](../../../images/jenkins_manage_jenkins.png)
3641
<img src="../../../images/jenkins_environment_vars.png" width = "400"/>
3742

38-
39-
### 创建一个构建任务,配置代码库信息
40-
进入Jenkins,通过【New Item】创建一个空白任务,在任务配置中【Source Code Management】配置待分析的代码库地址和凭证。
43+
### 配置 TCA 插件
44+
#### 方式一:可视化界面配置 TCA 插件
45+
创建一个构建任务,配置代码库信息,进入Jenkins,通过【New Item】创建一个空白任务,在任务配置中【Source Code Management】配置待分析的代码库地址和凭证。
4146
`Repository URL`: 填入远端仓库地址
4247
`Credentials`: 添加仓库的用户名和密码作为凭证,如果是公开仓库,可以不设置仓库凭证
4348

4449
<img src="../../../images/jenkins_new_item.png" width = "300"/>
4550

4651
![](../../../images/jenkins_git_config.png)
4752

48-
### 配置 TCA 插件
4953
在构建任务的【Build】中选择【TCA】插件并配置以下参数:
5054

51-
`CodeAnalysis`: 拉取代码所在的绝对路径
55+
`CodeAnalysis目录绝对路径`: 拉取到本地的CodeAnalysis开源仓库目录的绝对路径(例如:/User/CodeAnalysis)
5256
`团队ID`: 在 TCA 中创建的团队的标识ID,可在TCA【团队概览】中获取“团队唯一标识”
5357
`项目名称`: 在 TCA 中创建的项目的标识ID,可在TCA【项目概览】中获取“项目唯一标识”
5458
`Token`: 在 TCA 的【个人中心】->【个人令牌】中获取
55-
`分支名称`: 需要扫描的代码分析名称
59+
`分支名称`: 需要扫描的代码分支名称
5660
`语言类别`: 项目需要扫描的语言
5761
`分析方案模板ID`: 需要使用的分析方案模板ID,在分析方案模板的“基础属性”中获取,将根据此模板创建分析方案(选填)
5862
`分析方案名称`: 指定创建出来的分析方案的名称(选填)
@@ -63,6 +67,34 @@ Value:`GITPATH` Value:xxxx(路径不包含git)
6367

6468
![](../../../images/jenkins_tca_plugin_config.png)
6569

70+
#### 方式二:pipeline语法配置 TCA 插件
71+
在步骤中添加TCA插件参数配置语句,下面的配置语句可作为参考
72+
73+
```pipeline
74+
pipeline{
75+
agent any
76+
77+
stages{
78+
stage('Build'){
79+
steps{
80+
TCA_Builder(codeAnalysisPath: '/zhuay/CodeAnalysis/', teamId: 'xxxx', projectName: 'demo', token: 'xxxxxxxxxxxx', branchName: 'master', languageType: 'Java', refSchemeID: '1', scanPlan: 'model', threshold: '90', total:true)
81+
}
82+
}
83+
}
84+
}
85+
86+
```
87+
`codeAnalysisPath`: 拉取到本地的CodeAnalysis开源仓库目录的绝对路径(例如:/User/CodeAnalysis)
88+
`teamId`:团队ID
89+
`projectName`: 项目名称
90+
`token`: 在 TCA 的【个人中心】->【个人令牌】中获取
91+
`branchName`: 需要扫描的代码分支名称
92+
`languageType`: 项目需要扫描的语言
93+
`refSchemeID`: 需要使用的分析方案模板ID,在分析方案模板的“基础属性”中获取,将根据此模板创建分析方案(选填)
94+
`scanPlan`: 指定创建出来的分析方案的名称(选填)
95+
`threshold`: 设置质量门禁值
96+
`total`: 是否全量扫描,填`ture`为全量扫描,不填或填`false`为增量扫描
97+
6698
### 启动构建并查看结果
6799
点击【Build Now】启动构建。
68100
进入构建任务,在【Console Output】中查看执行过程。
@@ -74,6 +106,8 @@ Value:`GITPATH` Value:xxxx(路径不包含git)
74106
## 设置质量门禁
75107
在上述 TCA 插件配置部分填写`质量门禁`参数,需要填写一个整数,即当前分支的扫描问题量大于该质量门禁值时,判断为不通过;否则为通过。完成后会将TCA结果状态(`success`|`failure`)输出到工作空间下的`tca_threshold.txt`文件中,供后续步骤判断和终止流水线。
76108

109+
### 可视化界面使用质量门禁
110+
77111
在TCA插件后增加shell命令步骤,输入以下脚本内容:
78112

79113
![](../../../images/jenkins_shell.png)
@@ -87,5 +121,30 @@ else
87121
exit 255
88122
fi
89123
```
90-
91124
当质量门禁不通过时,会终止流水线(退出码:255)。
125+
126+
### pipeline脚本使用质量门禁
127+
以下是pipeline脚本使用质量门禁进行相应操作的示例,你可以在if和else部分写入你想要运行的脚本
128+
129+
```pipeline
130+
pipeline{
131+
agent any
132+
133+
stages{
134+
stage('Build'){
135+
steps{
136+
TCA_Builder(codeAnalysisPath: '/zhuay/CodeAnalysis/', teamId: 'xxxx', projectName: 'demo', token: 'xxxxxxxxxxxx', branchName: 'master', languageType: 'Java', refSchemeID: '1', scanPlan: 'model', threshold: '90', total:true)
137+
script{
138+
def tca_status = readFile('tca_threshold.txt')
139+
if (tca_status == "success") {
140+
echo ">> tca scan pass!"
141+
} else {
142+
echo ">> tca scan fail! exit code 255"
143+
error("TCA scan failed. Terminating pipeline.")
144+
}
145+
}
146+
}
147+
}
148+
}
149+
}
150+
```

plugins/jenkins_plugin/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,7 @@
7070
<url>https://repo.jenkins-ci.org/public/</url>
7171
</pluginRepository>
7272
</pluginRepositories>
73+
<build>
74+
<finalName>tca_jenkins_plugin</finalName>
75+
</build>
7376
</project>

plugins/jenkins_plugin/src/main/java/io/jenkins/plugins/TCABuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import hudson.util.FormValidation;
2020
import jenkins.tasks.SimpleBuildStep;
2121
import org.apache.commons.lang.StringUtils;
22+
import org.jenkinsci.Symbol;
2223
import org.json.JSONObject;
2324
import org.kohsuke.stapler.DataBoundConstructor;
2425
import org.kohsuke.stapler.DataBoundSetter;
@@ -230,6 +231,7 @@ public void perform(@NonNull Run<?, ?> run, @NonNull FilePath workspace, @NonNul
230231
}
231232

232233
@Extension
234+
@Symbol("TCA")
233235
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
234236
public FormValidation doCheckCodeAnalysisPath(@QueryParameter String value) throws IOException, ServletException {
235237
if (StringUtils.isBlank(value)){

0 commit comments

Comments
 (0)