gitlab+jenkins环境搭建.md
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gitlab+jenkins环境搭建.md相关的知识,希望对你有一定的参考价值。
gitlab+jenkins自动化部署环境搭建
环境说明
系统 | 主机 | IP | 安装软件 |
---|---|---|---|
CentOS 7 | study-1 | 192.168.100.51 | gitlab、git |
CentOS 7 | study-2 | 192.168.100.52 | jdk、maven、jenkins、git |
CentOS 7 | study-3 | 192.168.100.53 | tomcat、mysql、jenkins、git |
所有系统的selinux和iptables都已经关闭,同时需要做好时间同步,机器的hosts文件如下:
192.168.100.51 study-1 gitlab.study.com
192.168.100.52 study-2
192.168.100.53 study-3
下面还需要在study-1机器上生成的公钥和私钥复制到study-2和study-3主机上。
[[email protected] ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory ‘/root/.ssh‘.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
f0:02:05:f7:5c:9c:66:d2:25:42:42:a4:11:dc:27:92 [email protected]
The key‘s randomart image is:
+--[ RSA 2048]----+
| .+B*.oooo. |
| E=o+oo*. |
| o..oo+ |
| . o |
| . S |
| . |
| |
| |
| |
+-----------------+
[[email protected] ~]# ssh-copy-id [email protected]
[[email protected] ~]# scp .ssh/id_rsa* [email protected]:/root/.ssh
[[email protected] ~]# ssh-copy-id [email protected]
[[email protected] ~]# scp .ssh/id_rsa* [email protected]:/root/.ssh
gitlab主机配置
下载
下载地址:https://mirror.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/
安装
[[email protected] ~]# yum install -y gitlab-ce-8.16.9-ce.0.el7.x86_64.rpm
配置
安装后修改配置文件中的如下内容:
[[email protected] ~]# vim /etc/gitlab/gitlab.rb
......
external_url ‘http://gitlab.study.com‘
......
[[email protected] ~]# gitlab-ctl reconfigure
[[email protected] ~]# gitlab-ctl start
启动后需要登录:http://gitlab.study.com
然后去设置用户root的密码这里不做介绍。之后需要进行密钥的配置,这个比较简单就不错介绍
测试程序配置
这里的测试代码是用的如下的内容:https://git.oschina.net/zchuanzhao/jeesns.git
下面是在gitlab克隆测试的代码:
tomcat主机配置
jdk安装
[[email protected] ~]# tar xf jdk-7u79-linux-x64.tar.gz -C /usr/local/
[[email protected] ~]# ln -sv /usr/local/jdk1.7.0_79 /usr/local/java
‘/usr/local/java’ -> ‘/usr/local/jdk1.7.0_79’
[[email protected] ~]# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/jdk1.7.0_79
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
[[email protected] ~]# . /etc/profile.d/java.sh
[[email protected] ~]# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
tomcat安装
[[email protected] ~]# tar xf apache-tomcat-7.0.72.tar.gz -C /usr/local/
[[email protected] ~]# ln -sv /usr/local/apache-tomcat-7.0.72 /usr/local/tomcat
‘/usr/local/tomcat’ -> ‘/usr/local/apache-tomcat-7.0.72’
[[email protected] ~]# vim /etc/profile.d/tocmat.sh
export PATH=$PATH:/usr/local/tomcat/bin
[[email protected] ~]# . /etc/profile.d/tocmat.sh
[[email protected] ~]# catalina.sh start
MySQL安装
[[email protected] ~]# yum install -y perl-Data-Dumper
[[email protected] ~]# tar xf mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
[[email protected] ~]# ln -sv /usr/local/mysql-5.6.31-linux-glibc2.5-x86_64 /usr/local/mysql
‘/usr/local/mysql’ -> ‘/usr/local/mysql-5.6.31-linux-glibc2.5-x86_64’
[[email protected] ~]# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
[[email protected] ~]# . /etc/profile.d/mysql.sh
[[email protected] ~]# cd /usr/local/mysql
[[email protected] mysql]# groupadd -r mysql
[[email protected] mysql]# useradd -r -g mysql -M -s /sbin/nologin mysql
[[email protected] mysql]# vim /etc/my.cnf
[mysqld]
datadir=/data/mydata
socket=/tmp/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/data/mydata/err.log
pid-file=/var/run/mysql/mariadb.pid
!includedir /etc/my.cnf.d
[[email protected] mysql]# mkdir -pv /data/mydata
[[email protected] scripts]# ./scripts/mysql_install_db --datadir=/data/mydata/ --basedir=/usr/local/mysql --user=mysql
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql]# chmod +x /etc/init.d/mysqld
[[email protected] mysql]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
导入sql语句:
[[email protected] ~]# mysql
mysql> create database jeesns character set utf8;
Query OK, 1 row affected (0.00 sec)
mysql> use jeesns
Database changed
mysql> source /root/jeesns.sql;
sql文件请在:http://git.oschina.net/zchuanzhao/jeesns 上面进行下载。
jenkins主机配置
下载路径:https://pkg.jenkins.io/redhat-stable/
jdk安装
[[email protected] ~]# tar xf jdk-7u79-linux-x64.tar.gz -C /usr/local/
[[email protected] ~]# ln -sv /usr/local/jdk1.7.0_79 /usr/local/java
‘/usr/local/java’ -> ‘/usr/local/jdk1.7.0_79’
[[email protected] ~]# vim /etc/profile.d/java.sh
[[email protected] ~]# . /etc/profile.d/java.sh
[[email protected] ~]# chown -R root.root /usr/local/java/
[[email protected] ~]# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
maven安装
[[email protected] ~]# tar xf apache-maven-3.5.0-bin.tar.gz -C /usr/local/
[[email protected] ~]# ln -sv /usr/local/apache-maven-3.5.0 /usr/local/maven
‘/usr/local/maven’ -> ‘/usr/local/apache-maven-3.5.0’
[[email protected] ~]# vim /etc/profile.d/maven.sh
[[email protected] ~]# . /etc/profile.d/maven.sh
[[email protected] ~]# chown -R root.root /usr/local/maven/
[[email protected] ~]# mvn -version
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
Maven home: /usr/local/maven
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: /usr/local/jdk1.7.0_79/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-327.el7.x86_64", arch: "amd64", family: "unix"
jenkins 安装
[[email protected] ~]# yum install -y jenkins-2.46.2-1.1.noarch.rpm
[[email protected] ~]# vim /etc/init.d/jenkins
···
candidates="
/usr/local/jdk1.7.0_79/bin/java #需要添加jdk的路径
/etc/alternatives/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-1.7.0/bin/java
/usr/lib/jvm/jre-1.7.0/bin/java
/usr/bin/java
"
···
[[email protected] ~]# systemctl daemon-reload
[[email protected] ~]# /etc/init.d/jenkins start
Starting jenkins (via systemctl): [ OK ]
在浏览器输入:http://192.168.100.52:8080
,会提示需要登录密码:
第一次登录需要的密码是安装自动生成的,路径已经说明在/var/lib/jenkins/secrets/initialAdminPassword
文件中。
插件安装
实用插件
ios专用:Xcode integration
android专用:Gradle plugin
Gitlab插件:GitLab Plugin 和 Gitlab Hook Plugin
Git插件: Git plugin
GitBuckit插件: GitBuckit plugin
签名证书管理插件: Credentials Plugin 和Keychains and Provisioning Profiles Management
FTP插件: Publish over FTP
脚本插件: Post-Build Script Plug-in
修改Build名称/描述(二维码): build-name-setter / description setter plugin
获取仓库提交的commit log: Git Changelog Plugin
自定义全局变量: Environment Injector Plugin
自定义邮件插件: Email Extension Plugin
获取当前登录用户信息: build-user-vars-plugin
显示代码测试覆盖率报表: Cobertura Plugin
来展示生成的单元测试报表,支持一切单测框架,如junit、nosetests等: Junit Plugin
其它: GIT plugin / SSH Credentials Plugin
安装插件
点击左侧的“系统管理”----"管理插件"------“已安装”---查找需要安装的插件:git-client/scm-api/git/publish over SSH//ssh
jenkins设置
密钥的配置
[[email protected] opt]# cp /root/.ssh/* /var/lib/jenkins/.ssh/
[[email protected] opt]# chown -R jenkins.jenkins /var/lib/jenkins/.ssh/
Global Tool Configuration 配置
点击左侧的“系统管理”----"Global Tool Configuration"
下面是jdk和git的配置:
下面是maven的配置:
Credentials 配置
点击左侧“Credentials”-->“System”-->在“Global credentials (unrestricted)”选择“Add Credentials”-->写上登录gitlab上的帐号密码。
系统设置
配置gitlab主机:
jenkins的访问地址,自动生成可以不用配置:
Publish over SSH配置:
Configure Global Security 配置
SSH Server 端口的修改,这里使用的是默认端口:
安装排错
构建时控制台报错如下:
Host key verification failed.
lost connection
Build step ‘Execute shell‘ marked build as failure
SSH: Current build result is [FAILURE], not going to run.
Finished: FAILURE
原因:这个是由于ssh密钥配置的问题,虽然我们在系统配置中配置了私钥但是还是不行。原先我是用root运行jenkins,而rpm包是用普通用户运行。所以密钥需要拷贝至jenkins的家目录中及:/var/lib/jenkins/.ssh/
中才能使用。
构建项目
创建
点击左侧的“新建”然后如下:
源码管理
构建
点击“增加构建步骤”-->选择“Execute shell”:
点击“增加构建步骤”-->选择“Invoke top-level Maven targets”:
配置了上面的信息后,maven会将打包的war包放在下面的这个目录中:
/var/lib/jenkins/jobs/study/workspace/target/
/var/lib/jenkins/:jenkins的工作目录
jobs/study/:jenkins管理study项目的目录
workspace:表示maven等插件的工作目录
点击“增加构建步骤”-->选择“Execute shell”:
构建后操作
选择“增加构建后操作步骤”-->选择“Send build artifacts over SSH”:
上面图片来自:http://www.cnblogs.com/horizonli/p/5332515.html 。之后就是保存了。我们还可以在上面中继续添加server,添加server之前需要提前做好设置。
执行构建的项目
下面就是选择刚才创建的项目并执行“立即构建”。
构建后我们还可以在“Console Output”中查看构建执行的状态。
下面是远程主机执行的脚本,没有做过多的判断只能能够让其运行起来就行了。
[[email protected] jeesns]# cat /opt/deploy/deploy.sh
#!/bin/bash
tomcatPid=`ps aux |grep tomcat |grep -v grep|awk ‘{print $2}‘`
kill $tomcatPid
sleep 5
rm -rf /usr/local/tomcat/webapps/jeesns*
cp /opt/deploy/jeesns/jeesns.war /usr/local/tomcat/webapps/
/usr/local/tomcat/bin/catalina.sh start
echo ‘部署完毕‘
以上是关于gitlab+jenkins环境搭建.md的主要内容,如果未能解决你的问题,请参考以下文章
Jenkins+Maven+Gitlab+Nexus持续集成环境搭建
Linux下的Jenkins+Tomcat+Maven+Gitlab+Shell环境的搭建使用(jenkins自动化部署)