def git_auth="xxx"
def git_url="xxx"
def imageName = "xxx:${branch}"
def harbor_url="xxx"
def harbor_project_name="library"
def harbor_auth_id="xxxxx"
def JAR_FILE="target/xxx-0.0.1-SNAPSHOT.jar"
node {
stage('拉取代码') {
checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[ url: "${git_url}"]]])
}
stage('编译代码,打包') {
sh 'mvn clean package -Dfile.encoding=UTF-8 -DskipTests=true'
stash includes: 'target/*.jar', name: 'app'
}
stage('构建镜像') {
unstash 'app'
sh "docker build --build-arg JAR_FILE=${JAR_FILE} --build-arg BUILD_ENV=${environment} -t ${harbor_url}/${harbor_project_name}/${imageName} ."
withCredentials([usernamePassword(credentialsId: "${harbor_auth_id}", passwordVariable: 'password', usernameVariable: 'username')]) {
sh "docker login -u ${username} -p ${password} ${harbor_url}"
sh "docker push ${harbor_url}/${harbor_project_name}/${imageName}"
}
sh "docker rmi -f ${imageName}"
sh "docker rmi -f ${harbor_url}/${harbor_project_name}/${imageName}"
echo "完成编译,构建镜像"
}
stage('运行镜像'){
echo "镜像已上传,请用k8s进行重启!"
}
}