智慧保安后台管理-外网
Administrator
2022-07-13 798ab07db6464f24e353dcdf8397506ab246c91d
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
pipeline {
  agent any
  //变量定义
  environment {
    REGISTRY = 'registry.cn-hangzhou.aliyuncs.com'
    ALIYUN_NAMESPACE = 'arsn'
  }
 
  stages {
    //1.拉取源码
    stage('Git Checkout'){
      steps {
        git branch: 'master', credentialsId: 'gitblit', url: 'http://192.168.0.105:10010/r/zhba_management_w.git'
      }
    }
    //2.编译成jar包
    stage('Maven Build') {
      steps {
        sh 'ls'
        sh 'mvn clean package -Dmaven.test.skip=true'
        sh 'ls target/'
      }
    }
    //3.构建镜像
    stage('Build and Push Image'){
      steps{
        sh 'docker build -f Dockerfile -t zhbaw:latest .'
      }
    }
    //4.拉取镜像并启动
    stage('推送镜像'){
      steps{
          script{
            withCredentials([usernamePassword(credentialsId: 'aliyun', passwordVariable: 'password', usernameVariable: 'username')]) {
                sh 'echo "$password" | docker login $REGISTRY -u "$username" --password-stdin'
                sh 'docker tag zhbaw:latest $REGISTRY/$ALIYUN_NAMESPACE/zhbaw:SNAPSHOT-$BUILD_NUMBER'
                sh 'docker push  $REGISTRY/$ALIYUN_NAMESPACE/zhbaw:SNAPSHOT-$BUILD_NUMBER'
                sh 'docker rmi  $REGISTRY/$DOCKERHUB_NAMESPACE/zhbaw:SNAPSHOT-$BUILD_NUMBER'
                sh 'docker rmi  zhbaw:latest'
            }
          }
       }
    }
    //5.部署
    stage('deploy to dev') {
      steps {
          input '是否部署?'
          sh 'docker-compose up -d'
          sh 'docker rmi  $REGISTRY/$DOCKERHUB_NAMESPACE/zhbaw:SNAPSHOT-$(BUILD_NUMBER-1)'
        }
    }
  }
}