GitLab + Jenkins achieve continuous integration + Automated Deployment

I. Introduction substantially automated on-line codes

1. Software Development Life Cycle

Creative product manager boss --- --- --- project development team --- test team (black and white box) --- line maintenance operation on
product manager --- --- plus demand --- development team test team - - operation and maintenance update the code on line

2. What is the environment?

Development Environment: develop their own local computer with installed php python's own development tools.
Test environment: multi-personal code together to run again, run a test mysql redis so on.
Pre-release environment: direct connection to online databases, within the domain of specialized test small-scale tests.
Production: Each line item environment of an environmental (lower lines may be put together with a multi-domain not online).

3. What is continuous integration (CI)?

We have carried out once a day code merger is continuous integration.
Multiple developers to write managed code together into the code library (version control management software) code library tool: svn git (and more). (Version control system: a mechanism similar snapshot incremental backup, that is, every version of the update will preserve the contents of the change in the basis of the original, create a new version, the previous version will be retained for quick return roll.)
have to do every day merge codes, conflict prevention, in the time of the merger will conflict, so there is time to submit the order, that is, you first upload, and then I uploaded you pull over and I are combined, excluding conflict then I upload to the main branch. Continuous Integration is a software development practice, that members of the development team integrate their work frequently, usually each member at least once integrated, which means integration may occur several times a day. Each integration automated build (including the compilation, publishing, automated testing) to verify that all through, so find integration errors as quickly as possible. Many teams find that this process can significantly reduce integration issues, so that the team can more quickly develop cohesive software.

4. What is the continuous delivery?

First, the development unit test --- finally put to test all the code --- code quality checks together.

5. What is Continuous Deployment?

Line is the continued deployment frequent code.

6. No continuous integration

Module integration projects to do, we found a lot of the interface does not pass ==> waste a lot of time
needed to compile a manual packaged latest code ==> build process opaque
release code, on-line, basically by hand ==> Script flying

7. Continuous Integration Best Practices

Maintain a single code base
makes building automation
to perform the test is part of the build
integrated log and history
to use a unified management library dependencies
integrated at least once a day

Two .jenkins Basics

Note: Refer to the documentation on how to install Jenkins: https://www.jianshu.com/p/343bdd43c82a
to install omitted here.

1. Log Jenkins webUI interface to create a project

2. Enter a project name (free-job) and choose to build a free-style software project

After configuration created above 3. come jump, choose to drop the old constructs (below generally can be kept for days at 5 to 7 days)

4. Then select the above constructed, and select to execute shell commands Execute Shell

5. Since it can execute shell commands, then we perform a pwd, where to look in the default working directory

6. Click Save to build immediately after the above, the following will generate a build history, (a blue that means normal, red means that if there is a problem)

7. built, we can click on the build ID drop-down box to select the Console Output View Details

8. Adoption of output we can see Jenkins default working directory in /root/.jenkins/workspace/ (project name)

9. Since it can execute shell commands, then we try to create a file; return to the table, click Configure

10. Create a file and save free.txt

11. The same as above, we clicked immediately build

12. After the console output to see the same building, you can see build success

13. Since the build is successful, then we enter the server to verify that create success
[root@jenkins ~]# cd /var/lib/jenkins/workspace/free-job/
[root@jenkins /var/lib/jenkins/workspace/free-job]# ls
free.txt

三.GitLab+Jenkins自动化部署及更新

流程图如下:

说明:通过gitlab+Jenkins实现代码的自动更新同步代码到web服务器站点目录。此处示例后端web服务器使用nginx。
环境规划以及ip分配如下表:

注意:开发环境这里直接放在gitlab服务器上这里将git也部署在gitlab服务器上。git基本使用请参考:https://www.jianshu.com/p/96c4a6d0c587

3.1gitlab 配置

说明:首先在gitlab上面创建一个群组,并创建一个项目,将代码push 到代码仓库中(项目)。创建一个开发用户(用于提交代码等),同时在Jenkins服务器上面生成ssh秘钥并将key添加到新建用户的ssh认证下面。

1.登录gitlab点击项目,然后点击创建一个群组

2.点击新建群组

3.输入新建群组的相关信息并点击创建



4.新建一个项目



