// declarative pipeline pipeline { agent { node { label 'oem-linux' } } parameters { booleanParam(name: 'UPDATE_NM_PARTS', defaultValue: true, description: 'update the netmodule submodules') booleanParam(name: 'UPDATE_COMMUNITY_PARTS', defaultValue: false, description: 'update the the community submodules') } environment { SUBMODULE_VERION_FILE = "submodule_revisions" } options { timeout(time: 1, unit: 'HOURS') buildDiscarder( logRotator(numToKeepStr: '5', daysToKeepStr: '5', artifactNumToKeepStr: '5', artifactDaysToKeepStr: '5' ) ) disableConcurrentBuilds() } stages { stage('prepare') { steps { script { printJobParameters() prepareUpdate() } } } stage('update to head') { steps { script { updateSubmodules(params.UPDATE_NM_PARTS, params.UPDATE_COMMUNITY_PARTS) } } post { success { archiveArtifacts(artifacts: "${env.SUBMODULE_VERION_FILE}", onlyIfSuccessful: false) sh "rm -f ${env.SUBMODULE_VERION_FILE}" } } } stage('commit') { steps { commitChanges(params.UPDATE_NM_PARTS, params.UPDATE_COMMUNITY_PARTS) } } } // stages } def printJobParameters() { println "----------------------------------\n\ Job Parameters:\n\ ----------------------------------\n\ UPDATE_NM_PARTS = ${params.UPDATE_NM_PARTS}\n\ UPDATE_COMMUNITY_PARTS = ${params.UPDATE_COMMUNITY_PARTS}\n\ ----------------------------------\n" } def prepareUpdate() { sh 'git submodule update --init' // init submodules used if first checkout def userId = "${currentBuild.getBuildCauses()[0].userId}" def userName = "${currentBuild.getBuildCauses()[0].userName}" if("${userId}" == "null") { userId = "downstream" userName = "Jenkins" } env.TRIGGERED_USER = "${userName} (userId=${userId})" def notNmUpdate = "${!params.UPDATE_NM_PARTS}" def notCommunityUpdate = "${!params.UPDATE_COMMUNITY_PARTS}" if(notNmUpdate.toBoolean() && notCommunityUpdate.toBoolean()) { error("Nothing to update selected - both parameters are false") } } def updateSubmodules(isNmUpdate, isCommunityUpdate) { if(isNmUpdate) { sh(script: "git submodule update --remote --rebase meta-netmodule-*") } if(isCommunityUpdate) { sh(script: "git submodule update --remote --rebase \$(git submodule status | grep -v \"meta-netmodule-*\" | sed 's/^ *//g' | cut -d' ' -f2)") } submoduleStatus = sh(returnStdout: true, script: "git submodule status").trim() // print submodule hashes to jenkins log println "${submoduleStatus}" writeFile(file: "${env.SUBMODULE_VERION_FILE}", text: "${submoduleStatus}") } def commitChanges(isNmUpdate, isCommunityUpdate) { String updatedLayers = "" if(isNmUpdate) { updatedLayers += "netmodule"} if(isNmUpdate && isCommunityUpdate) { updatedLayers += " and " } if(isCommunityUpdate) { updatedLayers += "community" } sh(script: "git checkout ${env.BRANCH_NAME}") sh(script: "git add -u") stagedData = sh(returnStdout: true, script: "git diff --cached").trim() if("${stagedData}" == "") { println "everything up to date, nothing to commit" return } sh(script: "git commit -m \"submodules: updated ${updatedLayers} hashes, triggered by ${env.TRIGGERED_USER}\" && git push") }