广告

用Jenkins实现Docker容器持续集成与自动部署

回到Jenkins欢迎页面,点击新建:
在新建任务页面,输入任务名,选择构建一个”自由风格的软件项目”,点击OK。

1.填写项目名称
2.配置Git
Repository URL字段填写git远程仓库地址,以码云为例,你可以从git代码托管平台上找到:

到这里,我们来测试一下,可以点击“保存”按钮回到Jenkins欢迎页面,点击“立即新建”,
等待构建成功之后,选择“工作空间”,发现user-service文件夹下多出了个build文件夹,里面有构建后的文件,在libs文件夹下,可以找到构建后的工程文件user-server-0.0.1-SNAPSHOT.jar。说明构建成功了:
3.准备Dockerfile和远程构建服务器目录结构
 用putty或SSH链接远程服务器,建立部署目录结构如下:
编写Dockerfile,文件内容如下:
FROM java:8
VOLUME /tmp
ADD user-server-0.0.1-SNAPSHOT.jar user-server.jar
RUN bash -c ‘touch /user-server.jar’
EXPOSE 9090
ENTRYPOINT [“java”,”-Djava.security.egd=file:/dev/./urandom”,”-jar”,”/user-server.jar”]
然后把Dockerfile放置到目录/root/deploy/service/user-server下面。
注意:这里特别需要注意的一点是Dockerfile里面的ADD user-server-0.0.1-SNAPSHOT.jar user-server.jar中的user-server必须和Jenkins里配置的项目名称一样,否则Docker运行会有问题的,下图右边红圈里的名字不是源码的项目名,而是jenkins中配置的项目名:
4.配置构建触发器,Generic Webhook Trigger插件的目的是实现当本地代码提交到Git服务器时,就调用Jenkins开始自动构建,如果看不到“Generic Webhook Trigger”选项,请先安装Generic Webhook Trigger插件后再试:
传送门:
配置构建触发器的钩子功能以实现当用户当提交本地代码到Git服务器时,就开始触发构建,实现自动部署,配置如下图():

5.配置Gradle构建:
6.配置构建后的操作,目的是将编译后的build/libs/user-server-0.0.1-SNAPSHOT.jar文件推送到远程服务器的/root/deploy/service/user-server下,然后停止并删除正在运行的user-server容器并且删除user-server镜像,执行Docker命令重新构建user-server镜像,并以Docker容器的方式启动user-server-0.0.1-SNAPSHOT.jar
关于第5步的自动化部署执行脚本的下载地址:
到这里,我们再来测试一下,可以点击“保存”按钮回到Jenkins欢迎页面,点击“立即新建”,
Docker构建成功后,点击“Build History”下的构建标签“#2”,然后选择“ Console Output”,我们看到Docker成功构建并运行镜像日志:
Step 1/6 : FROM java:8
8: Pulling from library/java
5040bd298390: Pulling fs layer
fce5728aad85: Pulling fs layer
76610ec20bf5: Pulling fs layer
60170fec2151: Pulling fs layer
e98f73de8f0d: Pulling fs layer
11f7af24ed9c: Pulling fs layer
49e2d6393f32: Pulling fs layer
bb9cdec9c7f3: Pulling fs layer
60170fec2151: Waiting
e98f73de8f0d: Waiting
11f7af24ed9c: Waiting
49e2d6393f32: Waiting
bb9cdec9c7f3: Waiting
fce5728aad85: Verifying Checksum
fce5728aad85: Download complete
76610ec20bf5: Verifying Checksum
76610ec20bf5: Download complete
5040bd298390: Verifying Checksum
5040bd298390: Download complete
60170fec2151: Verifying Checksum
60170fec2151: Download complete
11f7af24ed9c: Verifying Checksum
11f7af24ed9c: Download complete
e98f73de8f0d: Verifying Checksum
e98f73de8f0d: Download complete
bb9cdec9c7f3: Verifying Checksum
bb9cdec9c7f3: Download complete
5040bd298390: Pull complete
fce5728aad85: Pull complete
49e2d6393f32: Verifying Checksum
49e2d6393f32: Download complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
 ---> d23bdf5b1b1b
Step 2/6 : VOLUME /tmp
 ---> Running in a7031314084d
Removing intermediate container a7031314084d
 ---> 47169bf41454
Step 3/6 : ADD user-server-0.0.1-SNAPSHOT.jar user-server.jar
 ---> 4fb4d5f53c97
Step 4/6 : RUN bash -c 'touch /user-server.jar'
 ---> Running in ba9b38fcecd4
Removing intermediate container ba9b38fcecd4
 ---> b0f97252614d
Step 5/6 : EXPOSE 9090
 ---> Running in ab54f1739cb7
Removing intermediate container ab54f1739cb7
 ---> 44eb2201db9e
Step 6/6 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/user-server.jar"]
 ---> Running in b280257ce531
Removing intermediate container b280257ce531
 ---> 8be73281de84
Successfully built 8be73281de84
Successfully tagged user-server:latest
3dd0884c1cef0d6d4ceccc23ec0012a18474aaeedf13cb097881e4e2028d5735
SSH: EXEC: completed after 37,620 ms
SSH: Disconnecting configuration [HK-2] ...
SSH: Transferred 1 file(s)
Finished: SUCCESS

7.上面我们都是手动构建的,接下来我们来配置当本地代码提交到Git服务器(码云)后,会自动调用
Generic Webhook Trigger进行自动构建。

7.1配置用户和Security:
(1)在“系统管理–管理用户–用户列表–admin处点击进去–左边侧边栏–设置”设置用户API Token!找到API Token,等会会用。

(2)然后在“系统管理->Configure Global Security”,去掉防止跨站点请求伪造的勾,这样我们就可以在浏览器和码云的webhook访问到api的地址了,不然我们把用户名和密码放到url 中,还是不能访问。

7.2 配置码云WebHooks
找到WebHooks配置界面,在POST地址框中输入如下格式内容:
http://<User ID>:<API Token>@<Jenkins IP地址>:端口/generic-webhook-trigger/invoke
具体如下:

User ID:Jenkins登录用户名

API Token:就是我们上面步骤获取到的API Token;

Jenkins IP地址:就是你的Jenkins的服务器IP地址,这个必须是公网IP地址;

端口:就是Jenkins服务器的端口;

后边的内容就是Generic Webhook Trigger 插件的地址,无需改变;

然后点击“测试”,码云会提示如下信息,别忘了去看一下Jenkins是否开始构建:

{"status":"ok","data":{"triggerResults":{"business":"OK","user-server":"OK"}}}

至此,全部配置已经完成,如有疑问欢迎留言。

广告
赞赏

微信赞赏支付宝赞赏

3 thoughts on “用Jenkins实现Docker容器持续集成与自动部署

  1. Hey there! Someone in my Myspace group shared this website
    with us so I came to check it out. I’m definitely enjoying the information.
    I’m book-marking and will be tweeting this to my followers!
    Excellent blog and wonderful design and style.

  2. Magnificent items from you, man. I have take note your stuff previous to
    and you are simply too magnificent. I actually like what you’ve received right here, really like what you’re saying and the way in which during which you
    assert it. You’re making it enjoyable and you continue to take care of to keep it wise.
    I cant wait to read much more from you. That is actually a wonderful
    web site.

发表评论

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d 博主赞过: