Run groovy script via Jenkins CLI

Jenkins supports ssh authentication in CLI.

Below is a command to verify that I am authenticated:

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

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

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

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