1流水线部署
1.流水线文本名Jenkinsfile,将流水线放入gitlab远程仓库代码里面
2构建参数
2pipeline脚本
Jenkinsfile文件内容
pipeline {
agent any
environment {
key="value"
}
stages {
stage("拉取git仓库代码") {
steps {
deleteDir()
checkout scmGit(branches: [[name: "$tag"]], extensions: [], userRemoteConfigs: [[url: 'http://192.168.88.120:8929/root/test.git']])
echo '拉取git仓库代码 --SUCCESS'
}
}
stage("通过maven构建项目") {
steps {
sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'
echo '通过maven构建项目 --SUCCESS'
}
}
stage("通过sonarqube做代码质量检测") {
steps {
sh '/var/jenkins_home/sonar-scanner/bin/sonar-scanner -Dsonar.source=./ -Dsonar.projectname=${JOB_NAME} -Dsonar.login=2f0c3fe097389f621ebe683db16dc83931cb3502 -Dsonar.projectKey=${JOB_NAME} -Dsonar.java.binaries=./target/'
echo '通过sonarqube做代码质量检测 --SUCCESS'
}
}
stage("通过docker制作自定义镜像") {
steps {
sh '''mv target/*.jar docker/
cd docker/
docker build -t ${JOB_NAME}:$tag ./'''
echo '通过docker制作自定义镜像 --SUCCESS'
}
}
stage("将自定义镜像推送到harbor仓库") {
steps {
sh '''docker login -u admin -p Harbor12345 192.168.88.121:80
docker tag ${JOB_NAME}:$tag 192.168.88.121:80/repo/${JOB_NAME}:$tag
docker push 192.168.88.121:80/repo/${JOB_NAME}:$tag'''
echo '将自定义镜像推送到harbor仓库 --SUCCESS'
}
}
stage("远程仓库下载并启动容器") {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: 'test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "docker.sh 192.168.88.121:80 repo ${JOB_NAME} $tag $container_port $host_port", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
echo '远程仓库下载并启动容器 --SUCCESS'
}
}
}
}
3.优化后的pipeline代码
pipeline {
agent any
environment {
harborUser = "admin"
harborPasswd = "Harbor12345"
harborAddress = "192.168.88.121:80"
harborRepo = "repo"
}
stages {
stage("拉取git仓库代码") {
steps {
deleteDir()
checkout scmGit(branches: [[name: "$tag"]], extensions: [], userRemoteConfigs: [[url: 'http://192.168.88.120:8929/root/test.git']])
echo '拉取git仓库代码 --SUCCESS'
}
}
stage("通过maven构建项目") {
steps {
sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'
echo '通过maven构建项目 --SUCCESS'
}
}
stage("通过sonarqube做代码质量检测") {
steps {
sh '/var/jenkins_home/sonar-scanner/bin/sonar-scanner -Dsonar.source=./ -Dsonar.projectname=${JOB_NAME} -Dsonar.login=2f0c3fe097389f621ebe683db16dc83931cb3502 -Dsonar.projectKey=${JOB_NAME} -Dsonar.java.binaries=./target/'
echo '通过sonarqube做代码质量检测 --SUCCESS'
}
}
stage("通过docker制作自定义镜像") {
steps {
sh '''mv target/*.jar docker/
cd docker/
docker build -t ${JOB_NAME}:${tag} ./'''
echo '通过docker制作自定义镜像 --SUCCESS'
}
}
stage("将自定义镜像推送到harbor仓库") {
steps {
sh '''docker login -u ${harborUser} -p ${harborPasswd} ${harborAddress}
docker tag ${JOB_NAME}:$tag ${harborAddress}/${harborRepo}/${JOB_NAME}:$tag
docker push ${harborAddress}/${harborRepo}/${JOB_NAME}:${tag}'''
echo '将自定义镜像推送到harbor仓库 --SUCCESS'
}
}
stage("远程仓库下载并启动容器") {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: 'test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "docker.sh ${harborAddress} ${harborRepo} ${JOB_NAME} ${tag} ${container_port} ${host_port}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
echo '远程仓库下载并启动容器 --SUCCESS'
}
}
}
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » Jenkins流水线pipeline--基于上一章的工作流程
发表评论 取消回复