流水线文件
由于要进行sonar代码质量分析 所以主pom文件需要进行如下配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <properties> <sonar.jacoco.reportPaths>${ PWD}/./target/jacoco.exec</sonar.jacoco.reportPaths> <sonar.groovy.binaries>target/classes</sonar.groovy.binaries> </properties> <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.2</version> <configuration> <append>true</append> </configuration> <executions> <execution> <id>agent-for-ut</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>agent-for-it</id> <goals> <goal>prepare-agent-integration</goal> </goals> </execution> <execution> <id>jacoco-site</id> <phase>verify</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> <plugin> <groupId>org.sonarsource.scanner.maven</groupId> <artifactId>sonar-maven-plugin</artifactId> <version>3.6.0.1398</version> </plugin> </plugins> </build>
|
JenkinsFile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| #固定写法 pipeline { agent { node { label 'maven' }
} #流程 stages { stage('拉取代码') { steps { git(url: 'https://github.com/flover4/gulimall_src_leifengyang.git', credentialsId: 'github-id', branch: 'master', changelog: true, poll: false) sh 'echo 正在构建 $PROJECT_NAME 版本号:$PROJECT_VERSION 将会提交给 $REGISTRY 镜像仓库' container('maven') { #记得要将maven的配置放在代码文件下 /mvn-settings.xml 主要是aliyun镜像 以及jdk8编译 sh 'mvn clean install -Dmaven.test.skip=true -gs `pwd`/mvn-settings.xml' }
} } #记得要将maven的配置放在代码文件下 /mvn-settings.xml 主要是aliyun镜像 以及jdk8编译 stage('sonar代码质量分析') { steps { container('maven') { withCredentials([string(credentialsId: "$SONAR_CREDENTIAL_ID", variable: 'SONAR_TOKEN')]) { withSonarQubeEnv('sonar') { sh 'echo 当前目录 `pwd`' sh "mvn sonar:sonar -gs `pwd`/mvn-settings.xml -Dsonar.branch=$BRANCH_NAME -Dsonar.login=$SONAR_TOKEN" }
}
timeout(time: 1, unit: 'HOURS') { waitForQualityGate true }
}
} } stage('构建镜像-推送镜像') { steps { container('maven') { sh 'mvn -Dmaven.test.skip=true -gs `pwd`/mvn-settings.xml clean package' sh 'cd $PROJECT_NAME && docker build -f Dockerfile -t $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER .' withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$DOCKER_CREDENTIAL_ID" ,)]) { sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin' sh 'docker tag $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:latest ' sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:latest ' }
}
} } stage('部署到k8s') { steps { input(id: "deploy-to-dev-$PROJECT_NAME", message: "是否将 $PROJECT_NAME 部署到集群中?") kubernetesDeploy(configs: "$PROJECT_NAME/deploy/**", enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID") } } stage('发布版本'){ when{ expression{ return params.PROJECT_VERSION =~ /v.*/ } } steps { container ('maven') { input(id: 'release-image-with-tag', message: '发布当前版本镜像吗?') sh 'docker tag $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:$PROJECT_VERSION ' sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:$PROJECT_VERSION ' withCredentials([usernamePassword(credentialsId: "$GITHUB_CREDENTIAL_ID", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) { sh 'git config --global user.email "liml.hz@gmail.com" ' sh 'git config --global user.name "flover4" ' sh 'git tag -a $PROJECT_NAME-$PROJECT_VERSION -m "$PROJECT_VERSION" ' sh 'git push http://$GIT_USERNAME:$GIT_PASSWORD@github.com/$GITHUB_ACCOUNT/gulimall_src_leifengyang.git --tags --ipv4' }
} } }
} #环境变量 environment { DOCKER_CREDENTIAL_ID = 'dockerhub-id' #Id是在k8s中创建的凭证 GITHUB_CREDENTIAL_ID = 'github-id' KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig' REGISTRY = 'docker.io' DOCKERHUB_NAMESPACE = 'flover4' GITHUB_ACCOUNT = 'flover4' SONAR_CREDENTIAL_ID = 'sonar-qube' BRANCH_NAME = 'master' } #参数化构建 parameters { string(name: 'PROJECT_VERSION', defaultValue: 'v0.0Beta', description: '项目版本') string(name: 'PROJECT_NAME', defaultValue: 'gulimall-gateway', description: '构建模块') } }
|
作者声明