当前位置: 首页 > article >正文

pipeline + node +jenkins+kubernetes部署yarn前端项目

1、编写Dockerfile文件

# Set the base image
FROM node:16.10.0

# WORKDIR /usr/src/app/
WORKDIR /home/option

# Copy files
COPY ./ /home/option/

# Build arguments
LABEL branch=${BRANCH}
LABEL commit=${COMMIT}
LABEL date=${BUILD_DATE}
ARG ENV

# Set ENV variables
ENV COMMIT_BRANCH=${BRANCH}
ENV COMMIT_SHA=${COMMIT}
ENV BUILD_DATE=${DATE}
ENV INSTALLATION_TYPE=docker
ENV MY_ENV=${ENV}


# Install dependencies and compile
RUN yarn install --frozen-lockfile
RUN yarn
RUN yarn build:${MY_ENV}

# Expose port 15888 - note that docs port is 3000
EXPOSE 3000

# Set the default command to run when starting the container
CMD yarn run start

这个Dockerfile需要传环境变量参数
2、编写pipeline文件

// 参数构建
pipeline {
    agent any
    parameters {
        gitParameter(name: 'BRANCH_TAG', type: 'PT_BRANCH_TAG', branchFilter: 'origin/(.*)', defaultValue: 'main', selectedValue: 'DEFAULT', sortMode: 'DESCENDING_SMART', description: '请选择需要部署的代码:')
        choice(name: 'mode', choices: ['deploy','rollback'], description: '请选择发布或者回滚?')
        choice(name: 'ENVMENT', choices: ['sit','exp','gray','prod'], description: '环境参数')        
        string(name: 'iname', defaultValue: 'option-front', description: '服务名称')
    }
    environment {
        dest_path = "/var/jenkins_home/workspace/${JOB_NAME}"
        job_path = "/data/docker-compose/jenkins/jenkins_home/workspace/${JOB_NAME}"
        mod_path = "/data/docker-compose/jenkins/jenkins_home/mod"
    }

    stages {
        stage('clean'){
            steps {
              cleanWs(
                  cleanWhenAborted: true, 
                  cleanWhenFailure: true, 
                  cleanWhenNotBuilt: true, 
                  cleanWhenSuccess: true, 
                  cleanWhenUnstable: true, 
                  cleanupMatrixParent: true, 
                  disableDeferredWipeout: true,
                  deleteDirs: true
              )
            }    
        }
        
        stage('从 gitlab 中拉取代码') {
            when {
                environment name: 'mode',value: 'deploy'
            }
            steps {
                deleteDir()
                checkout([$class: 'GitSCM', 
                    branches: [[name: "${params.BRANCH_TAG}"]],
                    gitTool: 'Default', 
                    userRemoteConfigs: [[url: 'https://gitlab.yunson.com/test/option/option_front.git', credentialsId: 'gitlab-deploy',]]
                ])
            }
        }

        stage('Node Install And Build docker image'){
            steps{
                script{
                    sh """
                        docker build --build-arg ENV=${ENVMENT} -t harbor.yunson.com/test/${iname}:$ENVMENT .
                    """                  
                }
            }
        }
        stage('Push image to hub'){
            steps{
                script{
                    withCredentials([usernamePassword(credentialsId: 'harbor-secret-dev',, passwordVariable: 'password', usernameVariable: 'username')]) {
                    sh 'docker login -u ${username} -p ${password}  harbor.yunson.com'
                    }
                    sh 'docker push harbor.yunson.com/test/${iname}:$ENVMENT'
                }
            }
        }

        stage('deploy Server'){
            steps{
                script{
                    sh """
                        curl -X PUT \
                        -H "content-type: application/json" \
                        -H "Cookie: KuboardUsername=admin; KuboardAccessKey=ccpyiaxei7i8.disiejnk4dg5pfjlobgmflkuefkufdwf" \
                        -d '{"kind":"deployments","namespace":"test","name":"${iname}","images":{"harbor.yunson.com/test/${iname}":"harbor.yunson.com/test/${iname}:${ENVMENT}"}}' \
                        "http://69.36.89.2:18085/kuboard-api/cluster/Test/kind/CICDApi/admin/resource/updateImageTag"    
                        
                        curl -X PUT \
                        -H "Content-Type: application/yaml" \
                        -H "Cookie: KuboardUsername=admin; KuboardAccessKey=ccpyiaxei7i8.disiejnk4dg5pfjlobgmflkuefkufdwf" \
                        -d '{"kind":"deployments","namespace":"test","name":"${iname}"}' \
                        "http://69.36.89.2:18085/kuboard-api/cluster/Test/kind/CICDApi/admin/resource/restartWorkload"
					"""
                }
            }
        }
    }
}

构建jenkins项目
在这里插入图片描述
构建成功如下
在这里插入图片描述
3、编写部署pods的yaml文件

