开启 agent
root@jenkins:~/learning-jenkins-cicd/07-jenkins-agents# docker-compose -f docker-compose-inbound-agent.yml up -d
Jenkins配置添加
pipeline { agent { label 'docker-jnlp-agent' }parameters {booleanParam(name:'pushImage', defaultValue: 'true', description: 'Push Image to Harbor?')} tools {maven 'Maven-3.8.8'}triggers {GenericTrigger(genericVariables: [[key: 'ref', value: '$.ref']],token: 'fClZ0e/kTcqL2ARh7YqxW/3ndOCZA2SqfKnRTLat',causeString: 'Triggered on $ref',printContributedVariables: true,printPostContent: true)} environment {codeRepo="http://192.168.1.50/root/spring-boot-helloWorld.git"harborServer='harbor.luohw.net'projectName='spring-boot-helloworld'imageUrl="${harborServer}/ikubernetes/${projectName}"imageTag='latest'}stages {stage('Source') {steps {git branch: 'main', credentialsId: 'gitlab-root-credential', url: "${codeRepo}"}}stage('Build') {steps {sh 'mvn -B -DskipTests clean package'}}stage('Test') {steps {sh 'mvn test'}}stage("SonarQube Analysis") {steps {withSonarQubeEnv('SonaQube-Server') {sh 'mvn sonar:sonar'}}}stage("Quality Gate") {steps {timeout(time: 30, unit: 'MINUTES') {waitForQualityGate abortPipeline: true}}} stage('Build Docker Image') {agent { label 'master' }steps {sh 'docker image build . -t "${imageUrl}:${imageTag}"'// input(message: '镜像已经构建完成,是否要推送?')} }stage('Push Docker Image') {agent { label 'master' }when {expression { params.pushImage }}steps {withCredentials([usernamePassword(credentialsId: 'harbor-user-credential', passwordVariable: 'harborUserPassword', usernameVariable: 'harborUserName')]) {sh "docker login -u ${env.harborUserName} -p ${env.harborUserPassword} ${harborServer}"sh "docker image push ${imageUrl}:${imageTag}"}} } }post{success{qyWechatNotification failNotify: true, webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5530d220-0983-490e-ada5-a74fa66570c8'}failure{qyWechatNotification failNotify: true, webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5530d220-0983-490e-ada5-a74fa66570c8'}}
}
192.168.1.51:50000
添加pod模版
kube-agent222
maven:3.8.6-eclipse-temurin-17-alpine
挂载路径
/root/.m2
配置pod模版
编辑流水线
pipeline {agent {kubernetes {inheritFrom 'kube-maven'}}parameters {booleanParam(name:'pushImage', defaultValue: 'true', description: 'Push Image to Harbor?')} tools {maven 'Maven-3.8.8'}triggers {GenericTrigger(genericVariables: [[key: 'ref', value: '$.ref']],token: 'fClZ0e/kTcqL2ARh7YqxW/3ndOCZA2SqfKnRTLat',causeString: 'Triggered on $ref',printContributedVariables: true,printPostContent: true)} environment {codeRepo="http://192.168.1.50/root/spring-boot-helloWorld.git"harborServer='hub.magedu.com'projectName='spring-boot-helloworld'imageUrl="${harborServer}/ikubernetes/${projectName}"imageTag='latest'}stages {stage('Source') {steps {git branch: 'main', credentialsId: 'gitlab-root-credential', url: "${codeRepo}"}}stage('Build') {steps {container('maven') {sh 'mvn -B -DskipTests clean package'}}}stage('Test') {steps {container('maven') {sh 'mvn test'}}}stage("SonarQube Analysis") {steps {container('maven') { withSonarQubeEnv('SonarQube-Server') {sh 'mvn sonar:sonar'}}}}stage("Quality Gate") {steps {timeout(time: 30, unit: 'MINUTES') {waitForQualityGate abortPipeline: true}}} }post{success{qyWechatNotification failNotify: true, webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c79e3215-d39f-44a7-a760-fa0ab63ca46b'}failure{qyWechatNotification failNotify: true, webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c79e3215-d39f-44a7-a760-fa0ab63ca46b'}}
}