Jenkins+Gitlab持续集成

Install Jenkins

Java Environment

下载jdk

1
2
3
4
wget http://download.oracle.com/otn-pub/java/jdk/8u171-b11/512cd62ec5174c3487ac17c61aaa89e8/jdk-8u171-linux-x64.tar.gz?AuthParam=1526090409_eeb74c2f68fe8539ce3cb2608695d850;

tar xvf jdk-8u171-linux-x64.tar.gz -C /opt/;
mv /opt/jdk1.8.0_171/ /opt/jdk/;

配置环境变量

编辑/etc/profile

1
2
3
4
JAVA_HOME=/opt/jdk
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH

运行环境

1
source /etc/profile

验证环境

1
2
3
4
kdreader@kdreader-staging:~$ java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)

Install jenkins

1
wget https://prodjenkinsreleases.blob.core.windows.net/debian/jenkins_2.121_all.deb; sudo  dpkg -i jenkins_2.121_all.deb;

更新/etc/init.d/jenkinsPATH变量

1
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/opt/jdk/bin/

启动 Jenkins

sudo /etc/init.d/jenkins start

配置nginx

添加/etc/nginx/sites-enabled/jenkins.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server {
listen 80;
server_name ci.kdreader.com;


server_name_in_redirect on;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header User-Agent $http_user_agent;
proxy_set_header Referer $http_referer;
proxy_connect_timeout 300;
proxy_send_timeout 300;

uwsgi_read_timeout 360;
uwsgi_send_timeout 360;

location / {
proxy_pass http://127.0.0.1:8080;
}
}

重启nginx

sudo nginx -s reload

Config Jenkins

http://ci.kdreader.com访问Jenkins

初始化秘钥路径:/var/lib/jenkins/secrets/initialAdminPassword

Install and Config Gitlab plugin

配置Credential

Gitlab生成token

gitlab-generate-token

配置Credentials

jenkins-config-credential

配置Gitlab

jenkins-config-gitlab

Install and Config SSH Remote Hosts

配置Credentials

jenkins-config-ssh-credential

配置SSH remote hosts

jenkins-config-ssh-remote-hosts

Config Email Notification

jenkins-config-email-notification

Create Project

jenkins-create-project

Buil Trigger

jenkins-project-trigger

配置Gitlab webhook

gitlab-webhook

配置安全策略

jenkins-security-config

Build

Jenkins-project-build

After Build

jenkins-project-after-build

Q&A

Question: Gitlab Webhook 403

1
2
3
4
5
6
7
8
9
10
11
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 403 anonymous is missing the Job/Build permission</title>
</head>
<body><h2>HTTP ERROR 403</h2>
<p>Problem accessing /project/kdreader. Reason:
<pre> anonymous is missing the Job/Build permission</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.z-SNAPSHOT</a><hr/>

</body>
</html>

Solution:

配置Jenkins 匿名用户安全策略

jenkins-security-config

参考