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.
ljj_spring_test/Jenkinsfile

85 lines
3.5 KiB

/**
* 定义一个流水线来自动化不同的阶段任务。
* 流水线中包含多个阶段(stages),每个阶段包含多个步骤(steps)。
* 每个阶段旨在完成特定的任务,例如拉取代码、打包、质量检测等。
*/
pipeline {
agent any // 指定流水线运行的代理,any表示可以在任何可用的agent上运行
environment {
// harborUser = "admin"
// harborPassword = "Harbor12345"
// harborHost = "host.docker.internal:8077"
// harborRepo = "spring"
aliyunUser = "yying01234"
aliyunPassword = "a123456#"
aliyunHost = "registry.cn-hangzhou.aliyuncs.com"
aliyunRepo = "night_yy_php"
}
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 ${aliyunUser} -p ${aliyunPassword} ${aliyunHost}
docker tag ${JOB_NAME}:$tag ${aliyunHost}/${aliyunRepo}/${JOB_NAME}:$tag
docker push ${aliyunHost}/${aliyunRepo}/${JOB_NAME}:$tag'''
}
}
stage('ssh到目标服务器执行脚本启动容器') { // 阶段6:执行脚本在服务器上启动容器
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: 'local', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "source ~/.bash_profile && deploy.sh $aliyunHost $aliyunRepo $JOB_NAME $tag $host_port $container_port", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])
}
}
}
// https://oapi.dingtalk.com/robot/send?access_token=5f5b130a0e9904359b3c1c21760d5cbf6802e387d52bacdf873fac531e731bba
post {
success {
dingtalk(
robot: 'jenkins-dingding',
type: 'MARKDOWN',
title: 'success: ${JOB_NAME}',
text: ["- 成功构建: ${JOB_NAME}! \n - 版本: ${tag} \n - 持续时间: ${currentBuild.durationString}"],
)
}
failure {
dingtalk(
robot: 'jenkins-dingding',
type: 'MARKDOWN',
title: 'fail: ${JOB_NAME}',
text: ["- 失败构建: ${JOB_NAME}! \n - 版本: ${tag} \n - 持续时间: ${currentBuild.durationString}"],
)
}
}
}