使用jenkins通过docker方式自动发布前端服务

在【使用jenkins通过docker方式自动发布java后端服务】基础上增加插件:Docker Slaves Plugin、NodeJS、NodeJS Plugin等前端打包插件。

1、配置Docker Slaves Plugin

首页点击配置、系统配置
在这里插入图片描述

2、全局工具配置

挂载系统安装的NodeJS
在这里插入图片描述

挂载系统安装的git
在这里插入图片描述

3、创建项目

这里选择构建一个自由风格的软件项目
在这里插入图片描述

配置源码git地址
用户和密码
在这里插入图片描述

构建环境配置
在这里插入图片描述

配置构建执行shell
在这里插入图片描述

shell脚本如下:

echo $PATH
npm config list
#npm编译
npm install
rm -rf dist
#npm打包
npm run build
pwd
tar -zcvf Globe.tar.gz dist

配置构建后操作
在这里插入图片描述

脚本如下:

#!bin/sh
cd /data/nari/ibImages/Globe/
#当前路径
pwd
#删除原来的文件夹
rm -rf Globe
#解压文件夹
tar -zxvf Globe.tar.gz
#修改文件夹名称
mv dist Globe
#删除压缩包
rm -rf  Globe.tar.gz
docker build -t 192.168.5.71:5609/docker_rep/ibv-globe:latest .
docker push 192.168.5.71:5609/docker_rep/ibv-globe:latest
docker rmi 192.168.5.71:5609/docker_rep/ibv-globe:latest

3、构建项目

点击build构建项目,省略……


----------------------------------可选版本配置----------------------------

自由选择版本号配置

在这里插入图片描述

#!bin/sh
cd /data/nari/ibImages/ibv-app/
#当前路径
pwd
#删除原来的文件夹
rm -rf app
#解压文件夹
tar -zxvf app.tar.gz
#修改文件夹名称
mv dist app
#删除压缩包
rm -rf  app.tar.gz
TAG=$dockerTag
PORT=7007
IMAGE_NAME=192.168.5.71:5609/docker_rep/ibv-app
CONTAINER_NAME=ibv-app
#停止容器
docker ps -a | grep "$CONTAINER_NAME" | awk '{print $1}' |xargs docker stop
#删除容器
docker ps -a | grep "$CONTAINER_NAME" | awk '{print $1}' |xargs docker rm
#删除原来的镜像
docker images | grep "$IMAGE_NAME" | awk '{print $3}' |xargs docker rmi
#生成镜像
docker build -t $IMAGE_NAME:$TAG .
#运行容器
docker run --name $CONTAINER_NAME -d -p $PORT:80 --restart=always $IMAGE_NAME:$TAG
#push到镜像仓库
docker push $IMAGE_NAME:$TAG

在这里插入图片描述

----------------------------------可选版本配置----------------------------


3、k8s发布

使用k8s从镜像仓库拉取镜像,发布前端
yml如下:

apiVersion: v1
kind: Service
metadata:
  annotations:
    k8s.kuboard.cn/workload: web-ibv-globe
  creationTimestamp: '2020-11-05T09:18:43Z'
  labels:
    k8s.kuboard.cn/layer: web
    k8s.kuboard.cn/name: web-ibv-globe
  name: web-ibv-globe
  namespace: default
  resourceVersion: '3644503'
  selfLink: /api/v1/namespaces/default/services/web-ibv-globe
  uid: c00d05cf-e5c6-4930-9ef2-80052bcd7c1b
spec:
  clusterIP: 10.1.166.204
  externalTrafficPolicy: Cluster
  ports:
    - name: d8w6wx
      nodePort: 30051
      port: 80
      protocol: TCP
      targetPort: 80
  selector:
    k8s.kuboard.cn/layer: web
    k8s.kuboard.cn/name: web-ibv-globe
  sessionAffinity: None
  type: NodePort


发布后自动生成的yml如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: '7'
    k8s.kuboard.cn/ingress: 'false'
    k8s.kuboard.cn/service: NodePort
    k8s.kuboard.cn/workload: web-ibv-globe
  creationTimestamp: '2020-11-05T09:18:43Z'
  generation: 8
  labels:
    k8s.kuboard.cn/layer: web
    k8s.kuboard.cn/name: web-ibv-globe
  name: web-ibv-globe
  namespace: default
  resourceVersion: '3887733'
  selfLink: /apis/apps/v1/namespaces/default/deployments/web-ibv-globe
  uid: f8a74fc8-d567-45f5-870b-719e9c2e8863
spec:
  progressDeadlineSeconds: 600
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s.kuboard.cn/layer: web
      k8s.kuboard.cn/name: web-ibv-globe
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      annotations:
        kubectl.kubernetes.io/restartedAt: '2020-11-07T11:01:49+08:00'
      labels:
        k8s.kuboard.cn/layer: web
        k8s.kuboard.cn/name: web-ibv-globe
    spec:
      containers:
        - image: '192.168.5.71:5609/docker_rep/ibv-globe'
          imagePullPolicy: Always
          name: ibv-globe
          ports:
            - containerPort: 80
              protocol: TCP
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      imagePullSecrets:
        - name: harbor
      nodeSelector:
        disktype: support
      restartPolicy: Always
      schedulerName: default-scheduler
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 3
  conditions:
    - lastTransitionTime: '2020-11-06T03:14:46Z'
      lastUpdateTime: '2020-11-06T03:14:46Z'
      message: Deployment has minimum availability.
      reason: MinimumReplicasAvailable
      status: 'True'
      type: Available
    - lastTransitionTime: '2020-11-05T09:18:43Z'
      lastUpdateTime: '2020-11-07T03:01:55Z'
      message: ReplicaSet "web-ibv-globe-698d86cbc7" has successfully progressed.
      reason: NewReplicaSetAvailable
      status: 'True'
      type: Progressing
  observedGeneration: 8
  readyReplicas: 3
  replicas: 3
  updatedReplicas: 3

---
apiVersion: v1
kind: Service
metadata:
  annotations:
    k8s.kuboard.cn/workload: web-ibv-globe
  creationTimestamp: '2020-11-05T09:18:43Z'
  labels:
    k8s.kuboard.cn/layer: web
    k8s.kuboard.cn/name: web-ibv-globe
  name: web-ibv-globe
  namespace: default
  resourceVersion: '3644503'
  selfLink: /api/v1/namespaces/default/services/web-ibv-globe
  uid: c00d05cf-e5c6-4930-9ef2-80052bcd7c1b
spec:
  clusterIP: 10.1.166.204
  externalTrafficPolicy: Cluster
  ports:
    - name: d8w6wx
      nodePort: 30051
      port: 80
      protocol: TCP
      targetPort: 80
  selector:
    k8s.kuboard.cn/layer: web
    k8s.kuboard.cn/name: web-ibv-globe
  sessionAffinity: None
  type: NodePort
  
  

4、测试

浏览器访问:http://k8s集群地址:30051即可

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