Jenkins を使用してnodeをビルド
環境変数
- システムアドミニストレーション->グローバル属性 -> 環境変数 -> 新しいキーを追加します。
- JENKINS_WORKSPACE : /var/lib/docker/volumes/soft_jenkin_home/_data/workspace
NodeJS の設定
# 下载NodeJS插件
# 调整[系统管理 -> 全局工具配置 -> nodeJS 配置]
# nodejs 14.15.0
Jenkins の設定
- フリースタイルでプロジェクトを作成する
- ソースとしてgiteeを使用
- [构建环境] セクションに [Provide Node & npm bin/folder to PATH] を追加します。
- [构建]ステップ に[shell]ステップを追加します。
pwd
# ディスクのインストール中の全コンテンツの
rm -rf dist
# 依存関係(不要)
npm install --registry https://registry.npm.taobao. rg
# ビルドプロジェクト
npm run build:prod
# node_modulesとdist 以外の全てのコンテンツ
ls | grep -v 'node_modules\|dist' | xargs rm -rf
- 作成手順に[Send files or execute commands over SSH (sshリモートでシェルを実行)] ステップを追加します。
export app_version='1.0'
if [ -z $DOCKER_JENKINS_WORKSPACE ]; then
echo "环境变量 DOCKER_JENKINS_WORKSPACE:[$DOCKER_JENKINS_WORKSPACE] 缺失,需配置 DOCKER_JENKINS_WORKSPACE 环境变量(exit -1)"
exit -1
fi
cd $DOCKER_JENKINS_WORKSPACE/$JOB_NAME
mkdir -p ./nginx/conf.d/
# todo 按需调整用户名和组
chown -R 1000:1000 ./nginx
# 添加default.conf文件
tee ./nginx/conf.d/default.conf <<-'EOF'
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html; # 用于解决刷新页面后,显示404的问题
}
}
EOF
# 编辑Dockerfile文件
echo "FROM nginx:stable-alpine" > Dockerfile
echo "MAINTAINER Fa" >> Dockerfile
echo "WORKDIR /usr/share/nginx/html" >> Dockerfile
echo "RUN rm -rf *" >> Dockerfile
echo "ADD ./dist ." >> Dockerfile
echo "ADD ./nginx/conf.d/default.conf /etc/nginx/conf.d/" >> Dockerfile
echo "EXPOSE 80" >> Dockerfile
# 构建镜像
docker build --no-cache -t $JOB_NAME:$app_version .
# 上传镜像到私服
docker tag $JOB_NAME:$app_version registry.cn-zhangjiakou.aliyuncs.com/fa/$JOB_NAME:$app_version
docker push registry.cn-zhangjiakou.aliyuncs.com/fa/$JOB_NAME:$app_version
# 删除空镜像
docker images | awk '{if($1=="<none>")print $3}' | xargs docker rmi &> /dev/null
# 编辑stack yml文件
tee $JOB_NAME.yml <<-'EOF'
version: '3.5'
services:
$JOB_NAME:
image: registry.cn-zhangjiakou.aliyuncs.com/fa/$JOB_NAME:${app_version}
environment:
TZ: "Asia/Shanghai"
networks:
- middleware
deploy:
replicas: 1
update_config:
parallelism: 1
restart_policy:
condition: on-failure
networks:
middleware:
external: true
EOF
docker stack up -c $JOB_NAME.yml app --with-registry-auth
-
Nginxにプロキシ設定を追加
-
conf.d ディレクトリに mall-admin-web-demo.conf ファイルを追加します。
server {
listen 80;
server_name mall-admin-web-demo.iuin.xyz iuin.xyz;
access_log /var/log/mall-admin-web-demo_access.log;
error_log /var/log/mall-admin-web-demo_error.log;
location / {
include cors.conf;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://mall-admin-web-demo;
}
}
デプロイする
# 镜像下载问题,可试这添加--with-registry-auth看看
docker stack up -c $JOB_NAME.yml --with-registry-auth app