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.
63 lines
2.6 KiB
63 lines
2.6 KiB
/**
|
|
* 定义一个流水线来自动化不同的阶段任务。
|
|
* 流水线中包含多个阶段(stages),每个阶段包含多个步骤(steps)。
|
|
* 每个阶段旨在完成特定的任务,例如拉取代码、打包、质量检测等。
|
|
*/
|
|
pipeline {
|
|
agent any // 指定流水线运行的代理,any表示可以在任何可用的agent上运行
|
|
|
|
environment {
|
|
harborUser = "admin"
|
|
harborPassword = "Harbor12345"
|
|
harborHost = "host.docker.internal:8077"
|
|
harborRepo = "spring"
|
|
}
|
|
|
|
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:通过ssh连接到目标服务器
|
|
steps {
|
|
echo 'ssh到服务器--成功' // 打印信息表示ssh连接阶段成功完成
|
|
}
|
|
}
|
|
stage('从Harbor仓库拉取镜像') { // 阶段7:从Harbor仓库拉取镜像到目标服务器
|
|
steps {
|
|
echo '从Harbor仓库拉取镜像--成功' // 打印信息表示镜像拉取阶段成功完成
|
|
}
|
|
}
|
|
stage('执行脚本启动容器') { // 阶段8:执行脚本在服务器上启动容器
|
|
steps {
|
|
echo '执行脚本启动容器--成功' // 打印信息表示容器启动阶段成功完成
|
|
}
|
|
}
|
|
}
|
|
}
|