Continuous integration gitlab + jenkins (pipeline) line code that implements automatic
Preparing the environment:
Centos7.6 version
ip: 192.168.0.13 host name: git
ip: 192.168.0.23 host name: Jenkins
ip: 192.168.0.15 host name: web01
turn off the firewall
turned off selinux
distribute a public key so that the two inter-Two Masters can password-free connectivity
The host 192.168.0.13
1. Installation gitlab
# gitlab mounted below the first installation dependencies
[@ Git git_data the root] # yum the install the policycoreutils -Y-curl Python OpenSSH-Server
# gitlab install package
[root @ git git_data] # rpm -ivh gitlab-ce -10.2.0-ce.0.el7.x86_64.rpm
2. Modify gitlab profile
[root@git git_data]# vim /etc/gitlab/gitlab.rb
...
external_url 'http://192.168.0.13' line # 13 in the position, to modify the ip address is a native address url
3. Reconfigure gitlab
[root@git ~]# gitlab-ctl reconfigure
4. Review the status of gitlab
[root@git monitor]# gitlab-ctl status
5. Then you can log gitlab the modified password Google browser input http://192.168.0.13, first login will be prompted to change your password, user name is root, password is
The host 192.168.0.23
1.安装jenkins和jdk
[root@jenkins ~]# rpm -ivh jdk-8u181-linux-x64.rpm
[root@jenkins ~]# rpm -ivh jenkins-2.99-1.1.noarch.rpm
2. Edit the configuration file for the user to change the root
[Jenkins the root @ ~] # Vim / etc / sysconfig / Jenkins
...
JENKINS_USER="root"
3. Confirm jdk installed restarted Jenkins
[Jenkins the root @ ~] # Start Jenkins systemctl
4. Log jenkins, in the browser enter http://192.168.0.23, the first time you log in you will be prompted to enter a password
to copy the password in the following files to the web page to the password box
[root @ jenkins ~] # cat / var / lib / Jenkins / Secrets / initialAdminPassword
fc0da02333a443799c90dead163240a0
5. Enter the password to continue may be prompted to create a user, temporarily created, because the default administrator account password
administrator account to change the password for the admin login to post 123
6.接着安装jenkins的必要插件
下载好插件包jenkins-plugins.tar.gz
安装jenkins插件,把下载的压缩包解压到/var/lib/jenkins/plugins
把压缩包里的plugin目录下的文件移动到/var/lib/jenkins/plugins下即可
7.刷新页面在系统管理下就可查看到安装的插件了
192.168.0.15主机上
1.搭建好nginx环境
安装nginx,安装配置方法可看我上一篇博客
上面步骤完成了gitlab,jenkins和nginx的搭建和部署,接下来就可以进行代码项目的部署了
为了练习,这里在码云上面找了个开源项目
接着把码云的项目代码导入到gitlab,方法如下图1,2,3
完成代码导入后继续下面的操作
代码的拉取和推送要先有代码仓库权限,因此要把git和jenkins主机的root用户的ssh公钥放到gitlab上,
然后可以在命令行进行测试:git clone
在gitlab添加公钥的方法如下图
开始新建pipeline项目,pipeline的声明式脚本执行后可以查看到详细的每一步执行,如果有错误还会提示错误位置和信息。
下面图片的顺序为操作步骤的顺序,要按照下面图片顺序来搭建:
下面代码为pipeline的声明式脚本
pipeline{
agent any
stages{
stage("get code"){
steps{
echo "get code"
}
}
stage("unit test"){
steps{
echo "unit test"
}
}
stage("package"){
steps{
sh 'tar zcf /opt/web-${BUILD_ID}.tar.gz' ./* --exclude=.git --exclude=Jenkinsfile
}
}
stage("deploy"){
steps{
sh 'ssh 192.168.0.15 "cd /usr/share/nginx/ && mkdir web-${BUILD_ID}"'
sh 'scp /opt/web-${BUILD_ID}.tar.gz 192.168.0.15:/usr/share/nginx/web-${BUILD_ID}'
sh 'ssh 192.168.0.15 "cd /usr/share/nginx/web-${BUILD_ID} && tar xf web-${BUILD_ID}.tar.gz && rm -fr web-${BUILD_ID}.tar.gz"'
sh 'ssh 192.168.0.15 "cd /usr/share/nginx/ && rm -fr html && ln -s web-${BUILD_ID} html"'
}
}
}
}
以下为测试部分:
谷歌浏览器输入http://192.168.0.15
下图为修改代码的操作
[root@git monitor]# grep -n "dark上传模拟" index.html #修改后查看修改的代码
43: <a class="logo pull-left" href="index.html" style="width: 233px">dark上传模拟</a>
[root@git monitor]# git commit -am "modified index.html" #把修改的代码文件提交到本地仓库
[master 19bf27d] modified index.html
1 file changed, 1 insertion(+), 1 deletion(-)
[root@git monitor]# git push -u origin master #把本地仓库的文件提交到远程仓库gitlab上
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 329 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To [email protected]:dark/monitor.git
4eae6ad..19bf27d master -> master
Branch master set up to track remote branch master from origin.
过一会刷新页面后,代码成功自动更改,就成功了
#####################博客文章为原创,仅供参考学习使用######################