pipeline {
    agent any

    environment {
        // git账号
        GIT_CREDENTIAL_ID = '175776d4-6bbc-4da1-ace2-f2a89453fba4'
        // git地址
        GIT_ADDRESS = 'https://git.mall4j.com/tmerclub/tmerclub'
        // git分支
        GIT_BRANCH = 'master'

        // harbor账号
        HARBOR_CREDENTIAL_ID = 'edd44d2d-f182-40ab-8e72-b4fcae6f793f'
        // harbor地址
        HARBOR_ADDR = '192.168.1.11:80'
        // 名命空间
        NAMESPACE = 'tmerclub-release'
    }
    stages {
        stage ('拉取git仓库代码') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/' + "$GIT_BRANCH"]], extensions: [], userRemoteConfigs: [[credentialsId: "$GIT_CREDENTIAL_ID", url: "$GIT_ADDRESS"]]])
            }
        }
        stage ('通过maven构建项目') {
            steps {
                // 构建项目
                sh '/var/jenkins_home/maven/bin/mvn -T 1C clean package -DskipTests=true'
            }
        }
        stage ('通过docker并行制作镜像') {

            parallel {
                stage('构建tmerclub-auth镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-auth"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
                stage('构建tmerclub-biz镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-biz"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
                stage('构建tmerclub-marketing镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-marketing"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
                stage('构建tmerclub-flow镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-flow"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
                stage('构建tmerclub-gateway镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-gateway"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
                stage('构建tmerclub-group镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-group"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
                stage('构建tmerclub-im镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-im"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
                stage('构建tmerclub-leaf镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-leaf"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
                stage('构建tmerclub-order镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-order"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
                stage('构建tmerclub-payment镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-payment"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
                stage('构建tmerclub-admin镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-admin"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
                stage('构建tmerclub-product镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-product"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
                stage('构建tmerclub-search镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-search"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
                stage('构建tmerclub-seckill镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-seckill"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
                stage('构建tmerclub-user镜像') {
                    steps {
                        withEnv(["PROJECT_NAME=tmerclub-user"]) {
                            sh 'cd ./$PROJECT_NAME && docker build -t $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER .'
                            withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
                                sh 'echo "$DOCKER_PASSWORD" | docker login $HARBOR_ADDR -u "$DOCKER_USERNAME" --password-stdin'
                                sh 'docker push  $HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER'
                                sh 'ssh root@192.168.1.5 kubectl set image deploy/$PROJECT_NAME $PROJECT_NAME=$HARBOR_ADDR/$NAMESPACE/$PROJECT_NAME:$BUILD_NUMBER -n $NAMESPACE'
                            }
                        }
                    }
                }
            }
        }
    }
}
