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  阅读(7117)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示
CONTENTS