|
| 1 | +/* bump-jenkins-job automates the process of checking for updates to Jenkins plugins, updating the plugin versions in a configuration file, |
| 2 | +pushing the changes to a Git repository, and opening a pull request for review. |
| 3 | + |
| 4 | +Plugin Update Process: |
| 5 | + |
| 6 | +This job reads a list of plugins from the file plugins.txt in the repository. |
| 7 | +It iterates over each plugin, checks for updates and if a newer version is available, it updates the version in the file. |
| 8 | +For each plugin, it fetches the latest version by querying a URL based on the Jenkins plugin repository structure. |
| 9 | +If an update is found, it modifies the plugins.txt file to reflect the new version. |
| 10 | +The updates in the plugins.txt file are committed and pushed to the pr_branch. |
| 11 | +It also opens a pull request with the updated plugin versions. */ |
| 12 | + |
| 13 | +repo = "coreos/fedora-coreos-pipeline" |
| 14 | +fork_repo = "coreosbot-releng/fedora-coreos-pipeline" |
| 15 | +botCreds = "github-coreosbot-releng-token-username-password" |
| 16 | +pr_branch = "JenkinsPluginsUpdate" |
| 17 | + |
| 18 | +/* Function to extract the plugin version from the plugin URL */ |
| 19 | +def getVersionFromPluginUrl(pluginUrl) { |
| 20 | + /* example url : https://updates.jenkins.io/download/plugins/${pluginName}/latest/${pluginName}.hpi */ |
| 21 | + def parts = pluginUrl.split("/") |
| 22 | + def pluginVersion |
| 23 | + if (parts.size() >= 4) { |
| 24 | + def groupId = parts[-3] |
| 25 | + pluginVersion = parts[-2] |
| 26 | + } else { |
| 27 | + error("Unable to extract plugin version from the URL.") |
| 28 | + } |
| 29 | + return pluginVersion |
| 30 | +} |
| 31 | + |
| 32 | +node { |
| 33 | + checkout scm: [ |
| 34 | + $class: 'GitSCM', |
| 35 | + branches: [[name: "${pr_branch}"]], |
| 36 | + userRemoteConfigs: [[url: "https://github.com/${fork_repo}.git"]], |
| 37 | + extensions: [[$class: 'WipeWorkspace']] |
| 38 | + ] |
| 39 | + pipeutils = load("utils.groovy") |
| 40 | + |
| 41 | + properties([ |
| 42 | + pipelineTriggers([ |
| 43 | + /* Schedule to check once a month */ |
| 44 | + cron('H H 1 * *') |
| 45 | + ]), |
| 46 | + buildDiscarder(logRotator( |
| 47 | + numToKeepStr: '100', |
| 48 | + artifactNumToKeepStr: '100' |
| 49 | + )), |
| 50 | + durabilityHint('PERFORMANCE_OPTIMIZED') |
| 51 | + ]) |
| 52 | + |
| 53 | + try { |
| 54 | + shwrap(""" |
| 55 | + git config --global user.name "CoreOS Bot" |
| 56 | + git config --global user.email " [email protected]" |
| 57 | + """) |
| 58 | + |
| 59 | + def pluginslist |
| 60 | + def pluginsToUpdate = [:] |
| 61 | + def plugins_lockfile = "jenkins/controller/plugins.txt" |
| 62 | + |
| 63 | + stage("Read plugins.txt") { |
| 64 | + /* Clone the repository and switch to the '${pr_branch}' branch */ |
| 65 | + shwrapCapture(""" |
| 66 | + git clone --depth=1 --branch ${pr_branch} https://github.com/${fork_repo}.git |
| 67 | + cd fedora-coreos-pipeline |
| 68 | + git remote add upstream https://github.com/${repo}.git |
| 69 | + git fetch upstream |
| 70 | + git reset --hard upstream/main |
| 71 | + """) |
| 72 | + |
| 73 | + /* Read the plugins from the lockfile */ |
| 74 | + pluginslist = shwrapCapture("grep -v ^# ${plugins_lockfile}").split('\n') |
| 75 | + } |
| 76 | + |
| 77 | + stage("Check for plugin updates") { |
| 78 | + def pluginUrl |
| 79 | + def pluginsUpdateList = [] |
| 80 | + pluginslist.each { plugin -> |
| 81 | + def parts = plugin.split(':') |
| 82 | + if (parts.size() != 2) { |
| 83 | + error("Invalid plugin format: ${plugin}") |
| 84 | + } else { |
| 85 | + def pluginName = parts[0] |
| 86 | + def currentVersion = parts[1] |
| 87 | + |
| 88 | + /* Retrieve the download URL for the most recent version of a Jenkins plugin from the Jenkins update center. |
| 89 | + After following all redirects, curl prints the final URL to stdout. The final URL is captured in the pluginUrl variable for further use. */ |
| 90 | + pluginUrl = shwrapCapture("curl -Ls -I -f -o /dev/null -w '%{url_effective}' https://updates.jenkins.io/download/plugins/${pluginName}/latest/${pluginName}.hpi") |
| 91 | + |
| 92 | + def latestVersion = getVersionFromPluginUrl(pluginUrl) |
| 93 | + if (latestVersion.toString() != currentVersion.toString()) { |
| 94 | + |
| 95 | + /* Update the plugin version in the lockfile */ |
| 96 | + pluginsToUpdate["${pluginName}"] = [currentVersion, latestVersion] |
| 97 | + println("Plugin: ${pluginName} current version is ${currentVersion}, it will be updated to latest version: ${latestVersion}") |
| 98 | + |
| 99 | + /* Plugins to be updated are added to the Plugins Update List */ |
| 100 | + pluginsUpdateList.add("-e s/${pluginName}:${currentVersion}/${pluginName}:${latestVersion}/g") |
| 101 | + |
| 102 | + } else { |
| 103 | + println("The latest version of ${pluginName} is already installed: ${currentVersion}") |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + /* Plugins find/replace operation */ |
| 108 | + if (!pluginsUpdateList.isEmpty()) { |
| 109 | + def pluginUpdate = "sed -i " + pluginsUpdateList.join(' ') |
| 110 | + shwrap(""" |
| 111 | + cd fedora-coreos-pipeline |
| 112 | + ${pluginUpdate} ${plugins_lockfile} |
| 113 | + """) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /* Open a PR if there are plugin updates */ |
| 118 | + stage("Open a PR") { |
| 119 | + if (shwrap("git diff --exit-code") != 0){ |
| 120 | + def message = "jenkins/plugins: update to latest versions" |
| 121 | + shwrap(""" |
| 122 | + cd fedora-coreos-pipeline |
| 123 | + git add jenkins/controller/plugins.txt |
| 124 | + git commit -m '${message}' -m 'Job URL: ${env.BUILD_URL}' -m 'Job definition: https://github.com/coreos/fedora-coreos-pipeline/blob/main/jobs/bump-jenkins-plugins.Jenkinsfile' |
| 125 | + """) |
| 126 | + withCredentials([usernamePassword(credentialsId: botCreds, |
| 127 | + usernameVariable: 'GHUSER', |
| 128 | + passwordVariable: 'GHTOKEN')]) { |
| 129 | + shwrap(""" |
| 130 | + cd fedora-coreos-pipeline |
| 131 | + git push -f https://\${GHUSER}:\${GHTOKEN}@github.com/${fork_repo} ${pr_branch} |
| 132 | + curl -H "Authorization: token ${GHTOKEN}" -X POST -d '{ "title": "${message}", "head": "${pr_branch}", "base": "main" }' https://api.github.com/repos/${fork_repo}/pulls |
| 133 | + """) |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + currentBuild.result = 'SUCCESS' |
| 138 | + } catch (e) { |
| 139 | + currentBuild.result = 'FAILURE' |
| 140 | + throw e |
| 141 | + } finally { |
| 142 | + if (currentBuild.result == 'SUCCESS') { |
| 143 | + currentBuild.description = "bump-jenkins-plugins ⚡" |
| 144 | + } else { |
| 145 | + currentBuild.description = "bump-jenkins-plugins ❌" |
| 146 | + } |
| 147 | + if (currentBuild.result != 'SUCCESS') { |
| 148 | + message = "bump-jenkins-plugins #${env.BUILD_NUMBER} <${env.BUILD_URL}|:jenkins:> <${env.RUN_DISPLAY_URL}|:ocean:>" |
| 149 | + pipeutils.trySlackSend(message: message) |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments