惯例,介绍下 ansiblejenkinsgitlab

ansibleAnsible 简单的说是一个配置管理系统(configuration management system)。你只需要可以使用 ssh 访问你的服务器或设备就行。它也不同于其他工具,因为它使用推送的方式,而不是像 puppet 等那样使用拉取安装 agent 的方式。你可以将代码部署到任意数量的服务器上!

jenkinsJenkins 是一个开源的、可扩展的持续集成、交付、部署(软件/代码的编译、打包、部署)的基于 web 界面的平台。允许持续集成和持续交付项目,无论用的是什么平台,可以处理任何类型的构建或持续集成。

gitlabGitLab 是一个基于 Git 实现的在线代码仓库托管软件,你可以用 gitlab 自己搭建一个类似于 Github 一样的系统,一般用于在企业、学校等内部网络搭建 git 私服。


准备工作

准备 4 台机器并且为了方便,把防火墙和 SElinux 关闭停用(实际环境看需求开放端口)

ansible
hostnamedev-of-runfa-20
IP10.18.193.20

jenkins
hostnamedev-of-runfa-25
IP10.18.193.25

gitlab
hostnamedev-of-runfa-26
IP10.18.193.26

web
hostnamedev-of-runfa-27
IP10.18.193.27


部署 ansiblejenkinsgitlab 服务器

ansible

[root@dev-of-runfa-20 ~]# yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@dev-of-runfa-20 ~]# yum -y install ansible
[root@dev-of-runfa-20 ~]# ansible --version
ansible 2.8.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr  9 2019, 14:30:50) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

jenkins

[root@dev-of-runfa-25 ~]# curl https://download.docker.com/linux/centos/docker-ce.repo -o  /etc/yum.repos.d/docker.repo
[root@dev-of-runfa-25 ~]# yum -y install docker-ce.x86_64
[root@dev-of-runfa-25 ~]# systemctl start docker.service
[root@dev-of-runfa-25 ~]# systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@dev-of-runfa-25 ~]# mkdir -p /data/jenkins_home
[root@dev-of-runfa-25 ~]# chmod -R 777 /data/jenkins_home
[root@dev-of-runfa-25 ~]# docker run -itd --name jenkins --hostname jenkins -v /data/jenkins_home:/var/jenkins_home -v /etc/localtime:/etc/localtime:ro -p 8080:8080 -p 50000:50000 jenkins/jenkins:latest
[root@dev-of-runfa-25 ~]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                              NAMES
8ea0f7f2a521        jenkins/jenkins:latest   "/sbin/tini -- /usr/…"   5 seconds ago       Up 4 seconds        0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp   jenkins

gitlab

[root@dev-of-runfa-26 ~]# vim /etc/yum.repos.d/gitlab.repo
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
[root@dev-of-runfa-26 ~]# yum install -y gitlab-ce
[root@dev-of-runfa-26 ~]# gitlab-ctl reconfigure
[root@dev-of-runfa-26 ~]# gitlab-ctl start
ok: run: alertmanager: (pid 9205) 23s
ok: run: gitaly: (pid 9031) 27s
ok: run: gitlab-monitor: (pid 9089) 26s
ok: run: gitlab-workhorse: (pid 9061) 27s
ok: run: grafana: (pid 9230) 22s
ok: run: logrotate: (pid 8576) 123s
ok: run: nginx: (pid 8553) 129s
ok: run: node-exporter: (pid 9081) 26s
ok: run: postgres-exporter: (pid 9220) 23s
ok: run: postgresql: (pid 8319) 203s
ok: run: prometheus: (pid 9105) 25s
ok: run: redis: (pid 8158) 215s
ok: run: redis-exporter: (pid 9093) 25s
ok: run: sidekiq: (pid 8501) 141s
ok: run: unicorn: (pid 8473) 147s

配置 gitlabjenkins 服务器,模拟真实环境

gitlab 相关

先在 gitlab 上创建项目,然后在 windows 主机上(存放源码)安装 windows git 客户端

90134-ikm8lcexcg8.png

25449-n27z5e5vvt.png

15732-ijt9seut4zd.png

75102-j8qjo80v8es.png

87375-8i9ehg50r1j.png

17518-duzer0cbo5l.png

28109-cyl042g49op.png

使用 git 客户端把源码上传到 gitlab 仓库,git push 的时候提示输入用户名和密码,输入 gitlab 的登录用户的信息

Admin@LAPTOP-JB08JEOV MINGW64 ~
$ cd /d/CF/website/

Admin@LAPTOP-JB08JEOV MINGW64 /d/CF/website
$ git config --global user.name "Administrator"

Admin@LAPTOP-JB08JEOV MINGW64 /d/CF/website
$ git config --global user.email "[email protected]"

Admin@LAPTOP-JB08JEOV MINGW64 /d/CF/website
$ git init
Initialized empty Git repository in D:/CF/website/.git/

Admin@LAPTOP-JB08JEOV MINGW64 /d/CF/website (master)
$ git remote add origin http://10.18.193.26/root/website.git

Admin@LAPTOP-JB08JEOV MINGW64 /d/CF/website (master)
$ git add .

Admin@LAPTOP-JB08JEOV MINGW64 /d/CF/website (master)
$ git commit -m "One commit"

Admin@LAPTOP-JB08JEOV MINGW64 /d/CF/website (master)
$ git push -u origin master
Enumerating objects: 202, done.
Counting objects: 100% (202/202), done.
Delta compression using up to 4 threads
Compressing objects: 100% (198/198), done.
Writing objects: 100% (202/202), 12.01 MiB | 7.74 MiB/s, done.
Total 202 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), done.
To http://10.18.193.26/root/website.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

