1. 程式人生 > >Docker+Jenkins持續集成環境(2)使用docker+jenkins構建nodejs前端項目

Docker+Jenkins持續集成環境(2)使用docker+jenkins構建nodejs前端項目

HR meta blog 新版本 註意 class pro creat war

前文使用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,就可以看到效果了。

Docker+Jenkins持續集成環境(2)使用docker+jenkins構建nodejs前端項目