jenkins Continuous Integration - How do I see the code automatically deployed from 1 to

jenkins continuous integration of code to see how my automated deployment from 1

background

Recently because of work needs to learn jenkins persistence integration. For a studious and handsome me. He is not Shoudaoqinlai learning. The company is a small and medium sized start-up companies in the deployment of the code above, rarely used gitlab, jenkins, etc., there is no clear production environment - Testing environment - line environment of the points. The only some were written code - login service shut down the service, upload code - open service. That will tend to come a lot of problems, but also bring a lot of tedious work to programmers. This does not, within the company reorganization, needed a clear process, and in order to reduce the burden on the programmer, so they want to adopt gitlab + jenkins to deploy code.

demand

By programmers submit code to the gitlab; jenkins trigger automatic deployment trigger; deployed to the test server, if normal, formally submitted to the online environment

Environment Introduction

Ubuntu18.04 :

          网络:内网环境
          服务:gitlab环境

Ubuntu16.04:

          网络:内网环境
          服务:jenkins

CentOS 7:

          网路:公网环境
          服务:php+nginx+mysql

A public network environment for testing environment, there is no online environment, the environment can be a primer, the line successfully tested the same way truth.

Deployment Process

Why deploy from 1 to automate it? Because I do not want to write the deployment process installation. Process is very simple, no pit and difficult. Baidu, a little, a lot.

Prerequisites:
1, you need to have your own account and gitlab own projects, of course, use someone else's project also, but set some permissions information in the back, it may be troublesome to others. So his is the most convenient.
2, your public server must be able to access the network gitlab project, because in order to reduce errors, using the public network server pull gitlab code. Specifically we talked back.
3, not afraid to have a hard heart, and handsome face.

First, install the relevant plug-ins

Click System Management - management plug-ins, you can download the search box you want to insert. This just in time initialization steps jenkins also has a plug-in installed, try to install, straining equipment. According to their needs Ha!

Second, create a job on jenkins

1, the new task
jenkins Continuous Integration - How do I see the code automatically deployed from 1 to
2, enter the task name - Select - Select line - to determine
jenkins Continuous Integration - How do I see the code automatically deployed from 1 to
a brief overview of the advantages and disadvantages of these projects

Freestyle Job
need to add the module to complete the configuration items and parameters in the configuration page
for each Job can only achieve a function
can not be coded, is not conducive to migration and version control

Pipeline project
all parameters can be embodied as a pipeline script
can define multiple stage pipeline to build a working set of
configuration code technology, easy version control and configuration migration Job
script written in the Jenkins project in

Multi-branch pipeline
advantage with line
scripts written in GitLab project in (Jenkinsfile)

About multi-branch lines, it is recommended https://blog.51cto.com/12639039/2352222 blog

3, into the job configuration interface - click to build triggers
General need not be configured
as a programmer needs to submit a new code to gitlab, jenkins trigger. So choose the following options when building triggers

jenkins Continuous Integration - How do I see the code automatically deployed from 1 to

Please do not ignore this Gitlab webhook URL :. This webhook jenkins is used to trigger automatic build.

Click Advanced, create Secret token

jenkins Continuous Integration - How do I see the code automatically deployed from 1 to
4, webhook gitlab 3 on the addition step
jenkins Continuous Integration - How do I see the code automatically deployed from 1 to
to fill in the url of the webhook; Secret Token fill at Step three randomly generated string. Been completed, you can click Add.
jenkins Continuous Integration - How do I see the code automatically deployed from 1 to

在这里可以进行测试连通性。
注意到这里的时候,你可能会遇到一个问题:有些用户添加的时候会报这样一个错误,
jenkins Continuous Integration - How do I see the code automatically deployed from 1 to
说是不允许本地网络请求。这是由于新版本安全性的问题造成的,非常容易解决!
解决方法:使用gitlab管理员账户登陆。
jenkins Continuous Integration - How do I see the code automatically deployed from 1 to
将那两个全部勾选。然后回去重新添加即可。
5、编写pipeline脚本
jenkins Continuous Integration - How do I see the code automatically deployed from 1 to
这里有两个选择,第一个是在这里直接写入脚本(温馨提示,脚本在自己电脑上的编辑器上写好粘贴到这里,因为这里的编辑器像吃了翔一样难用!太难了。。。)第二个是使用jenkinsfile文件。我使用了第一个(因为演示操作简单,易懂!嘿嘿)。
写完后点击保存。即可完成一个job的创建

你以为这就完了。最重要的才刚刚开始!!!!!!!!!!!!!!!!!!!!!!!!!!!


三、pipeline script编写

让我们再来回顾一下需求:

程序员通过提交代码到gitlab上;出发jenkins自动部署触发器;部署到测试服务器,如果正常,提交到正式线上环境。不过,我觉得这个没有任何挑战性。我想自己加点难度,不管部署过程是否成功,都要有个钉钉消息发到程序员小哥哥的群里,给他们个警示!

话不多说,上代码一点点解释:
温馨提示:这是我为了满足自身需要而编写的代码,请不要照搬,当然与我有同样需求的随意喽。同时中间的解释也根据我的代码去解释,没有刻意去讲解语法,请谅解!

