Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added script to update the Jenkins plugins #24

Merged
merged 2 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions jenkinsfile-runner-base-image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

Build the image using the `./build.sh`

## Plugin versions
## Update Plugin versions

The [packager-config.yml](packager-config.yml) also contains the list of plugins to install. Creating the list manually, with groupId, artifactId, version of any plugin and the transitive dependencies, is very cumbersome.
The [packager-config.yml](packager-config.yml) also contains the list of plugins to install. Creating the list manually, with groupId, artifactId, version of any plugin and the transitive dependencies, is too cumbersome.

Below you find ways to generate this list.
To update the plugins in `packager-config.yml` run the following command (from project root):
```sh
update/updatePlugins.sh
```

### Plugins from existing Jenkins

From the Script Console of a running Jenkins execute the following script to get the list of all installed plugins, preformatted for the `packager-config.yml`.
Although not the recommended way, you can also generate the list from an existing Jenkins instance.<br>
From the Script Console of this Jenkins execute the following script to get the list of all installed plugins, preformatted for the `packager-config.yml` and paste it manually.

```groovy
import com.cloudbees.groovy.cps.NonCPS
Expand Down
4 changes: 4 additions & 0 deletions jenkinsfile-runner-steward-image/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ function die() {
}

name=stewardci-jenkinsfile-runner
tagPrefix="${5-}"

cd "$(dirname "$BASH_SOURCE")" || die

git rev-parse --git-dir
if [[ $? == 128 ]]; then
# not in a Git checkout
tag="localbuild-$(date +%y%m%d)" || die
elif [[ -n "${tagPrefix}" ]]; then
tag="${tagPrefix}_$(date +%y%m%d)_$(git log --format='%h' -n 1)" || die
else
tag="$(date +%y%m%d)_$(git log --format='%h' -n 1)" || die
fi
Expand All @@ -44,4 +47,5 @@ if [[ ${1-} == "--push" ]]; then
echo "Pushing ${docker_repo}/$name:$tag"
docker tag "${name}-local" "${docker_repo}/$name:$tag" || die
docker push "${docker_repo}/$name:$tag" || die
echo "${docker_repo}/$name:$tag" > deployInfo.txt || die
fi
7 changes: 7 additions & 0 deletions update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

printf "Updating plugins\n"
./update/updatePlugins.sh

printf "\nUpdating Jenkins LTS version\n"
printf "(not automated yet - update packager-config.yml manually)\n"
23 changes: 23 additions & 0 deletions update/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
50 changes: 50 additions & 0 deletions update/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Update Plugin List

To update the plugin versions use the `generate.groovy` script to generate an updated list of plugins with dependencies based on the latest Update Site.

Usage:
```groovy
Usage: groovy generate.groovy <pluginsListFile> <outputFormat> [--skip-optional]
pluginsListFile Path to a file with plugin names (new line separated) of wanted plugins
outputFormat -cwp Custom War Packager packager-config.yml format (default)
-list Simple list of plugin names
-tree Print the dependency tree of each plugin
--skip-optional Do not include optional plugin dependencies
```

e.g. `groovy generate.groovy "../jenkinsfile-runner-base-image/plugins.txt" -cwp --skip-optional`

will produce:
```yaml
# AnsiColor (Jenkins-Version: 2.145)
- groupId: "org.jenkins-ci.plugins"
artifactId: "ansicolor"
source:
version: "0.6.2"
# Pipeline: API (Jenkins-Version: 2.138.4)
- groupId: "org.jenkins-ci.plugins.workflow"
artifactId: "workflow-api"
source:
version: "2.34"
# Pipeline: Step API (Jenkins-Version: 2.121.1)
- groupId: "org.jenkins-ci.plugins.workflow"
artifactId: "workflow-step-api"
source:
version: "2.19"
# Structs (Jenkins-Version: 2.60.3)
- groupId: "org.jenkins-ci.plugins"
artifactId: "structs"
source:
version: "1.19"
[...]
```

This can be copied/pasted into the `plugins` section of a [Custom War Packager] `packager-config.yml`. Or better run the `update.sh` in the project root folder.

To print the dependency tree run e.g.
```sh
groovy generate.groovy "../jenkinsfile-runner-base-image/plugins.txt" -tree --skip-optional
```


[Custom War Packager]: https://github.com/jenkinsci/custom-war-packager
125 changes: 125 additions & 0 deletions update/generate.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import groovy.json.JsonSlurper
import groovy.json.JsonOutput

wantedPlugins = [:]
resultingPlugins = [:]
updateCenter = null
debug = false

if(args.length == 1) {
process(args[0], "-cwp")
} else if(args.length >= 2) {
if(args[1] == "-cwp" || args[1] == "-list" || args[1] == "-tree") {
skipOptional = (args.length == 3 && args[2] == "--skip-optional")
process(args[0], args[1], skipOptional)
} else {
println("Unknown argument " + args[1])
}
} else {
println "Usage: groovy generate.groovy <pluginsListFile> <outputFormat> [--skip-optional]"
println " pluginsListFile Path to a file with plugin names (new line separated) of wanted plugins"
println " outputFormat -cwp Custom War Packager packager-config.yml format (default)"
println " -list Simple list of plugin names"
println " -tree Print the dependency tree of each plugin"
println " --skip-optional Do not include optional plugin dependencies"
println ""
println "e.g. groovy generate.groovy plugins.txt -cwp"
}

def process(wantedPluginsFile, outFormat, skipOptional) {
def wantedPluginNames = []
String fileContents = new File(wantedPluginsFile).getText('UTF-8').eachLine { line ->
wantedPluginNames << line
}

def url = "https://updates.jenkins.io/update-center.json".toURL()
def updateCenterJson = url.text
updateCenterJson = updateCenterJson.substring(0, updateCenterJson.length()-2)
updateCenterJson = updateCenterJson.replace("updateCenter.post(", "")
updateCenterJson = updateCenterJson.trim()

def jsonSlurper = new JsonSlurper()
updateCenter = jsonSlurper.parseText(updateCenterJson)

for(wanted in wantedPluginNames){
def wantedPlugin = updateCenter.plugins[wanted]
wantedPlugins[wantedPlugin.name] = wantedPlugin
resultingPlugins[wantedPlugin.name] = wantedPlugin
if(debug) println "Added: " + wantedPlugin.name
addDependencies(wantedPlugin, skipOptional)
}

if(outFormat == "-list") {
for(plugin in resultingPlugins.values()){
println plugin.name
}
}

if(outFormat == "-cwp") {
for(pluginKey in resultingPlugins.sort()*.key){
plugin = resultingPlugins[pluginKey]
def gav = plugin.gav
def matcher = gav =~ /([^:]*):([^:]*):([^:]*)/
if (!matcher) throw new RuntimeException("Wrong gav: " + gav)
def groupId = matcher.group(1)
def artifactId = matcher.group(2)
def version = matcher.group(3)
println "# " + plugin.title + " (Jenkins-Version: " + plugin.requiredCore + ")"
println " - groupId: \"" + groupId + "\""
println " artifactId: \"" + artifactId + "\""
println " source:"
println " version: \"" + version + "\""
}
}

if(outFormat == "-tree") {
for(pluginKey in wantedPlugins.sort()*.key){
plugin = wantedPlugins[pluginKey]
printDependencyTree(plugin, 0, "wanted")
}
println("--- Statistics -------------------------")
println("Wanted plugins: " + wantedPlugins.size())
println("Resulting plugins: " + resultingPlugins.size())
}

}

def addDependencies(plugin, skipOptional) {
//println "addDependencies(" + plugin + ")"
for(dependency in plugin.dependencies){
if(skipOptional && dependency.optional) {
continue;
}
def dependencyPlugin = updateCenter.plugins[dependency.name]
if(dependencyPlugin) {
resultingPlugins[dependency.name] = dependencyPlugin
if(debug) println "Added dependency (of " + plugin.name + "): " + dependencyPlugin.name
addDependencies(dependencyPlugin, skipOptional)
} else {
if(dependency.optional) {
println "ERROR: Could not find (optional) dependency " + dependency
} else {
throw new RuntimeException("Could not find (required) dependency " + dependency)
}
}
}
}

def printDependencyTree(plugin, indent, type) {
if(indent == 0) {
print "- "
} else {
for(i = 0; i < indent; i++) {
print " "
}
}
println plugin.name + ":" + plugin.version + " (" + type + ")"
for(dependency in plugin.dependencies){
dependencyPlugin = resultingPlugins[dependency.name]
if(dependencyPlugin == null) {
if(!dependency.optional) throw new RuntimeException(dependency.name + " (required) not contained in resultingPlugins")
} else {
printDependencyTree(dependencyPlugin, indent+1, dependency.optional ? "optional" : "required")
}
}
}
16 changes: 16 additions & 0 deletions update/updatePlugins.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

PROJECT_ROOT=$(cd "$(dirname "$BASH_SOURCE")/.."; pwd)
packagerConfig="${PROJECT_ROOT}/jenkinsfile-runner-base-image/packager-config.yml"
pluginList="${PROJECT_ROOT}/jenkinsfile-runner-base-image/plugins.txt"

# Remove outdated plugins list
sed -e "/^##PLUGINS-START/,/^##PLUGINS-END/d" "${packagerConfig}" > "${packagerConfig}.tmp"
mv "${packagerConfig}.tmp" "${packagerConfig}"

# Generate new plugins list
echo "##PLUGINS-START (do not remove!)" >> "${packagerConfig}"
groovy "${PROJECT_ROOT}/update/generate.groovy" "${pluginList}" -cwp --skip-optional >> "${packagerConfig}"
echo "##PLUGINS-END (do not remove!)" >> "${packagerConfig}"

echo "Updated plugins in ${packagerConfig}"
alxsap marked this conversation as resolved.
Show resolved Hide resolved