pipeline{
agent {
node{
label 'maven'
}
}
stages {
stage('clone code') {
agent none
steps {
container('base') {
git(url:'http://xxx.git',credentialsId: 'gitlab', changelog: true, poll: false, branch: 'master')
}
}
}
stage('code analysis') {
agent none
when{
environment name: 'NEED_CODE_ANALYSIS', value: 'true'
}
steps {
container('maven') {
withCredentials([string(credentialsId : 'sonar', variable: 'SONAR_TOKEN' ,)]) {
withSonarQubeEnv('sonar') {
sh 'mvn compile sonar:sonar -Dsonar.login=$SONAR_TOKEN'
}
}
timeout(unit: 'HOURS', activity: ture, time: 1){
waitForQualityGate 'false'
}
}
}
}
stage('build & push') {
agent none
steps {
container('maven') {
dir('指定项目下的某模块的目录'){
sh 'mvn -e clean package - U'
withCredentials([usernamePassword(credentialsId: 'harbor',passwordVariable: 'HARBOR_PASSWORD', usernameVariable: 'H_USERNAMES' ,)]){
sh 'echo "$HARBOR_PASSWORD" | docker login harbor地址 -u "$H_USERNAMES" --password-stdin'
}
sh '''docker build --add-host 私服域名:ip地址 -t $REGISTRY/$HARBOR_NAMESPACE/$ENV/$APP_NAME:$BUILD_NUMBER .
docker push $REGISTRY/$HARBOR_NAMESPACE/$ENV/$APP_NAME:$BUILD_NUMBER'''
}
}
}
}
stage('deploy') {
agent none
steps {
container('maven') {
dir('指定项目下的某模块的目录'){
withCredentials([kubeconfigFile(credentialsId: 'kubesphere-名',variable: 'KUBECONFIG' ,)]){
sh '''
envsbst < deploy.yaml | kubectl apply -f -'''
}
}
}
}
}
}
environment {
SONAR_CREDENTIAL_ID = 'sonar'
HARBOR_NAMESPACE= 'xxxx'
ENV = 'dev'
APP_NAME = 'deployment.yaml文件中配置名称'
}
}
jenkins 流水线构建项目
最新推荐文章于 2024-05-30 20:52:53 发布