pipeline{
    agent any
    stages{
        stage("拉去代码"){
            steps {
                echo "STEP 1 :clone code"
            }

        }
        stage("打包代码"){
            steps {
                echo "step 2 : code package"
                sh label: '', script: '/usr/bin/ssh -p 62322 root@*.*.*.* "cd /var/www/html/pipeline/mytest && git pull && chmod -R 777 /var/www/html/pipeline/mytest/storage && composer install"'
            }
        }
        stage("上线发布"){
            steps {
                echo "step 3 :deploy package"
            }
        }
    }
    post {
        success {
            dingTalk accessToken:'钉钉机器人的token', 
            imageUrl:'图片的url', 
            jenkinsUrl:'http://192.168.5.194:8080/', 
            message:'pipeline-test代码部署成功。', 
            notifyPeople:''
        }
        failure {
            dingTalk accessToken:'钉钉机器人的token', 
            imageUrl:'图片的url', 
            jenkinsUrl:'http://192.168.5.194:8080/', 
          message:'pipeline-test代码部署失败'。, 
          notifyPeople:''
        }
    }

}

详解:

agent

          指示 Jenkins 为整个流水线分配一个执行器(在 Jenkins 环境中的任何可用代理/节点上)和工作区。

echo

          写一个简单的字符串到控制台输出。注意这里不是shell命令行的echo或php语法。和他们作用相同而已。

stage

          定义了在整个流水线的执行任务的概念性地不同的的子集(比如 "Build", "Test" 和 "Deploy" 阶段), 它被许多插件用于可视化 或Jenkins流水线目前的 状态/进展.

可能这句话不太形象(我第一次看官文也是蒙蔽),来张图
jenkins Continuous Integration - How do I see the code automatically deployed from 1 to

其中最后一个是post处理的状态。

在打包代码的stage块中

sh label: '', script: '/usr/bin/ssh  root@*.*.*.* "cd /var/www/html/pipeline/mytest && git pull && chmod -R 777 /var/www/html/pipeline/mytest/storage && composer install"'

这是通过jenkins的片段生成器生成的符合语法的命令,可以在shell中去执行的命令
那么,如何使用jenkins片段生成器?
(1)、点击流水线语法
jenkins Continuous Integration - How do I see the code automatically deployed from 1 to

(2)、从实例步骤中选择sh:shell script。在文本框输入需要生成的shell命令,
jenkins Continuous Integration - How do I see the code automatically deployed from 1 to
(3)、点击生成流水线脚本 按钮即可成成相应的流水线语法

Post

类似于python中try语句。如何根据stage执行的结果而进行特定处理则是实际Pipeline使用中经常会碰到的问题。所以这里post就是来做对异常处理的功能。同时,你也可以理解为自由风格中的构建后的操作步骤(在自由风格中发钉钉可以下载dingding的插件)。而这个post块,就是我要满足自己加的钉钉反馈的需求。说到这里为了让大家更明白post的使用方法想再多解释一下:

使用限制:

需要写在pipeline或者stage块中
注意:post块的位置一定要遵循这个原则,否则不会执行。

支持的条件预置:

always: 无论pipeline或者stage的执行结果如何,此块中的预置操作都会执行。

changed:只有当pipeline或者stage的执行后,当前状态与之前发生了改变时,此块中的预置操作才会执行。

fixed:前一次运行为不稳定状态或者失败状态,而且本次运行成功结束,这两个条件同时满足时,此块中的预置操作才会执行。

regression: 本次运行状态为不稳定状态,失败状态或者是中止状态,而且前一次运行成功结束,这两个条件同时满足时,此块中的预置操作才会执行。

aborted:当前pipeline或者stage的状态为aborted时,此块中的预置操作才会执行。通常是由于流水线被手工中会导致此状态产生,而产生此状态后,通常在Jenkins的UI界面会显示为灰色。

failure:当前pipeline或者stage的状态为failed时,此块中的预置操作才会执行。而产生此状态后,通常在Jenkins的UI界面会显示为红色。

success:当前pipeline或者stage的状态为success时,此块中的预置操作才会执行。而产生此状态后,通常在Jenkins的UI界面会显示为绿色。

unstable: 当前pipeline或者stage的状态为unstable时,此块中的预置操作才会执行。通常情况下测试失败或者代码规约的违反都会导致此状态产生,而产生此状态后,通常在Jenkins的UI界面会显示为黄色。

unsuccessful:当前pipeline或者stage的状态不是success时,此块中的预置操作才会执行。

cleanup:无论pipeline或者stage的状态为何种状态,在post中的其他的条件预置操作执行之后,此块中的预置操作就会执行。

我用到了两个,分别是success和failure,在成功时应该做什么操作,在失败时应该做什么操作。

dingTalkaccessToken:'钉钉机器人的token', 如果不知道如何添加钉钉机器人,去隔壁百度即可
imageUrl:'在发送的信息里会附加这个图片',
jenkinsUrl:'自己jenkins的访问地址', 发送的信息就是这个链接,可以直接跳转到我们的jenkins
message:'发送的文本信息、提示信息',
notifyPeople:'需要通知的人'

I'm here about the situation of the script also finished presentation. If you want more detailed jenkins grammar, recommended study addresses
https://jenkins.io/zh/doc/book/pipeline/ Chinese, and very detailed. I am here to write the script just to satisfy my personal needs, with similar needs partners can refer to!

One more reminder: beginners must pay attention to writing good code block, as far as possible standardized, so some kind of easy troubleshooting. Groovy syntax most likely because of a block of code braces will be able to find a half-day. So try to specification. If there is Groovy syntax highlighting editor for the better.

Fourth, the test operation of the job
clicking building immediately
jenkins Continuous Integration - How do I see the code automatically deployed from 1 to

After finished, you can see the whole process through the console output

jenkins Continuous Integration - How do I see the code automatically deployed from 1 to
Nail news
jenkins Continuous Integration - How do I see the code automatically deployed from 1 to

This will be done, let your little brother programmers to push the code to try it!

Guess you like

Origin blog.51cto.com/12332766/2460393