前面作過jenkins發佈nodejs項目,使用ssh插件,這回沒有使用ssh插件。css
一、將項目、發佈動做、主機做爲參數,配置參數化構建html
二、配置拉取git倉庫項目node
三、構建環境選項nodejslinux
四、編寫構建腳本nginx
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
五、在執行構建以前要在主機的git
/home/lucky-front/$project目錄建立好如下三個文件docker
Dockerfile文件npm
[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文件
ubuntu
[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文件vim
[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; } } }
六、這裏的邏輯說明一下,由於這是要發佈到k8s裏面的,因此Dockerfile是要構建docker鏡像的,nginx配置文件是Dockerfile裏面寫明要拷到容器裏面的,yaml文件是用來在k8s裏面使用Kubectl apply執行的