智慧保安后台管理-外网项目备份
zhongrj
2023-09-17 8853292babb2ad94de4a3207966f1e83b767cd2d
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
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:latest .'
        }
      }
    }
 
    stage('镜像推送') {
      agent none
      steps {
        container('maven') {
          withCredentials([usernamePassword(credentialsId : 'aliyun' ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) {
            sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
            sh 'docker tag zhbaw:latest $REGISTRY/$DOCKERHUB_NAMESPACE/zhbaw:SNAPSHOT-$BUILD_NUMBER'
            sh 'docker push  $REGISTRY/$DOCKERHUB_NAMESPACE/zhbaw:SNAPSHOT-$BUILD_NUMBER'
            sh 'docker rmi  $REGISTRY/$DOCKERHUB_NAMESPACE/zhbaw:SNAPSHOT-$BUILD_NUMBER'
            sh 'docker rmi  zhbaw:latest'
          }
        }
 
      }
    }
 
    stage('deploy to dev') {
      agent none
      steps {
        container('maven') {
          withCredentials([kubeconfigContent(credentialsId : 'kube-zhba' ,variable : 'KUBE_VAR' ,)]) {
            sh 'kubectl apply -f deploy/deploy.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 = 'registry.cn-hangzhou.aliyuncs.com'
    DOCKERHUB_NAMESPACE = 'arsn'
    GITHUB_ACCOUNT = 'kubesphere'
    APP_NAME = 'devops-java-sample'
  }
  parameters {
    string(name: 'TAG_NAME', defaultValue: '', description: '')
  }
}