jenkins流水線自動構建配置

猝死的路上發表於2024-07-15
pipeline {
    agent any
    
    tools {
        jdk 'jdk8'
    }
    
    environment {
        harborUser = "admin"
        harborPassword = "Harbor12345"
        harborAddress = "harbor地址"
        harborRepo = "倉庫名"
        jobName = "打好的docker映象名"
    }

    stages {
        
        stage('check') {
            steps {
                checkout scmGit(branches: [[name: '*/分支名']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitlab-login-user', url: 'gitlab地址']])
            }
        }
        
        stage('mvn') {
            steps {
                sh 'mvn clean package -DskipTests'
            }
        }
        
        stage("SonarQube analysis") {
            steps{
                withSonarQubeEnv('SonarQube10.6') {
                    sh 'sonar-scanner -Dsonar.projectKey=要掃描的工程 -Dsonar.projectName=工程名 -Dsonar.projectVersion=1.0.0 -Dsonar.language=java -Dsonar.modules=模組名 -D模組名.sonar.projectName=模組名 -D模組名.sonar.sources=src -Dhntt-模組名.sonar.projectBaseDir=模組根路徑 -Dsonar.java.binaries=target'
                }
            }
        }
        
        stage('docker') {
            steps {
                sh '''
                cd dockerfile檔案所在目錄
                docker build -t ${jobName}:${date} .'''
            }
        }
        
       
        stage('harbor') {
            steps {
                sh '''
                docker login -u ${harborUser} -p ${harborPassword} ${harborAddress}
                docker push ${jobName}:${date}
                '''
            }
        }
        
        
    }
}

相關文章