智慧保安后台管理-外网项目备份
钟日健
2022-03-19 c68a891f178447cfcaed15cf13928f6969e3ff5a
jenkinsfile,deploy 新增harbor 镜像仓库配置
1 files modified
2 files added
153 ■■■■■ changed files
Jenkinsfile-harbor 87 ●●●●● patch | view | raw | blame | history
deploy/deploy-harbor.yml 64 ●●●●● patch | view | raw | blame | history
deploy/deploy.yml 2 ●●● patch | view | raw | blame | history
Jenkinsfile-harbor
New file
@@ -0,0 +1,87 @@
pipeline {
  agent {
    node {
      label 'maven'
    }
  }
  stages {
    stage('拉取代码') {
      agent none
      steps {
        container('base') {
          git(url: 'http://192.168.0.105:10010/r/zhba_management_w.git', credentialsId: 'gitblit', branch: 'master', changelog: true, poll: false)
        }
      }
    }
    stage('打 jar 包') {
      agent none
      steps {
        container('maven') {
          sh 'ls'
          sh 'mvn clean package -Dmaven.test.skip=true'
          sh 'ls target/'
        }
      }
    }
    stage('镜像打包') {
      agent none
      steps {
        container('maven') {
          sh 'docker build -f Dockerfile -t zhbaw-harbor:latest .'
        }
      }
    }
    stage('镜像推送') {
      agent none
      steps {
        container('maven') {
          withCredentials([usernamePassword(credentialsId : 'harbor' ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) {
            sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
            sh 'docker tag zhbaw-harbor:latest $REGISTRY/$DOCKERHUB_NAMESPACE/zhbaw-harbor:SNAPSHOT-$BUILD_NUMBER'
            sh 'docker push  $REGISTRY/$DOCKERHUB_NAMESPACE/zhbaw-harbor:SNAPSHOT-$BUILD_NUMBER'
          }
        }
      }
    }
    stage('deploy to dev') {
          agent none
          steps {
            container('maven') {
              withCredentials([kubeconfigContent(credentialsId : 'kube-zhba' ,variable : 'KUBE_VAR' ,)]) {
                sh 'kubectl apply -f deploy/deploy-harbor.yml'
              }
            }
          }
        }
    stage('deploy to production') {
      steps {
        input(id: 'deploy-to-production', message: 'deploy to production?')
        kubernetesDeploy(configs: 'deploy/prod-ol/**', enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID")
      }
    }
  }
  environment {
    DOCKER_CREDENTIAL_ID = 'dockerhub-id'
    GITHUB_CREDENTIAL_ID = 'github-id'
    KUBECONFIG_CREDENTIAL_ID = 'kube-zhba'
    REGISTRY = '192.168.0.191'
    DOCKERHUB_NAMESPACE = 'zhbaw'
    GITHUB_ACCOUNT = 'kubesphere'
    APP_NAME = 'devops-java-sample'
  }
  parameters {
    string(name: 'TAG_NAME', defaultValue: '', description: '')
  }
}
deploy/deploy-harbor.yml
New file
@@ -0,0 +1,64 @@
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: zhbaw-harbor
  name: zhbaw-harbor
  #一定要写名称空间
  namespace: zhba
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  selector:
    matchLabels:
      app: zhbaw-harbor
  strategy:
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 50%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: zhbaw-harbor
    spec:
      imagePullSecrets:
        - name: harbor  #提前在项目下配置访问阿里云的账号密码
      containers:
        - image: 192.168.0.191/zhbaw/zhbaw-harbor:SNAPSHOT-1
#        - image: $REGISTRY/$DOCKERHUB_NAMESPACE/zhba:SNAPSHOT-$BUILD_NUMBER
          imagePullPolicy: Always
          name: zhbaw-harbor
          env:
            - name: JAVA_TOOL_OPTIONS
              value: -Xmx512m -Xms128m -Xmn32m -Xss16m
          ports:
            - containerPort: 81
              protocol: TCP
          resources:
            limits:
              cpu: 300m
              memory: 600Mi
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: zhbaw-harbor
  name: zhbaw-harbor
  namespace: zhba
spec:
  ports:
    - name: http
      port: 81
      protocol: TCP
      targetPort: 81
  selector:
    app: zhbaw-harbor
  sessionAffinity: None
  type: ClusterIP
deploy/deploy.yml
@@ -25,7 +25,7 @@
      imagePullSecrets:
        - name: aliyun  #提前在项目下配置访问阿里云的账号密码
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/arsn/zhbaw:SNAPSHOT-8
        - image: registry.cn-hangzhou.aliyuncs.com/arsn/zhbaw:SNAPSHOT-9
#        - image: $REGISTRY/$DOCKERHUB_NAMESPACE/zhba:SNAPSHOT-$BUILD_NUMBER
          imagePullPolicy: Always
          name: zhbaw