You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by GitBox <gi...@apache.org> on 2022/06/15 10:26:50 UTC

[GitHub] [sling-tooling-jenkins] kwin commented on a diff in pull request #8: Feature/parallel builds

kwin commented on code in PR #8:
URL: https://github.com/apache/sling-tooling-jenkins/pull/8#discussion_r897808804


##########
vars/slingOsgiBundleBuild.groovy:
##########
@@ -122,21 +161,117 @@ def defineStage(def globalConfig, def jobConfig, def jdkVersion, def isReference
         if ( isReferenceStage && jobConfig.archivePatterns ) {
             archiveArtifacts(artifacts: SlingJenkinsHelper.jsonArrayToCsv(jobConfig.archivePatterns), allowEmptyArchive: true)
         }
+        if ( isReferenceStage && goal == "deploy" && shouldDeploy ) {
+            // Stash the build results from the local deployment directory so we can deploy them on another node
+            stash name: 'local-snapshots-dir', includes: 'local-snapshots-dir/**'
+        }
     }
     
     def branchConfig = jobConfig?.branches?."$env.BRANCH_NAME" ?: [:]
-    if ( branchConfig.nodeLabel && branchConfig.nodeLabel != globalConfig.mainNodeLabel )
-        invocation = wrapInNode(invocation,branchConfig.nodeLabel)
-
 
     return {
-        stage("Build (Java ${jdkVersion}, ${goal})") {
-            invocation.call()
+        wrapInNode(branchConfig.nodeLabel ?: globalConfig.mainNodeLabel, {
+            echo "Running on node ${env.NODE_NAME}"
+            timeout(time: 30, unit: 'MINUTES') {
+                stage("Maven Build (Java ${jdkVersion}, ${goal})") {
+                    invocation.call()
+                }
+            }
+            if ( isReferenceStage ) {
+                // SonarQube (must be wrapped in same node)
+                if ( jobConfig.sonarQubeEnabled ) {
+                    stage('Analyse with SonarCloud') {
+                        timeout(time: 30, unit: 'MINUTES') {
+                            analyseWithSonarCloud(globalConfig, jobConfig)
+                        }
+                    }
+                }
+            }
+        })
+    }
+}
+
+def analyseWithSonarCloud(def globalConfig, def jobConfig) {
+    // this might fail if there are no jdks defined, but that's always an error
+    // also, we don't activate any Maven publisher since we don't want this part of the
+    // build tracked, but using withMaven(...) allows us to easily reuse the same
+    // Maven and JDK versions
+    def additionalMavenParams = additionalMavenParams(jobConfig)
+    def isPrBuild = env.BRANCH_NAME.startsWith("PR-")
+
+    // As we don't have the global SonarCloud conf for now, we can't use #withSonarQubeEnv so we need to set the following props manually
+    def sonarcloudParams="-Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=apache -Dsonar.projectKey=apache_${jobConfig.repoName} -Pjacoco-report -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco-merged/jacoco.xml ${jobConfig.sonarQubeAdditionalParams}"
+    if ( jobConfig.sonarQubeUseAdditionalMavenParams ) {
+        sonarcloudParams="${sonarcloudParams} ${additionalMavenParams}"
+    }
+    // Params are different if it's a PR or if it's not
+    // Note: soon we won't have to handle that manually, see https://jira.sonarsource.com/browse/SONAR-11853
+    if ( isPrBuild ) {
+        sonarcloudParams="${sonarcloudParams} -Dsonar.pullrequest.branch=${CHANGE_BRANCH} -Dsonar.pullrequest.base=${CHANGE_TARGET} -Dsonar.pullrequest.key=${CHANGE_ID}"
+    } else if ( isOnMainBranch() ) {
+        sonarcloudParams="${sonarcloudParams} -Dsonar.branch.name=${BRANCH_NAME}"
+    }
+    static final String SONAR_PLUGIN_GAV = 'org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184'
+    // Alls params are set, let's execute using #withCrendentials to hide and mask Robert's token
+    withCredentials([string(credentialsId: 'sonarcloud-token-rombert', variable: 'SONAR_TOKEN')]) {
+        // always build with Java 11 (that is the minimum version supported: https://sonarcloud.io/documentation/appendices/end-of-support/)
+        withMaven(maven: globalConfig.mvnVersion,
+            jdk: jenkinsJdkLabel(11, globalConfig),
+            publisherStrategy: 'EXPLICIT') {
+                try {
+                     sh  "mvn ${SONAR_PLUGIN_GAV}:sonar ${sonarcloudParams}"
+                } catch ( Exception e ) {
+                    // TODO - we should check the actual failure cause here, but see
+                    // https://stackoverflow.com/questions/55742773/get-the-cause-of-a-maven-build-failure-inside-a-jenkins-pipeline/55744122
+                    echo "Marking build unstable due to mvn sonar:sonar failing. See https://cwiki.apache.org/confluence/display/SLING/SonarCloud+analysis for more info."
+                    currentBuild.result = 'UNSTABLE'
+                }
+        }
+    }
+}
+
+def deployToNexus(def globalConfig) {
+    node('nexus-deploy') {
+        timeout(60) {
+            echo "Running on node ${env.NODE_NAME}"
+            // first clear workspace
+            deleteDir()
+            // Nexus deployment needs pom.xml
+            checkout scm
+            // Unstash the previously stashed build results.
+            unstash name: 'local-snapshots-dir'
+            // https://www.mojohaus.org/wagon-maven-plugin/merge-maven-repos-mojo.html
+            static final String WAGON_PLUGIN_GAV = "org.codehaus.mojo:wagon-maven-plugin:2.0.2"
+            String mavenArguments = "${WAGON_PLUGIN_GAV}:merge-maven-repos -Dwagon.target=https://repository.apache.org/content/repositories/snapshots -Dwagon.targetId=apache.snapshots.https -Dwagon.source=file:${env.WORKSPACE}/local-snapshots-dir"
+            withMaven(maven: globalConfig.mvnVersion,
+                     jdk: jenkinsJdkLabel(11, globalConfig),
+                     publisherStrategy: 'EXPLICIT') {
+                        sh "mvn ${mavenArguments}"
+            }
         }
     }
 }
 
-def wrapInNode(Closure invocation, def nodeLabel) {
+boolean getShouldDeploy() {
+    // check branch name
+    if ( !isOnMainBranch() ) {
+        return false
+    }
+    // check version
+    def mavenPom = readMavenPom()
+    def mavenVersion = mavenPom.version ?: mavenPom.parent.version
+    def isSnapshot = mavenVersion.endsWith('-SNAPSHOT')
+    if ( !isSnapshot ) {
+        return false
+    }
+    return true
+}
+
+boolean isOnMainBranch() {
+    return env.BRANCH_NAME == 'feature/test-parallel-builds'

Review Comment:
   Before being merged this needs to be reverted back to `master`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org