查看 gitlab 是否有你所上传的项目

82381-txnj5zv5yw.png

jenkins 相关

打开 jenkins web 页面并做一些配置

20170-cdhfujmhv4h.png

03896-wt35aioyufg.png

19467-b0eb6c2gsep.png

57826-nle1jjfm2c8.png

77335-paigyhp0v9c.png

16945-6cgstvjmuph.png

23509-5qwszwr8nme.png

安装一些前端项目需要用到的插件(具体需求具体分析,我这里的不一定通用)
并且针对项目需求做一些配置,我这里安装 gitlabgitlab hooknodejsSSHPublish Over SSH 五个插件

29689-da3bf04egyf.png

30273-ncor755icx.png

93227-xpdzqsx9kgr.png

93775-h0bsydu94o6.png

34490-af2yc8jawn5.png

49062-2nb6rl4yv1d.png

52018-gaswxww9ykv.png

87069-p9vj4b1oo2h.png

jenkinsgitlab 串联起来并尝试构建

首先在 jenkins 上生成私钥和公钥

[root@dev-of-runfa-25 ~]# 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:
SHA256:4Rql+pHx9WmNPXyU9blYevCK8/xmwDThgk8+USo+aUA root@dev-of-runfa-25
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|       E     o   |
|      . o . + . .|
|       = + = +  =|
|      + S B =..+o|
|     . * * = O*..|
|    . + o . *o*+.|
|     . .   oo o= |
|      .    .o++. |
+----[SHA256]-----+
[root@dev-of-runfa-25 ~]# cat .ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAwm0l/UzoOGlpIK1cz8+PKEJGILmV5FJWLYwH/Kbe7c5GGS0d
lXLcAl7TngjV9UNSkCDXh792ThCr5hMZyL5YAD0dJ8fRLWmWwyO8neda9I+XndQY
ktgW9TCPtrjquhtFMbL6gwvAua6jzmk2wQzNNUHfkQI+eAk0Fe1XYKLQ1Z/Chk6Z
okfwuZ762zxpmyTyMJBDNibEMBo7PcqLfEuw37hTEkSCGDVi01Im4b9nCl3TK6ZR
IqviQaxrOIyrOlQqhQCSBbtP8T7O1ZmWA1sMdwehxUQyj9+J/x6hErf8DOSmamM2
f4EKxA0Syfcbl/Oz1Eg1exctxmZrI9q8CEtfcQIDAQABAoIBAQCXkqgPNSnE8Mi4
ZO76Q4IDjMdHM+ebPGVa1NRVL3KUSj9z2Sw7GnRSlMK9bX3/2yA+pJvihG0ipvSc
RT/QtDO+2+Yi4TA+CxcxwmaFY5wRFPDKZlNSB4y+UcF4XekAdcoX2MRb7YmuOGwT
uw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MQ==
-----END RSA PRIVATE KEY-----
[root@dev-of-runfa-25 ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDCbSX9TOg4aWkgrVzPz48oQkYguZXkUlYtjAf8pt7tzkYZLR2VctwCXtOeCNX1Q1KQINeHv3ZOEKvmExnIvlgAPR0nx9EtaZbDI7yd51r0j5ed1BiS2Bb1MI+2uOq6G0UxsvqDC8C5rqPOaTbBDM01Qd+RAj54CTQV7VdgotDVn8KGTpmiR/C5nvrbPGmbJPIwkEM2JsQwGjs9yot8S7DfuFMSRIIYNWLTUibhv2cKXdMrplEiq+JBrGs4jKs6VCqFAJIFu0/xPs7VmZYDWwx3B6HFRDKP34n/HqESt/wM5KZqYzZ/gQrEDRLJ9xuX87PUSDV7Fy3GZmsj2rwIS19x root@dev-of-runfa-25

把私钥放到 jenkins 上,把公钥放到 gitlab 上,使 jenkins 能从 gitlab 拉取代码构建

10464-iuodjoih64.png

86438-wpopcudn89.png

51981-xswbkqq9kh.png

47049-lvab4q1gxl.png

44562-e1ewmkybbo7.png

68881-77735nw96h8.png

51009-ebe1ds715h.png

回到 jenkins,新建项目

40308-qvh286k3srs.png

51469-1128xplaza3.png

02972-2yvdfmmbgw8.png

97703-ss80m612ru.png

66721-ghy5hoxzbdv.png

96223-atwhwp68cql.png

79268-ag8g2ywf5wb.png

测试是否能正常构建,构建完成最后又 SUCCESS 即为构建成功(最后一张图比较大,可以另开页面放大看下)
我这里一开始因为 npm installnpm run build 后面有个空格导致构建失败,所以调试了好几次才成功,能看到调试了 7
所以大家有什么问题要自己看下

88722-5473sdazjxw.png

70619-1or6olostgbh.png

84310-uulr85xf1hd.png

65950-789f325qalw.png


这篇文章结束了

到这里,已经把 jenkinsgitlab 串联起来了
因为篇幅所限,下一篇文章将介绍如何使用 gitlab 钩子触发自动构建,以及 jenkinsansible 协同部署到服务器


文章作者: Runfa Li
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Linux 小白鼠
Linux Linux ansible jenkins gitlab 手动回滚 自动化发布
觉得文章不错,打赏一点吧,1分也是爱~
打赏
微信 微信
支付宝 支付宝