jenkins发布nodejs项目(修改)
前面做过jenkins发布nodejs项目,使用ssh插件,这回没有使用ssh插件。
1、将项目、发布动作、主机作为参数,配置参数化构建
2、配置拉取git仓库项目
3、构建环境选项nodejs
4、编写构建脚本
url="harbor.xxx.com:10443/library"
opt="/opt/kubernetes/bin/kubectl"
value=`ssh root@$host "$opt -n wehgc get deploy $project" |tail -1|awk '{print \$1}'`
VER=`ssh root@$host "cd /home/lucky-front/$project;grep $url $project.yaml|cut -d ":" -f 4"` ##这个$VER是取docker镜像的标签
export NODE_HOME=/var/jenkins_home/node-v14.15.4-linux-x64
export PATH=$PATH:$NODE_HOME/bin
export PATH=$PATH:/root/.npm-global/bin
update(){
# npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install #安装cnpm
cnpm run build #构建项目
ssh root@$host "rm -rf /home/lucky-front/$project/$project"
scp -r $WORKSPACE/$project root@$host:/home/lucky-front/$project/
ssh root@$host "cd /home/lucky-front/$project;sed -i 's#$VER#$BUILD_ID.1#g' $project.yaml" ##这里面是表示每次更新后将标签更改为$BUILD_ID.
ssh root@$host "cd /home/lucky-front/$project;docker build -t $url/$project:$BUILD_ID.1 ."
ssh root@$host "docker login -u "admin" -p "GJ9JwGl0jL" $url"
ssh root@$host "docker push $url/$project:$BUILD_ID.1"
[ -n "$value" ] && ssh root@$host "$opt -n wehgc set image deployment $project $project=$url/$project:$BUILD_ID.1" || ssh root@$host "cd /home/lucky-front/$project;$opt apply -f $project.yaml"
}
case $Status in
Deploy)
echo "Status:$Status"
update
;;
Rollback)
echo "Status:$Status"
ssh root@$host "kubectl -n wehgc rollout history deploy $project"
ssh root@$host "kubectl -n wehgc rollout undo deploy $project"
;;
*)
exit
;;
esac
5、在执行构建之前要在主机的
/home/lucky-front/$project目录创建好以下三个文件
Dockerfile文件
[root@master trucpal]# cat Dockerfile FROM ubuntu:latest RUN apt-get update RUN apt-get install -y vim RUN apt-get install -y nginx RUN apt-get install -y net-tools COPY nginx.conf /etc/nginx/nginx.conf RUN ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log RUN mkdir /usr/local/www COPY $project /usr/local/www/$project ENV TZ=Asia/Shanghai RUN set -eux; \ ln -snf /usr/share/zoneinfo/$TZ /etc/localtime; echo $TZ > /etc/timezone WORKDIR /home CMD ["nginx","-g","daemon off;"]
yaml文件
[root@master trucpal]# cat trucpal.yaml apiVersion: v1 kind: Service metadata: name: trucpal namespace: wehgc spec: ports: - nodePort: 31280 port: 1280 protocol: TCP targetPort: 1280 selector: app: trucpal type: NodePort --- apiVersion: apps/v1 kind: Deployment metadata: name: trucpal namespace: wehgc spec: replicas: 1 selector: matchLabels: app: trucpal template: metadata: labels: app: trucpal spec: containers: - image: harbor.wehgc.com:10443/library/trucpal:5.1 imagePullPolicy: Always name: trucpal ports: - containerPort: 1280 protocol: TCP imagePullSecrets: - name: registry-pull-secret
nginx.conf文件
[root@master trucpal]# cat nginx.conf
worker_processes 2;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
include /etc/nginx/conf.d/*.conf;
server {
listen 1280;
server_name 127.0.0.1;
root /usr/local/www/trucpal;
location ~ .*\.(htm|html|gif|jpg|jpeg|png|ico|css|js|txt|flv|doc)$ {
error_page 405 =200 $uri;
}
index index.html;
charset utf-8;
location / {
try_files $uri $uri/ @router;
index index.html;
}
location @router {
rewrite ^.*$ /index.html last;
}
}
}
6、这里的逻辑说明一下,因为这是要发布到k8s里面的,所以Dockerfile是要构建docker镜像的,nginx配置文件是Dockerfile里面写明要拷到容器里面的,yaml文件是用来在k8s里面使用Kubectl apply执行的


