【持续集成】jenkins安装部署从git获取代码

一:持续集成的概念:

1.1:总体的概括

持续集成Continuous Integration
持续交付Continuous Delivery
持续部署Continuous Deployment

 

1.2:什么是持续集成:
持续集成是指开发者在代码的开发过程中,可以频繁的将代码部署集成到主干,并进程自动化测试

1.3:什么是持续交付:
持续交付指的是在持续集成的环境基础之上,将代码部署到预生产环境

1.4:持续部署:
在持续交付的基础上,把部署到生产环境的过程自动化,持续部署和持续交付的区别就是最终部署到生产环境是自动化的。

 

二:安装部署持续集成工具jenkins:
2.1:安装部分:
2.1.1:执行安装:
下载jdk-8u111-linux-x64.rpm

# rpm -ivh jdk-8u111-linux-x64.rpm

# vim /etc/profile

export JAVA_HOME=/usr/java/jdk1.8.0_111 
export PATH=$JAVA_HOME/bin:$PATH

安装jenkins
方法①yum方式安装

# cd /etc/yum.repos.d/
# wget http://pkg.jenkins.io/redhat/jenkins.repo
# rpm --import http://pkg.jenkins.io/redhat/jenkins.io.key
# yum install -y jenkins
# systemctl start jenkins

方法②通过war包的方式安装下载地址:http://updates.jenkins-ci.org/download/war/2.60.1/jenkins.war

复制代码
unzip apache-tomcat-8.0.37.zip
mv apache-tomcat-8.0.37 /usr/local/
cd /usr/local
ln -s /usr/local/apache-tomcat-8.0.37 /usr/local/tomcat
useradd jenkins
# 将jenkins.war放到/usr/local/tomcat/webapps目录下,解压
unzip -d jenkins jenkins.war
chown -R jenkins.jenkins /usr/local/apache-tomcat-8.0.37
bin/startup.sh
复制代码

 

2.1.2:打开web界面:第一次打开要输入随机生成的密码:
http://192.168.3.199:8080/jenkins

查看密码

$ cat /home/jenkins/.jenkins/secrets/initialAdminPassword
8743f91ff1474a85a0abcd841fc74eb6

 

输入密码点下确认之后选择插件,选择默认安装插件即可,此过程需要一段时间,而且根据网络不同有些插件安装不成功,但是之后可以自己再安装即可

报错:
An error occurred
An error occurred during installation: No such plugin: cloudbees-folder

解决办法:
下载cloudbees-folder.hpi放在目录/usr/local/tomcat/webapps/jenkins/WEB-INF/detached-plugins/下,重启tomcat即可

设置用户名密码

3.1配置邮件通知,系统管理–系统设置

添加管理员邮箱,添加邮件

点击保存或测试邮件,系统会给目标邮箱发送一封测试邮件:

安装gitlab插件,系统管理–管理插件–可选插件

插件安装界面,会额外安装一些依赖关系的插件,jenkins基于ruby开发,所以会有ruby环境:

在gitlab管理界面将用户添加到一个项目,下一步要用此用户拉取项目代码

三:使用jenkins:
3.1:添加一个认证用户,拉取git代码的时候使用:

创建任务 --> 构建一个自由风格的软件项目

项目描述信息:

配置git项目地址,我们先进行其他配置,等会再继续配置git相关的选项

系统管理 --> Global Tool Configuration

 

Kind 选择 SSH Username with private key
Username 填 root
PrivateKey 选择 From a file on jenkins master ,然后将服务器的 私钥的存放路径(私钥、私钥、私钥,再说三遍) 粘贴进去
passphrase 填创建SSH秘钥时的设置的密码,未设置可不填
如果没报错,说明成功了,点击页面底部的“apply”。如果出错了,会在“Repository URL”和“Credentials”之间显示红色的错误信息。

jenkins job默认对master分支进行构建,你也可以自定义分支。这要求你的Gitlab代码仓库中要存在这个分支,一般来说,就是要向代码仓库提交一次更改,请 自行完成(Gitlab项目刚创建时是空的,一个分支也没有,这样的话,自动构建时会出错)
---------------------
作者:魏都青年
来源:CSDN
原文:https://blog.csdn.net/qq_16538827/article/details/81873997
版权声明:本文为博主原创文章,转载请附上博文链接!


如下配置,jenkins服务器上root用户生成密钥对

