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,输入构建命令:
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:
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仓库
2.4 自动部署镜像#
和上文一样,这里继续使用ssh实现docker镜像部署。
增加构建步骤,Execute shell script on remote host using ssh:
选择docker swarm的manager机器,输入命令:
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.测试构建#
点击立即构建:
稍等片刻,就构建成功了;
[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
出处:https://www.cnblogs.com/xiaoqi/p/docker-jenkins-nodejs-cicd.html
版权:本文采用「署名-非商业性使用-相同方式共享 4.0 国际(欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接) 」知识共享许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了