Docker+Jenkins持续集成环境(2)使用docker+jenkins构建nodejs前端项目

前文使用Docker搭建Jenkins+Docker持续集成环境我们已经搭建了基于docker+jenkins的持续集成环境,并构建了基于maven的项目。这一节,我们继续扩展功能,增加对Nodejs的支持,实现nodejs项目构建、并打包成docker镜像和自动部署。

1. 配置Nodejs环境#

1.1 安装nodejs插件#

打开系统管理——管理插件——可选插件,搜索NodeJS,选择NodeJS Plugin安装

1.2 配置nodejs 版本#

系统管理 —— 全局工具配置 —— NodeJS,选择安装nodejs,选择当前最新版本9.4,命名NodeJS 9.4。

2. 配置项目#

2.1 新建项目#

新建jenkins项目,选择自由项目,按前文说明配置好SVN、触发器。

在构建环境里,选择Provide Node & npm bin/ folder to PATH,选择我们配置的9.4版本nodejs

2.2 配置构建命令#

一般是通过npm命令构建,我们选择增加构建步骤 —— Excute shell,输入构建命令:

Copy
alias cnpm="npm --registry=https://registry.npm.taobao.org \ --cache=$HOME/.npm/.cache/cnpm \ --disturl=https://npm.taobao.org/dist \ --userconfig=$HOME/.cnpmrc" cnpm install cnpm run build

注意,这里为了构建更快,选择通过alias增加cnpm指令,指定使用淘宝的仓库。

2.3 构建docker镜像#

由于我们构建出来的已经是可访问的资源了,放在dis目录,所以我们可以基于nginx作为基础镜像。
编写DockerFile:

Copy
FROM nginx ADD ./dist /usr/share/nginx/html EXPOSE 80

然后,增加构建步骤,ADD build/publish docker image
设置image:192.168.86.8:5000/allinone-web-cicd
勾上push image,会自动push到192.168.86.8:5000仓库
enter description here

2.4 自动部署镜像#

和上文一样,这里继续使用ssh实现docker镜像部署。

增加构建步骤,Execute shell script on remote host using ssh:

选择docker swarm的manager机器,输入命令:

Copy
docker service rm allinone-web-cicd docker service create --name allinone-web-cicd --replicas 1 --publish 10081:80 192.168.86.8:5000/allinone-web-cicd

这次,我们使用docker service来实现部署,先service rm掉老服务,然后create新服务。

3.测试构建#

点击立即构建:

enter description here

稍等片刻,就构建成功了;

Copy
[SSH] executing... allinone-web-cicd mj9dwq00ath03i05b8bfe5plx overall progress: 0 out of 1 tasks 1/1: overall progress: 0 out of 1 tasks overall progress: 0 out of 1 tasks overall progress: 0 out of 1 tasks overall progress: 0 out of 1 tasks overall progress: 0 out of 1 tasks overall progress: 0 out of 1 tasks overall progress: 1 out of 1 tasks verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Service converged [SSH] completed [SSH] exit-status: 0 Finished: SUCCESS

这个时候,访问swarm集群的任一http://ip:10081,就可以看到效果了。


作者:Jadepeng
出处:jqpeng的技术记事本--http://www.cnblogs.com/xiaoqi
您的支持是对博主最大的鼓励,感谢您的认真阅读。
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

关注作者

欢迎关注作者微信公众号, 一起交流软件开发:欢迎关注作者微信公众号

posted @   JadePeng  阅读(7116)  评论(1编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
CONTENTS