• 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    这个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"
    					"""
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    构建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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
  • 相关阅读:
    mysql
    K8s和Docker
    CSS 段落p 和 行 相关属性 :行高,缩进 水平垂直居中,加粗
    自动化测试的必备准则
    如何判断人脸门禁一体机/人脸识别终端是否支持4G、WIFI、刷IC卡、刷身份证
    做自媒体怎样在一年之内赚到 10万元?
    【UCB操作系统CS162项目】Pintos Lab2:用户程序 User Programs(上)
    Unity3D 拖拽赋值组件与通过Find赋值组件的优点与缺点详解
    【前端面试题】有关html和css的前端面试27问
    [Linux] shell条件语句和if语句
  • 原文地址:https://blog.csdn.net/baidu_38432732/article/details/134427849