---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    k8s.kuboard.cn/displayName: option-front
  labels:
    k8s.kuboard.cn/layer: web
    k8s.kuboard.cn/name: option-front
  name: option-front
  namespace: test
  resourceVersion: '38871139'
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s.kuboard.cn/name: option-front
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      annotations:
        kubectl.kubernetes.io/restartedAt: '2023-11-15T19:51:15+08:00'
      creationTimestamp: null
      labels:
        k8s.kuboard.cn/name: option-front
        pod-template-hash: 645b77b9c
    spec:
      containers:
        - env:
            - name: TZ
              value: Asia/Shanghai
          image: 'harbor.yunson.com/test/option-front:sit'
          imagePullPolicy: Always
          name: option-front
          ports:
            - containerPort: 3000
              name: msag7
              protocol: TCP
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      imagePullSecrets:
        - name: acr-secret
        - name: harbor-secret
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30

---
apiVersion: v1
kind: Service
metadata:
  annotations: {}
  labels:
    k8s.kuboard.cn/layer: web
    k8s.kuboard.cn/name: option-front
  name: option-front
  namespace: biking
  resourceVersion: '31418671'
spec:
  internalTrafficPolicy: Cluster
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
  ports:
    - name: ef5znm
      port: 3000
      protocol: TCP
      targetPort: 3000
  selector:
    k8s.kuboard.cn/name: option-front
  sessionAffinity: None
  type: ClusterIP

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations: {}
  labels:
    k8s.kuboard.cn/layer: web
    k8s.kuboard.cn/name: option-front
  name: option-front
  namespace: biking
  resourceVersion: '38696977'
spec:
  ingressClassName: biking-ingress
  rules:
    - host: option-test.cuiwjrpcvi.com
      http:
        paths:
          - backend:
              service:
                name: option-front
                port:
                  number: 3000
            path: /
            pathType: Prefix
          - backend:
              service:
                name: hyperw-option
                port:
                  number: 9024
            path: /service-option-core
            pathType: Prefix
          - backend:
              service:
                name: hyperw-assets
                port:
                  number: 9027
            path: /hyperw-assets
            pathType: Prefix
          - backend:
              service:
                name: option-index
                port:
                  number: 9029
            path: /service-option-index
            pathType: Prefix
          - backend:
              service:
                name: option-ws
                port:
                  number: 9090
            path: /ws
            pathType: Prefix
          - backend:
              service:
                name: hyperw-system
                port:
                  number: 9028
            path: /hyperw-system
            pathType: Prefix
          - backend:
              service:
                name: legend-index
                port:
                  number: 9026
            path: /public/web/kline/history
            pathType: Prefix
          - backend:
              service:
                name: legend-index
                port:
                  number: 9026
            path: /public/web/timeline/history
            pathType: Prefix
          - backend:
              service:
                name: option-ws
                port:
                  number: 9090
            path: /service-option-ws/ws
            pathType: Prefix
          - backend:
              service:
                name: hyperw-user
                port:
                  number: 9023
            path: /hyperw-user
            pathType: Prefix
          - backend:
              service:
                name: hyperw-agent
                port:
                  number: 9033
            path: /hyperw-agent
            pathType: Prefix
          - backend:
              service:
                name: hyperw-binary-option
                port:
                  number: 9044
            path: /binary-option
            pathType: Prefix
  tls:
    - hosts:
        - test.yunson.com
      secretName: yunson.com-ssl

http://www.kler.cn/a/135284.html

相关文章:

  • 前端vue 列表中回显并下拉选择修改标签
  • [JAVAEE] 面试题(四) - 多线程下使用ArrayList涉及到的线程安全问题及解决
  • HTTP常见的状态码有哪些,都代表什么意思
  • C++单例模式与多例模式
  • 马斯克万卡集群AI数据中心引发的科技涟漪:智算数据中心挑战与机遇的全景洞察
  • Vue 项目打包后环境变量丢失问题(清除缓存),区分.env和.env.*文件
  • Qt给控件添加图片
  • 牛客--求最小公倍数python
  • 动手学深度学习——循环神经网络的从零开始实现(原理解释+代码详解)
  • python绘图常见问题及解决方法总结
  • C编译环境和预处理(非常详细,建议收藏)
  • 【智能家居】5、主流程设计以及外设框架编写与测试
  • 截取某个元素前面的数字
  • 算法必刷系列之位运算
  • 深度学习系列53:mmdetection上手
  • 目标检测标注工具AutoDistill
  • RK3588平台开发系列讲解(项目篇)嵌入式AI的学习步骤
  • UML统一建模语言
  • rk3588编译lunch出错
  • 广州华锐互动VRAR:利用VR开展刑事案件公安取证培训,沉浸式体验提升实战能力
  • 第十一周任务总结
  • mysql无法访问故障排除步骤
  • 【Zabbix】Zabbix Agent 2在Ubuntu/Debian系统上的安装
  • 事务隔离级别和MVCC
  • 【开题报告】基于uni-app的汽车租赁app的设计与实现
  • NOSQL----redis的安装和基础命令