Run groovy script via Jenkins CLI

Jenkins supports ssh authentication in CLI.

Below is a command to verify that I am authenticated:

2java -jar jenkins-cli.jar -s http://myserver/jenkins who-am-i
4    Authenticated as: myuser
5    Authorities:
6        authenticated

However you still would meet permission error when running groovy script in CLI.

2java -jar jenkins-cli.jar -s http://myserver/jenkins groovysh 'jenkins.model.Jenkins.instance.pluginManager.plugins.each { println("${it.longName} - ${it.version}") };'
4Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
5at $Proxy2.main(Unknown Source)
6at hudson.cli.CLI.execute(
7at hudson.cli.CLI._main(
8at hudson.cli.CLI.main(

It's a bug of Jenkins. The workaround is create a groovy script, then run that script via Jenkins CLI.

1java -jar jenkins-cli.jar -s http://myserver/jenkins/ groovy test_script.gsh