天天看点

自动化部署之jenkins自动触发构建和发布

思路图:

一、下载gitlab plugin

jenkins-->系统管理-->管理插件-->下载并安装gitlab plugin

二、配置gitlab认证

路径:Jenkins-->Credentials-->System-->Global credentials(unrestricted)-->Add Credentials

1、Kind选择Gitlab API token

2、其中API token填写gitlab中有库权限的账号

3、ID填写用户账号

gitlab生成Api token,将生成的token填入上面的证书内。

三、选择连接

路径:系统管理-->系统设置

1、填写连接名

2、填写gitlab访问URL

3、选择gitlab认证

3、测试连接

四、配置任务在job配置中选择gitlab connection:gitlab

五、任务配置

在job配置界面

2、选择push events 时间触发构建

3、选择分支过滤(此处可以根据不同的需求来使用过滤功能)

4、secret token需要填入gitlab项目中的webhook

在gitlab中找到项目-->setting-->Integrations配置

填写在上图生成的链接:

Secret Token:3f199086a22c54957579966e34ad120a

点击Add webhook

测试是否生效:点击test-->选择push event会跳转到Hook excuted successfully:HTTP 200

六、构建发布PHP项目

在linux-node1上安装nginx,初始同步了线上代码,写入一个index.html。如图:

1

2

3

4

5

6

7

<code>[root@linux-node1 ~]</code><code># ll /data/www/php-deploy/</code>

<code>total 12</code>

<code>-rw-r--r-- 1 root root 28 Dec 23 15:45 index.html</code>

<code>-rw-r--r-- 1 root root 12 Dec 21 16:11 new.html</code>

<code>-rw-r--r-- 1 root root 19 Dec 21 16:11 readme</code>

<code>[root@linux-node1 ~]</code><code># cat /data/www/php-deploy/index.html </code>

<code>&lt;h1&gt;welcome to beijing&lt;</code><code>/h1</code><code>&gt;</code>

模拟程序员在linux-node2上进行修改代码,并提交到gitlab的master分支上

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<code>[root@linux-node2 app1]</code><code># pwd</code>

<code>/root/php/app1</code>

<code>[root@linux-node2 app1]</code><code># echo "Welcome to use Jenkins and Gitlab" &gt; index.html </code>

<code>[root@linux-node2 app1]</code><code># git status</code>

<code># On branch master</code>

<code># Changes not staged for commit:</code>

<code>#   (use "git add &lt;file&gt;..." to update what will be committed)</code>

<code>#   (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)</code>

<code>#</code>

<code>#modified:   index.html</code>

<code>no changes added to commit (use </code><code>"git add"</code> <code>and</code><code>/or</code> <code>"git commit -a"</code><code>)</code>

<code>[root@linux-node2 app1]</code><code># git add .</code>

<code>[root@linux-node2 app1]</code><code># git commit -m "jenkins + gitlab"</code>

<code>[master 7313bdd] jenkins + gitlab</code>

<code> </code><code>1 </code><code>file</code> <code>changed, 1 insertion(+), 1 deletion(-)</code>

<code>[root@linux-node2 app1]</code><code># git push origin master</code>

<code>Counting objects: 5, </code><code>done</code><code>.</code>

<code>Compressing objects: 100% (2</code><code>/2</code><code>), </code><code>done</code><code>.</code>

<code>Writing objects: 100% (3</code><code>/3</code><code>), 334 bytes | 0 bytes</code><code>/s</code><code>, </code><code>done</code><code>.</code>

<code>Total 3 (delta 0), reused 0 (delta 0)</code>

<code>To [email protected]:java</code><code>/app1</code><code>.git</code>

<code>   </code><code>dd37af6..7313bdd  master -&gt; master</code>

到此,自动触发和发布就完成了!这只是一个简单的构建发布,作为内部测试使用还是可以的!生产使用,有待优化!

本文转自 IT_外卖小哥  51CTO博客,原文链接:http://blog.51cto.com/jinlong/2053863