Showing posts with label jenkins-cli. Show all posts
Showing posts with label jenkins-cli. Show all posts

Tuesday, July 21, 2015

Daemon hell in Jenkins

Recently I wrote a Linux like initd script to start/stop my web application.

The script works well when running it in shell of linux. The web application will run in background by daemon.

However I found both daemon and web application(java) exited immediately if I started the script in Jenkins as a shell step of build process.

I put below simple script in 'Execute shell' block,

daemon --name=test-daemon -- sleep 200sleep 60

The process 'daemon' and 'sleep 200' should exit after 200 seconds the 'sleep' exits. The jenkins job will be finished in 60 secs.

jenkins   9954  9950  0 21:48 ?        00:00:00 sleep 60
jenkins   9955     1  0 21:48 ?        00:00:00 daemon —name=test-daemon — sleep 200
jenkins   9956  9955  0 21:48 ?        00:00:00 sleep 200

Above is the process info queried via ps command. The father pid of daemon is 1, not the script generated by Jenkins.
But both the process 'daemon' and 'sleep 200' immediately exited when the script finished. Should be something wrong in Jenkins to cause daemon exited unexpected.

It's something really frustrating to use daemon to stop/start the web application in Jenkins.

Finally I used **docker** container to run my web application, which easily can be stopped/started via script in Jenkins.

Monday, May 13, 2013

Run groovy script via Jenkins CLI

Jenkins supports ssh authentication in CLI.

Below is a command to verify that I am authenticated:

java -jar jenkins-cli.jar -s http://myserver/jenkins  who-am-i

    Authenticated as: myuser
    Authorities:
        authenticated

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

java -jar jenkins-cli.jar -s http://myserver/jenkins groovysh 'jenkins.model.Jenkins.instance.pluginManager.plugins.each { println("${it.longName} - ${it.version}") };'

Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at $Proxy2.main(Unknown Source)
at hudson.cli.CLI.execute(CLI.java:271)
at hudson.cli.CLI._main(CLI.java:417)
at 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.

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