| #!/usr/bin/env groovy | |
| // Copyright (C) 2020 The Android Open Source Project | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| // Declarative pipeline for building and testing a Gerrit plugin. | |
| // Parameters: | |
| // - name (optional): plugin name | |
| // - formatCheckId (optional): check-id for code-style validation | |
| // - buildCheckId (optional): check-id for build validation | |
| // | |
| // Minimalistic Jenkinsfile example (no checks): | |
| // pluginPipeline() | |
| // | |
| // Jenkinsfile example with code-style and build checks: | |
| // pluginPipeline(formatCheckId: 'myscheme:myformatcheckid', buildCheckId: 'myscheme:mybuildcheckid') | |
| // | |
| // P.S. The plugin project must have the corresponding checkers configured using | |
| // the Gerrit Checks GUI or API. The minimalistic example does not require any | |
| // checkers. | |
| def call(Map parm = [:]) { | |
| def pluginName = parm.name ?: "${env.GERRIT_PROJECT}".split('/').last() | |
| def formatCheck = parm.formatCheckId | |
| def buildCheck = parm.buildCheckId | |
| def extraPlugins = parm.extraPlugins ?: [] | |
| def extraModules = parm.extraModules ?: [] | |
| def extraGhRepos = parm.extraGhRepos ?: [] | |
| def gerritReviewBaseUrl = "https://gerrit.googlesource.com/a" | |
| def gerritReviewHostname = "gerrit.googlesource.com" | |
| def defaultGjfVersion = | |
| env.GERRIT_BRANCH ==~ /stable-3\.(11|12|13|14)/ ? '1.24.0' : '1.35.0' | |
| def gjfVersion = parm.gjfVersion ?: defaultGjfVersion | |
| def bashSetJavaCmd = "#!/bin/bash\n" + ". set-java.sh --branch $GERRIT_BRANCH" | |
| def bazeliskCmd = "${bashSetJavaCmd} && bazelisk" | |
| def bazeliskOptions = "--sandbox_tmpfs_path=/tmp" | |
| def gerritReviewCredentialsId = "gerrit.googlesource.com" | |
| def githubBaseUrl = "" | |
| echo "Starting pipeline for plugin '${pluginName}'" + (formatCheck ? " formatCheckId=${formatCheck}" : '') + (buildCheck ? " buildCheckId=${buildCheck}" : '') | |
| echo "Change : ${env.GERRIT_CHANGE_NUMBER}/${env.GERRIT_PATCHSET_NUMBER} '${env.GERRIT_CHANGE_SUBJECT}'" | |
| echo "Change URL: ${env.GERRIT_CHANGE_URL}" | |
| pipeline { | |
| options { skipDefaultCheckout true } | |
| agent { label 'bazel-debian' } | |
| stages { | |
| stage('Checkout') { | |
| steps { | |
| checkout scm | |
| withCredentials([usernamePassword(usernameVariable: "GH_GIT_USER", passwordVariable: "GH_GIT_PASS", credentialsId: env.GERRIT_CREDENTIALS_ID)]) { | |
| script { | |
| def scmUrl = sh(returnStdout: true, script: 'git config remote.origin.url').trim() | |
| def pluginScmHostname = new java.net.URI(scmUrl).getHost() | |
| githubBaseUrl = scmUrl.substring(0, scmUrl.lastIndexOf("/")) | |
| sh 'echo "machine ' + pluginScmHostname + ' login $GH_GIT_USER password $GH_GIT_PASS">> ~/.netrc' | |
| sh 'chmod 600 ~/.netrc' | |
| sh "git clone -b ${env.GERRIT_BRANCH} ${scmUrl}" | |
| sh "cd ${pluginName} && git fetch origin refs/changes/${BRANCH_NAME} && git config user.name jenkins && git config user.email jenkins@gerritforge.com && git merge FETCH_HEAD" | |
| } | |
| } | |
| withCredentials([usernamePassword(usernameVariable: "GS_GIT_USER", passwordVariable: "GS_GIT_PASS", credentialsId: gerritReviewCredentialsId)]) { | |
| sh 'echo "machine ' + gerritReviewHostname + ' login $GS_GIT_USER password $GS_GIT_PASS">> ~/.netrc' | |
| sh 'chmod 600 ~/.netrc' | |
| script { | |
| extraPlugins.each { plugin -> sh "git clone -b ${GERRIT_BRANCH} ${gerritReviewBaseUrl}/plugins/${plugin}" } | |
| extraModules.each { module -> sh "git clone -b ${GERRIT_BRANCH} ${gerritReviewBaseUrl}/modules/${module}" } | |
| extraGhRepos.each { repo -> sh "git clone -b ${GERRIT_BRANCH} ${githubBaseUrl}/${repo}.git" } | |
| } | |
| } | |
| } | |
| } | |
| stage('Formatting') { | |
| when { | |
| expression { formatCheck } | |
| } | |
| steps { | |
| gerritCheck (checks: ["${formatCheck}": 'RUNNING'], url: "${env.BUILD_URL}console") | |
| sh "${bashSetJavaCmd} && find ${pluginName} -name '*.java' | xargs /home/jenkins/format/google-java-format-${gjfVersion} -i" | |
| script { | |
| def formatOut = sh (script: "cd ${pluginName} && git status --porcelain", returnStdout: true) | |
| if (formatOut.trim()) { | |
| def files = formatOut.split('\n').collect { it.split(' ').last() } | |
| files.each { gerritComment path:it, message: 'Needs reformatting with GJF' } | |
| gerritCheck (checks: ["${formatCheck}": 'FAILED'], url: "${env.BUILD_URL}console") | |
| } else { | |
| gerritCheck (checks: ["${formatCheck}": 'SUCCESSFUL'], url: "${env.BUILD_URL}console") | |
| } | |
| } | |
| } | |
| } | |
| stage('build') { | |
| steps { | |
| script { if (buildCheck) { gerritCheck (checks: ["${buildCheck}": 'RUNNING'], url: "${env.BUILD_URL}console") } } | |
| sh 'git clone --recursive -b $GERRIT_BRANCH https://gerrit.googlesource.com/gerrit' | |
| dir ('gerrit') { | |
| script { | |
| ([pluginName] + extraPlugins + extraModules + extraGhRepos).each { plugin -> | |
| sh "cd plugins && ln -sf ../../${plugin} ." | |
| sh "if [ -f ../${plugin}/external_plugin_deps.MODULE.bazel ]; then echo 'include(\"//plugins/${plugin}:external_plugin_deps.MODULE.bazel\")' >> plugins/external_plugin_deps.MODULE.bazel; fi" | |
| } | |
| } | |
| sh "if [ -f ../${pluginName}/external_plugin_deps.bzl ]; then cd plugins && ln -sf ../../${pluginName}/external_plugin_deps.bzl .; fi" | |
| sh "if [ -f ../${pluginName}/external_package.json ]; then cd plugins && ln -sf ../../${pluginName}/external_package.json package.json; fi" | |
| sh "${bazeliskCmd} build ${bazeliskOptions} gerrit" | |
| sh "${bashSetJavaCmd} && ./polygerrit-ui/app/api/publish.sh --pack" | |
| sh "${bazeliskCmd} build ${bazeliskOptions} plugins/${pluginName}/..." | |
| sh "${bazeliskCmd} test ${bazeliskOptions} --test_env DOCKER_HOST=" + '$DOCKER_HOST' + " plugins/${pluginName}/..." | |
| } | |
| } | |
| } | |
| } | |
| post { | |
| success { | |
| script { if (buildCheck) { gerritCheck (checks: ["${buildCheck}": 'SUCCESSFUL'], url: "${env.BUILD_URL}console") } } | |
| gerritReview (labels: [Verified: 1], message: "Build console: ${env.BUILD_URL}console") | |
| } | |
| unstable { | |
| script { if (buildCheck) { gerritCheck (checks: ["${buildCheck}": 'FAILED'], url: "${env.BUILD_URL}console") } } | |
| gerritReview (labels: [Verified: -1], message: "Build console: ${env.BUILD_URL}console") | |
| } | |
| failure { | |
| script { if (buildCheck) { gerritCheck (checks: ["${buildCheck}": 'FAILED'], url: "${env.BUILD_URL}console") } } | |
| gerritReview (labels: [Verified: -1], message: "Build console: ${env.BUILD_URL}console") | |
| } | |
| } | |
| } | |
| } |