基于gitlab和jenkins的自动化部署实例

gitlab+jenkins服务简述:java

GitLab是一个代码仓库,用来管理代码。Jenkins是一个自动化服务器,能够运行各类自动化构建、测试或部署任务。
因此这二者结合起来,就能够实现开发者提交代码到GitLab,Jenkins以必定频率自动运行测试、构建和部署的任务,
帮组开发团队更高效的集成和发布代码。

实验环境:linux

红帽7.3版本虚拟机

gitlab的安装配置,官网给的配置最低要求为4G,使用虚拟机配置的话结合物理机的配置要求分配内存:ios

[root@test1 ~]# yum install curl policycoreutils openssh-server openssh-clients postfix  安装依赖性
[root@test1 ~]# systemctl status postfix.service  查看服务状态确保开启
[root@test1 ~]# yum install -y net-tools  安装工具包
[root@test1 ~]# ls
gitlab-ce-11.2.0-ce.0.el7.x86_64.rpm
[root@test1 ~]# rpm -ivh gitlab-ce-11.2.0-ce.0.el7.x86_64.rpm  安装gitlab服务

这里写图片描述
安装过程比较慢由于是跟咱们分配内存大小相关,安装成功会出来gitlab的图案:
这里写图片描述git

[root@test1 ~]# vim /etc/gitlab/gitlab.rb 编辑配置文件写入当前主机IP,修改gitlab仓库路径

这里写图片描述

[root@test1 ~]# gitlab-ctl reconfigure    初始化gitlab

这里写图片描述

[root@test1 ~]# gitlab-ctl restart  重载服务

这里写图片描述
在网页访问输入密码大于八位便可:
这里写图片描述
设置用户密码:
这里写图片描述
成功登录:
这里写图片描述
咱们能够建立一个新工程:
这里写图片描述
建立工程成功:
这里写图片描述
这里写图片描述
配置免密链接:github