复制代码
[root@node1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
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:
bf:1e:4d:b3:0f:fb:8b:71:cd:ef:d3:70:69:a4:46:ff root@node1.chinasoft.com
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| . . |
| S + + .|
| . o =.*.|
| o * oo=|
| o B .E|
| .o o.+o+|
+-----------------+
[root@node1 ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC3vhfbA4NdkP8g1WVyYkw93f1PZ04nWoyFnoerC9b40jIY5px+tkugdy/RZ3/bp4hMC5yNgV1S25Tm8RpzBJMp7pbJz8dO+LCKqUnXq9Eh0QhsZE0xlQN+J4awy9YIGiD2nFg7k/ZzeAtQRzNryrrPnKaWpXtg7tMGGTWBjeyLAVuqCOMU7euY94G26UmVfjDLIJkcJqB+8dwvodW3wpmUER32qVPKZLnSYSOCfoNed+P4Eujs5PBCLuzFvGyiDhY2Pwrk/4S11jWUa7TJItfoPXzeGc/ujaJi/o24dt8VXeFa/Rm4wywYLNW3TRjXy1mPpdVlob3701MMQ0bf3qPv root@node1.chinasoft.com
复制代码

查看公钥,id_rsa.pub是公钥。文件中的内容就是公钥。 

gitlab上配置web-demo项目设置private deploy key

把key复制进去,同时title起一个简单明了的名字

在jenkins服务器192.168.3.199上测试下是否能够正常获取代码

复制代码
[root@node1 tmp]# git clone git@192.168.3.198:web/web-demo.git
Cloning into 'web-demo'...
The authenticity of host '192.168.3.198 (192.168.3.198)' can't be established.
ECDSA key fingerprint is 9d:60:c3:8e:49:72:e8:9c:c0:d6:c8:d2:f1:b6:3a:74.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.3.198' (ECDSA) to the list of known hosts.
remote: Counting objects: 79, done.
remote: Compressing objects: 100% (79/79), done.
remote: Total 79 (delta 17), reused 0 (delta 0)
Receiving objects: 100% (79/79), 1.18 MiB | 0 bytes/s, done.
Resolving deltas: 100% (17/17), done.
复制代码

 

jenkins拉取代码没问题,继续配置jenkins的认证,这里应该输入私钥

复制jenkins拉取git代码服务器root的私钥

复制代码
[root@node1 ~]# cat .ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAt74X2wODXZD/INVlcmJMPd39T2dOJ1qMhZ6HqwvW+NIyGOac
frZLoHcv0Wd/26eITAucjYFdUtuU5vEacwSTKe6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xAuOGJg8CQrcOtka3brWfZVTVL3Q2OYMg4Iej2HXDoP8h
42zRR4u/THmhLL1O6NxVjUMCgYBslKyVbuja9T61HjXPVmDbqIIyEZ6a4NnlA6jy
7Mnq85LofCosQB00vDDH7SCyF0B75P/KaSNjH8JLtSoKfpoXVn2nkz/gxcZ9WWhP
Ve+CCuoNjDJpQriVDT74m42Jt14uJ9eaiPy+JUR4w5YOhdmZhg5NkKs75XJUkUQk
FR8FVQKBgQDOWXJYF3VEYdCD1qIy/CsY3s4ARD9kery+5aOCu6TNd6auYEfqxsev
ie1jD+cvAgVR1aZ5aP3C8jEYhZa4xgn1+hxfRLWs79uMYnXXNGJ+FxKftDyhuV+m
efEQrUf8jh4MofW9Ee6Z7YjitIRY7SQ+kZl2Xms+QZlJotBzTG2DSA==
-----END RSA PRIVATE KEY-----
复制代码

 

添加描述,点击OK

继续编辑配置

这样就不报错了,下面还可以指定从哪个分支拉取,默认从master拉取,有些公司使用release分支

源码浏览器,去gitlab上找到http的url:http://192.168.3.198/web/web-demo.git

找出gitlab的版本,输入url和版本,版本只支持2位,拉到最下面保存

[root@web01 ~]# rpm -qa|grep gitlab
gitlab-ce-8.10.5-ce.0.el7.x86_64

 

点击立即构建,查看控制台,可以看到构建的详细输出

posted @ 2018-01-19 11:15  #甜甜8023  阅读(2497)  评论(0编辑  收藏  举报