Jenkins与Gitlab持续集成

由于使用插件Gitlab Merge Request Builder失败,便尝试使用自定义变量的方式来解决Gitlab的Merge Request触发Jenkins构建的问题。解决方案如下:

安装Jenkins插件

  • GitLab Plugin
  • Gitlab Hook Plugin
  • Gitlab Merge Request Builder
  • Post-Build Script Plug-in
  • Pre SCM BuildStep Plugin
  • Environment Injector Plugin
  • BuildContext Capture Plug-in

系统设置

  • Gitlab Web Hook
  • Git plugin
  • Gitlab
  • Gitlab Merge Request Builder

源码管理

勾选Git;在Repository URL中填写自定义变量,即${SOURCE_REPOSITORY_URL}

构建环境

勾选Run buildstep before SCM runs

1.Execute shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash

if [ -f propfile.txt ];then
rm propfile.txt
touch propfile.txt
fi

i=1
number=`cat "${JENKINS_HOME}/jobs/${JOB_NAME}/nextBuildNumber"`
last=$((${number}-${i}))
last_build="${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${last// /}/build.xml"

#url=`grep -a -A1 'gitlabSourceRepoURL' ${last_build} |grep 'value' |awk -F '>' '{print $2}' |awk -F '<' '{print $1}'`

url=`grep -a -A2 '<source>' ${last_build} |grep '<ssh__url>' |awk -F '>' '{print $2}' |awk -F '<' '{print $1}'`

echo "SOURCE_REPOSITORY_URL=${url}" >propfile.txt

2.Inject environment variables

在Properties File Path中填写propfile.txt

Build设置

1
2
Root POM:pom.xml
Goals and options:package checkstyle:checkstyle -Dcheckstyle.config.location="http://192.168.0.1/checkstyle.xml"

在pom.xml中添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>6.9</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

构建后操作

选择Execute a set of scripts -> Build steps -> Execute shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
#add comment to gitlab merge request


#step1:get gitlab merge request params
i=1
number=`cat "${JENKINS_HOME}/jobs/${JOB_NAME}/nextBuildNumber"`
last=$((${number}-${i}))
last_build="${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${last// /}/build.xml"
project_id=`grep -a -o -E '<targetProjectId>[0-9]*' ${last_build} |awk -F '>' '{print $2}'`
merge_id=`grep -a -B10 '<targetProjectId>' ${last_build} |grep -a -o -E '<id>[0-9]*' |awk -F '>' '{print $2}'`

#step2:get checkstyle result from log
last_log="${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${last// /}/log"
check_result=`grep -a 'target/checkstyle-result.xml of' ${last_log}`
comment="${check_result}[点击查看详情](http://192.168.0.1/job/${JOB_NAME}/lastBuild/checkstyleResult/)"

#step3:post a comment to merge request
private_token='input-your-token'
api_add_merge_note='http://192.168.0.1/api/projects/'${project_id// /}'/merge_request/'${merge_id// /}'/comments'
request=`curl --header "PRIVATE-TOKEN: ${private_token}" -d "note=${comment}" ${api_add_merge_note}`

配置Gitlab Web Hook

http://192.168.0.1:8080/project/your-project

八宝粥 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!