1. 程式人生 > 實用技巧 >穀粒商城部署推送映象(五十一)

穀粒商城部署推送映象(五十一)

383-390 流水線部署

問題:在程式碼掃描的時候,會有報錯,無法識別kubesphere內嵌的snoarqube,但是改成我主機安裝的發現又沒什麼問題,但是最後會發送回調請求判斷這個階段有沒有成功,所以也有問題,所以先跳過程式碼掃描。

正在成功的時候如下圖:

修改後的Jenkins檔案:

pipeline {
  agent {
    node {
      label 'maven'
    }

  }
  environment {
    DOCKER_CREDENTIAL_ID = 'dockerhub-id'
    GITHUB_CREDENTIAL_ID = 'gitee-id'
    KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
    REGISTRY = 'docker.io'
    DOCKERHUB_NAMESPACE = 'dalianpai'
    GITHUB_ACCOUNT = 'dalianpai'
    SONAR_CREDENTIAL_ID = 'sonar-qube'
    BRANCH_NAME = 'prod'
  }
  
  stages {
    stage('拉取程式碼') {
      steps {
        git(url: 'https://gitee.com/dalianpai/gulimall.git', credentialsId: 'gitee-id', branch: 'prod', changelog: true, poll: false)
        sh 'echo 正在構建 $PROJECT_NAME 版本號:$PROJECT_VERSION'
        container ('maven') {
            sh "mvn clean install -Dmaven.test.skip=true -gs `pwd`/mvn-settings.xml"
        }
      }
    }
    
    stage ('構建映象-推送映象') {
        steps {
            container ('maven') {
                sh 'mvn  -Dmaven.test.skip=true -gs `pwd`/mvn-settings.xml clean package'
                sh 'cd $PROJECT_NAME && docker build -f Dockerfile -t $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER .'
                withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$DOCKER_CREDENTIAL_ID" ,)]) {
                    sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
                    sh 'docker tag  $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:latest '
                    sh 'docker push  $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:latest '
                }
            }
        }
    }
    
    stage('部署到k8s') {
          steps {
            input(id: "deploy-to-dev-$PROJECT_NAME", message: "是否將 $PROJECT_NAME 部署到叢集中?")
            kubernetesDeploy(configs: "$PROJECT_NAME/deploy/**", enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID")
          }
    }
     
    stage('釋出版本'){
          when{
            expression{
              return params.PROJECT_VERSION =~ /v.*/
            }
          }
          steps {
              container ('maven') {
                input(id: 'release-image-with-tag', message: '釋出當前版本映象嗎?')
                  withCredentials([usernamePassword(credentialsId: "$GITHUB_CREDENTIAL_ID", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
                    sh 'git config --global user.email "[email protected]" '
                    sh 'git config --global user.name "dalianpai" '
                    sh 'git tag -a $PROJECT_VERSION -m "$PROJECT_VERSION" '
                    sh 'git push http://$GIT_USERNAME:[email protected]/$GITHUB_ACCOUNT/gulimall.git --tags --ipv4'
                  }
                sh 'docker tag  $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:$PROJECT_VERSION '
                sh 'docker push  $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:$PROJECT_VERSION '
          }
          }
     }
    
  }
  parameters {
    string(name: 'PROJECT_VERSION', defaultValue: 'v0.0Beta', description: '')
    string(name: 'PROJECT_NAME', defaultValue: '', description: '')
  }
}

修改後的k8s資源清單

kind: Deployment
apiVersion: apps/v1
metadata:
  name: gulimall-gateway
  namespace: gulimall
  labels:
    app: gulimall-gateway
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gulimall-gateway
  template:
    metadata:
      labels:
        app: gulimall-gateway
    spec:
      containers:
        - name: gulimall-gateway
          image: $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:latest
          ports:
            - containerPort: 8080
              protocol: TCP
          resources:
            limits:
              cpu: 1000m
              memory: 1000Mi
            requests:
              cpu: 10m
              memory: 10Mi
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600

---

kind: Service
apiVersion: v1
metadata:
  name: gulimall-gateway
  namespace: gulimall
  labels:
    app: gulimall-gateway
spec:
  ports:
    - name: http
      protocol: TCP
      port: 8080
      targetPort: 8080
      nodePort: 31004
  selector:
    app: gulimall-gateway
  type: NodePort
  sessionAffinity: None

修改後的Dockerfile,不然會報OOM

FROM java:8
EXPOSE 8080

VOLUME /tmp
ADD target/*.jar  /app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-jar","-Xms256m","-Xmx700m","/app.jar","--spring.profiles.active=prod"]