5.设置秘钥进行
[root@gitlab ~]# ssh-keygen -t rsa
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:WyI0C3MBpUdsAKQsDkEf99WmtCUvcXUoxWL+NgF2R0I root@gitlab
The key's randomart image is:
+---[RSA 2048]----+
|ooooo*+  .. =Eoo |
|.o. oo+..+ @ +o. |
|+. .+.=.. & = .  |
|+    * o + o .   |
| .    o S o . .  |
|       . +   +   |
|        .   . .  |
|                 |
|                 |
+----[SHA256]-----+
[root@gitlab ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDnnyT5IRLN2AzknCPE3pLnLhH0Vq6jGJsYsjDaLKBQNkBD4coTMW47PJpjxL/pjwyV9KaQ3BuEqam8jQlpaDYUQVBK++0ALFAObGTyTPThRVKhW7X3kjSkInkwAZVj2dLdU0oIinzEuCjldhOe3BF2FYHRgcbnklEFIVpK3quTJbKxcF4h8SMbihRbXor0AIsOupWd0z2skqqvr4xSDNPAb4t+Ypd0MnGPRJYb8DTs+Ign+gn6L7a48RWqqIhBjVjgDLHiitMeEKA6FCS4cEHvcbHhXtrLswfF2ocA+mjfb5WGfP73d959fSPfvraRhhxXqBMqM309DmO3DGtS2e1j root@gitlab
6.塞key操作,方便源码上传下载

7.点击头像查看创建项目的结果


8.查看项目根据提示配置初始化


9.模拟开发建立git本地工作目录和本地仓库
#这里的源码使用的码云上源码,你可以自行下载使用即可。
[root@gitlab ~/yiliaoqixie]# git config --global user.name "Administrator"
[root@gitlab ~/yiliaoqixie]# git config --global user.email "[email protected]"
[root@gitlab ~/yiliaoqixie]# git init
Initialized empty Git repository in /root/yiliaoqixie/.git/
[root@gitlab ~/yiliaoqixie]# git add .
[root@gitlab ~/yiliaoqixie]# git commit -m "第一次提交项目"
[root@gitlab ~/yiliaoqixie]# git remote add origin [email protected]:yiliao/yiliao.git
[root@gitlab ~/yiliaoqixie]# git push -u origin --all
10.查看上传的结果

11.点击设置选择新建用户

12.输入账号相关信息并点击创建用户


13.点击编辑,为上面新创建的用户设置密码

14.给这个账号设置密码并保存


15.将上面创建的用户添加到devops组中并给与开发者权限

16.找到刚刚创建的dev用户并给予开发人员权限,然后增加用户到群组

3.2Jenkins配置

1.在Jenkins的服务器上生成秘钥
[root@jenkins ~]# ssh-keygen -t rsa
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:Y/nVt2+ldZc+h4VrsormDhSXKj3ryP/LlLkpIU95xjA root@jenkins
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|         .       |
|      . o        |
|     .E+ .   .   |
|    . ==S   . ..o|
|    .o+++= .  .oB|
|     +o++ .   .B+|
|   . oo+.+  . =o+|
|    o.o*X....+ .+|
+----[SHA256]-----+
[root@jenkins ~]# cat ./.ssh/id_rsa
id_rsa      id_rsa.pub  
[root@jenkins ~]# cat ./.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCssc5KGRIvHV64Md3HR3SdT0w4twdrAIbxWSSo3tvfrauMdSk2ptmxvgzPqfGEQUgHfPsZY10eN0bpvaFARLBDytyRLbgdSo6gts+J0nGhy0hcjOhu4xJgygt8hN53UCUbdy7DhUnwxIfOuzENFYLS7ZW/Uk4N1FrNNuZK0X5aVNsV5r9H+fUeOfMJ4YLjLhFn6C94q4XmS9OfAW0aCSUfBbCT9LnRmeU/FoQMGdujBZ+Koc9289IVNHkVL+vEW6ZTxLPMEoRaB0RlLxfZF86LMsjsqx6ABBHNpV5FZWl0OYnnEn3jKHRNStfxx2g5Hz9e7eRguBubBis75OfHf2br root@jenkins
2.回到gitlab服务器上面使用maliu用户登录,然后点击头像选择设置

3.上面的设置点进来后,选择SSH 秘钥, 然后复制Jenkins服务器上生成的秘钥然后点击增加秘钥



4.新建任务


5.点击项目名称开始配置

6.选择源码管理,选Git,输入仓库URL地址(如果下方出现红色及表示出错了),然后选择保存

7.立即构建

8.查看构建信息

9.服务端查看
[root@jenkins /var/lib/jenkins/workspace/yiliao]# ls
about.html           article.html                         contact.html  index.html           product.html
album.html           Bootstrap医疗器械公司响应式网站模板  css           js
article_detail.html  comment.html                         images        product_detail.html

3.3实现自动部署

上面已经完成了Jenkins从gitlab上拉取代码,现在需要实现如何将源码自动部署到web服务器上,这里使用脚本的方式来实现,Jenkins支持执行脚本的功能。

1.在Jenkins服务器上面编写同步脚本,由于是通过脚本拷贝到web服务器的站点目录,所以需要先做一个ssh秘钥认证
[root@jenkins ~]# ssh-copy-id -i 10.0.0.7
[root@jenkins ~]# ssh-copy-id -i 10.0.0.8
[root@jenkins /server/scripts]# vim deploy.sh 
#项目目录
CODE_DIR=/var/lib/jenkins/workspace/yiliao/
#时间格式
DATE_TIME=`date +%Y-%m-%d-%H-%M-%S`
#打包后的名字
TAR_NAME=web-${DATE_TIME}.tar.gz
#web服务器站点目录的上一级 "/usr/share/nginx/html"
WEB_DIR=/usr/share/nginx/
#web服务器新建的站点目录名字
WEB_NEWDIR_NAME=web-${DATE_TIME}
#进入到项目目录上一级打包代码
cd $CODE_DIR && tar czf /opt/$TAR_NAME ./*
for i in {7,8}
do
#拷贝到web服务器的站点目录的上一级
    scp /opt/$TAR_NAME 10.0.0.${i}:$WEB_DIR
#连接web服务器进行解压压缩包到新的一个已时间命名的站点目录
    ssh 10.0.0.$i "cd $WEB_DIR && mkdir $WEB_NEWDIR_NAME && tar xf $TAR_NAME -C $WEB_NEWDIR_NAME"
#将新建的站点目录与html站点目录做一个软链接
    ssh 10.0.0.$i "cd $WEB_DIR && rm -rf html && ln -s $WEB_NEWDIR_NAME html"
    ssh 10.0.0.$i "cd $WEB_DIR && rm -rf $TAR_NAME"
    sleep 3
done
2.配置Jenkins,使用Jenkins调用部署脚本(此处写脚本全路径脚本名称)

3.立即构建

4.查看输出结果

5.测试访问结果

3.4自动更新

1.配置自动触发构建、需要设置安全令牌Secret token,进入项目选择配置,设置相关信息,然后生成token,复制token(需要填写到gitlab上面)和Build when a chang上面提示的URL地址( http://10.0.0.57:8080/project/yiliao)

选择构建触发器,然后选择build when a change is ....,下方有个高级选项后然后选择Filter branches byname,然后输入master分支,再点击下面的Generate生成token,然后点击保存。

2.配置gitlab,添加token

点击项目→设置→设置→集成,然后输入Jenkins上面提示的URL地址和和生成的token,点击增加web钩子。

3.测试Web钩子

添加完钩子完成后可以测试,选择Push event,执行完会显示上面的提示。

4.上面已经配置完成,接下来就是测试,我们知道目前web服务站点访问得到结果如上面, 这里gitlab服务器下载的代码并进行修改,然后推送gitlab仓库。这里去除首页的右上角的English版本。

修改首页内容:

5.推送新的修改源码
[root@gitlab ~/yiliaoqixie]# git add .
[root@gitlab ~/yiliaoqixie]# git commit -m "第二次提交"
[master 4b8d377] 第二次提交
[root@gitlab ~/yiliaoqixie]# git push -u origin --all
6.测试访问

之前的结果如下图:

现在的结果:

3.5配置构建Jenkins返回构建状态gitlab

1.现在gotlab上面生成访问令牌token,点击用户处的设置

2.访问令牌——>输入名称——>选择范围为api——>创建个人访问令牌,将生成的令牌token复制

3.登录Jenkins——>选择系统管理——>选择系统设置——>选择Gitlab——输入名字——>输入URL——点击Add

4.上面点击Add进来后,kind处选择Gitlab API token,粘贴上面复制的token,自定义ID和描述

5.上面添加后,回到这里选择刚刚生成的这个token。然后点击保存

6.上面保存后,进入到项目里选择配置,配置构建后操作,选择Publish ... to ...Gitlab,选择后直接保存就ok

7.测试,点击立即构建

8.回到gitlab上面查看,那里生成了一个对号

9.点击对号,进来后可以看到流水线,下面一些信息,比如状态,提交等。

10.点击上面Status下面的对号,可以看到如下图一样的有个jenkins-success

11.再点击jenkins就跳转到jenkins上面去了,也可以查看输出,还可以查看工作空间,可以查看到所有代码,返回的话只能选择后退。



Guess you like

Origin www.cnblogs.com/hujuntao/p/11984687.html