You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
2.6 KiB
55 lines
2.6 KiB
/**
|
|
* 定义一个流水线来自动化不同的阶段任务。
|
|
* 流水线中包含多个阶段(stages),每个阶段包含多个步骤(steps)。
|
|
* 每个阶段旨在完成特定的任务,例如拉取代码、打包、质量检测等。
|
|
*/
|
|
pipeline {
|
|
agent any // 指定流水线运行的代理,any表示可以在任何可用的agent上运行
|
|
|
|
environment {
|
|
harborUser = "admin"
|
|
harborPassword = "Harbor12345"
|
|
harborHost = "host.docker.internal:8077"
|
|
harborRepo = "spring"
|
|
hostPort = 8088
|
|
containerPort = 8080
|
|
}
|
|
|
|
stages { // 定义流水线中的各个阶段
|
|
stage('拉取git仓库代码') { // 阶段1:从Git仓库拉取代码
|
|
steps {
|
|
checkout scmGit(branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[url: 'https://git.mashibing.com/msb_134187/ljj_spring_test.git']])
|
|
}
|
|
}
|
|
stage('maven') {
|
|
steps {
|
|
sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'
|
|
}
|
|
}
|
|
|
|
stage('sonar质量检测') { // 阶段3:使用Sonar进行代码质量检测
|
|
steps {
|
|
sh '/var/jenkins_home/sonar-scanner-linux/bin/sonar-scanner -Dsonar.perjectname=${JOB_NAME} -Dsonar.projectKey=${JOB_NAME} -Dsonar.sources=./ -Dsonar.java.binaries=./target/ -Dsonar.login=sqa_7f6128c142f9f30caac0a5141c46d4223f71e2f1'
|
|
}
|
|
}
|
|
stage('构建镜像') { // 阶段4:构建Docker镜像
|
|
steps {
|
|
sh '''mv ./target/*.jar ./docker
|
|
docker build -t ${JOB_NAME}:${tag} ./docker'''
|
|
}
|
|
}
|
|
stage('推送镜像到Harbor') { // 阶段5:将构建的镜像推送至Harbor仓库
|
|
steps {
|
|
sh '''docker login -u ${harborUser} -p ${harborPassword} ${harborHost}
|
|
docker tag ${JOB_NAME}:$tag ${harborHost}/${harborRepo}/${JOB_NAME}:$tag
|
|
docker push ${harborHost}/${harborRepo}/${JOB_NAME}:$tag'''
|
|
}
|
|
}
|
|
stage('ssh到目标服务器执行脚本启动容器') { // 阶段6:执行脚本在服务器上启动容器
|
|
steps {
|
|
sshPublisher(publishers: [sshPublisherDesc(configName: 'local', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'deploy.sh $harborHost $harborRepo $JOB_NAME $tag $hostPort $containerPort', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
|
|
}
|
|
}
|
|
}
|
|
}
|