[root@foundation38 Desktop]# systemctl restart httpd    重启httpd服务
[root@foundation38 Desktop]# cd
[root@foundation38 ~]# cd .ssh/
[root@foundation38 .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
[root@foundation38 .ssh]# cat id_rsa.pub 查看公钥
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDNeD0hcnCreyvvLHo5fMcQ4MszPyzAihqHQa9plB7Hxn+Qau4SSo8h2jJbhIDtvGfQdrCcB7823+skP6QFrfhUWBUbwZ7UjleSbumA3YhHK/ooCEzDx1kQY9pXxOEIp7jZ4PWfOhvINyFC1RH/kPC7Tx7697lq3mcWQCRS1wArF3vg3AHsNDtswwiYLitHkiZDlUVBBBnwH4GM1xe4YKtQXDOoqjnfxoq2LHFh8JKb/92NvqzYr1w+E8ps/jx+AoeUnv9pU4qtHierA3B/DQawI+wH3ChABZAjmr7zONbdINj1QJIjFmjDOgXQIc2eyYTFJb7/qaCwvm+Cwm6SVitx root@foundation38.ilt.example.com

这里写图片描述
配置gitlab的免密链接:
这里写图片描述
这里写图片描述web

[root@foundation38 .ssh]# git clone git@172.25.38.11:root/demo.git  进行克隆
Cloning into 'demo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
[root@foundation38 .ssh]# cd demo/
[root@foundation38 demo]# ls
README.md
[root@foundation38 demo]# cd .git/
[root@foundation38 .git]# ls
branches  description  hooks  info  objects      refs
config    HEAD         index  logs  packed-refs
[root@foundation38 .git]# git remote -v   
origin	git@172.25.38.11:root/demo.git (fetch)
origin	git@172.25.38.11:root/demo.git (push)
[root@foundation38 .git]# cd ..
[root@foundation38 demo]# ls
README.md

这里写图片描述

[root@foundation38 demo]# vim README.md 
[root@foundation38 demo]# cat README.md 
# demo
# helloworld

[root@foundation38 demo]# git add README.md   提交到暂存区
[root@foundation38 demo]# git commit -m "add README.md"   直接提交
[master e4b9560] add README.md
 1 file changed, 1 insertion(+)
[root@foundation38 demo]# git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@172.25.38.11:root/demo.git
   c11a0b1..e4b9560  master -> master

这里写图片描述
在gitlab界面能够看到实时数据:
这里写图片描述
从新打开一台虚拟机配置jenkins:vim

[root@localhost ~]# ls
[root@localhost ~]# route -n  查看网关
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 eth0
[root@localhost ~]# ip route add default via 172.25.38.250  添加网关
[root@localhost ~]# route -n   查看网关
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.38.250   0.0.0.0         UG    0      0        0 eth0
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 eth0
[root@localhost ~]# vim /etc/resolv.conf  配置dns
[root@localhost ~]# cat /etc/resolv.conf 
nameserver 114.114.114.114

这里写图片描述

[root@foundation38 Desktop]# iptables -t nat -I POSTROUTING -s 172.25.38.0/24 -j MASQUERADE  在真机添加策略

这里写图片描述

[root@localhost ~]# ping www.baidu.com  保证虚拟机能够ping通百度(上网便可)

这里写图片描述
jenkins的安装配置:api

[root@localhost ~]# ls
jdk-8u171-linux-x64.rpm  jenkins-2.121.1-1.1.noarch.rpm
[root@localhost ~]# yum install * -y

这里写图片描述

[root@localhost ~]# systemctl status jenkins
● jenkins.service - LSB: Jenkins Automation Server
   Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:systemd-sysv-generator(8)
[root@localhost ~]# /etc/init.d/jenkins start  开启服务
Starting jenkins (via systemctl):                          [  OK  ]

这里写图片描述

[root@localhost ~]# yum install net-tools  安装工具包
[root@localhost ~]# netstat -antlp  查看端口8080开启
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      905/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1193/master         
tcp        0      0 172.25.38.12:22         172.25.38.250:55754     ESTABLISHED 11819/sshd: root@pt 
tcp6       0      0 :::8080                 :::*                    LISTEN      12175/java          
tcp6       0      0 :::22                   :::*                    LISTEN      905/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1193/master

这里写图片描述
在网页访问jenkins:
这里写图片描述
根据提示在虚拟机获取初始化密码:浏览器

[root@localhost ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
bdb371f17f07452eb5d935d7ab6d4f16

复制粘贴上去进行下一步:
这里写图片描述
安装建议安装插件:
这里写图片描述
这里要保证全部插件安装成功,保证网络有失败的再次安装便可:
这里写图片描述
建立Admin用户:
这里写图片描述
保存完成:
这里写图片描述
开始使用jenkins:
这里写图片描述
进入jenkins界面:
这里写图片描述
这里写图片描述
在本台虚拟机安装git:服务器

[root@localhost ~]# hostnamectl set-hostname test2  更改主机名字
[root@localhost ~]# exit
logout
Connection to 172.25.38.12 closed.
[kiosk@foundation38 images]$ ssh root@172.25.38.12
root@172.25.38.12's password: 
Last login: Fri Aug 24 13:30:58 2018 from 172.25.38.250
[root@test2 ~]# yum install -y git  安装git

这里写图片描述
设置浏览器网页为中文的设置:
这里写图片描述
这里写图片描述
这里写图片描述
查看私钥:

[root@foundation38 ~]# cd /root/.ssh/
[root@foundation38 .ssh]# ls
authorized_keys  demo  id_rsa  id_rsa.pub  known_hosts
[root@foundation38 .ssh]# cat id_rsa  所有复制到网页便可

设置免密保存便可:
这里写图片描述
这里写图片描述

[root@foundation38 demo]# pwd
/home/kiosk/demo
[root@foundation38 demo]# ls
aa.txt  readme.md  test.txt
[root@foundation38 demo]# vim test.txt 
[root@foundation38 demo]# cat test.txt 
test
[root@foundation38 demo]# git add test.txt   提交信息
[root@foundation38 demo]# git commit -m "add test.txt"
[master 02cadf8] add test.txt
 1 file changed, 1 deletion(-)

这里写图片描述

[root@foundation38 demo]# git push origin master  推送信息
Username for 'https://github.com': xuefeilong
Password for 'https://xuefeilong@github.com': 
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 248 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/xuefeilong/test.git
   acbf46a..02cadf8  master -> master

这里写图片描述
能够看到有推送信息标志:
这里写图片描述
查看控制台输出能够看到提交的信息:
这里写图片描述
安装gitlab插件:
这里写图片描述
这里写图片描述
构建触发器:
这里写图片描述
这里写图片描述
这里写图片描述

[root@test2 ~]# curl -X PUT --header "PRIVATE-TOKEN: JsL4B7yUKz4Qz8W2_GMy"   'http://172.25.38.11/api/v4/application/settings?allow_local_requests_from_hooks_and_services=true'

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

[root@foundation38 demo]# vim hello.txt
[root@foundation38 demo]# cat hello.txt 
hello
hello
hello
hello
hello
[root@foundation38 demo]# git add hello.txt
[root@foundation38 demo]# git commit -m "add hello.txt"
[master 80d86bd] add hello.txt
 1 file changed, 5 insertions(+)
 create mode 100644 hello.txt
[root@foundation38 demo]# git push origin master
Username for 'https://github.com': xuefeilong
Password for 'https://xuefeilong@github.com': 
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 334 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/xuefeilong/test.git
   f11ce30..80d86bd  master -> master

这里写图片描述
在jenkins网页能够看到提交信息:
这里写图片描述