安装插件
- Pipeline
- Pipeline: Stage View Plugin
创建任务
配置
- demo
开始实践
拉取git仓库代码
checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'http://178.119.30.133:8929/root/mytest.git']])
- 通过SonarQube做质量检测
sh '/var/jenkins_home/sonar-scanner/bin/sonar-scanner -Dsonar.source=./ -Dsonar.projectname=${JOB_NAME} -Dsonar.projectKey=${JOB_NAME} -Dsonar.login=6c99d1bafedaa4bad18fd63fc9975515fbeb4eab'
- 通过Docker制作自定义镜像
sh 'docker build -t ${JOB_NAME}:$tag .'
- 将自定义镜像推送到Harbor
sh '''docker login -u $harborUser -p $harborPasswd $harborAddressdocker tag$JOB_NAME:$tag$harborAddress/$harborRepo/$JOB_NAME:$tagdocker push $harborAddress/$harborRepo/$JOB_NAME:$tag'''
- 通过Publish Over SSH通知目标服务器
sshPublisher(publishers: [sshPublisherDesc(configName: 'test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'deploy.sh $harborAddress $harborRepo $JOB_NAME $tag $port ', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
- Jenkinsfile
pipeline {agent anyenvironment {harborUser = 'admin'harborPasswd = 'Harbor12345'harborAddress = '178.119.30.133:80'harborRepo = 'repo'}stages {stage('拉取git仓库代码') {steps {checkout scmGit(branches: [[name: '$tag']], extensions: [], userRemoteConfigs: [[url: 'http://178.119.30.133:8929/root/mytest.git']])}}stage('通过SonarQube做质量检测') {steps {sh '/var/jenkins_home/sonar-scanner/bin/sonar-scanner -Dsonar.source=./ -Dsonar.projectname=${JOB_NAME} -Dsonar.projectKey=${JOB_NAME} -Dsonar.login=6c99d1bafedaa4bad18fd63fc9975515fbeb4eab'}}stage('通过Docker制作自定义镜像') {steps {sh 'docker build -t ${JOB_NAME}:$tag .'}}stage('将自定义镜像推送到Harbor') {steps {sh '''docker login -u $harborUser -p $harborPasswd $harborAddressdocker tag$JOB_NAME:$tag$harborAddress/$harborRepo/$JOB_NAME:$tagdocker push $harborAddress/$harborRepo/$JOB_NAME:$tag'''}}stage('通过Publish Over SSH通知目标服务器') {steps {sshPublisher(publishers: [sshPublisherDesc(configName: 'test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "deploy.sh $harborAddress $harborRepo $JOB_NAME $tag $port", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])}}}}