Call Jenkins CLI from within pipeline - jenkins

Is it possible to call Jenkins CLI commands within a pipeline?
Upon migrating jobs to new Jenkins instances I would like to enable users to migrate their own jobs from an old Instance.

Yes, you can write pretty much any shell code you want, including calling the CLI jar from a shell instruction.
sh "java -jar /path/to/jenkins-cli.jar your-usual-command"

Related

Running jobs in Jenkins slave node using Groovy script

I am confused about whether a Jenkins job can be run using Groovy script in the slave node. I referred a StackOverflow answer [1] which says that System Groovy script jobs can be run in master and not in slave, and to run a job in slave it has to be Groovy script rather than System Groovy Script. Can someone clarify me whether we can run a slave job using System Groovy Script? Since I am trying through Groovy script I am unable to access few Jenkins instances. Please suggest me a better way with an explanation. Thanks in advance
I have finally found that it is only possible to run jobs in Jenkins slave node using Grovvy script. The system groovy script runs inside the Hudson master's JVM. Thus it will have access to all the internal objects of Hudson, so we can use this to alter the state of Hudson. It is similar to the Jenkins Script Console functionality.

deploy jar on windows server with jenkins pipeline

I have a maven built jar file which could be run as a server. I want to use jenkins-pipeline to deploy this jar file onto my windows 2016 server. I started with a freestyle jenkins job, with "Execute Windows batch" configuration:
set BUILD_ID=DontKillMe
start java -jar MyServer.jar
The java process successfully spawned on my windows 2016 server.
When I turned to use jenkins pipeline script with same batch commands, it's not as expected -- the process which should contain java -jar MyServer.jar was never spawned.
The pipeline script I wrote is:
bat '''
set BUILD_ID=DontKillMe
start java -jar MyServer.jar
'''
The reason I want to have jar starts running in another process is that it could release current jenkins build to following steps.
Could anyone please help with a solution? As long as I can spawn up the java process from batch command in jenkins pipeline (better with no parent process), I would be really grateful.
Ok, seems like jenkins is trying to abandon old jenkins users like me, here's the solution provided by jenkins pipeline:
withEnv(['JENKINS_NODE_COOKIE=DontKillMe']) {
bat "start java -jar MyServer.jar"
}

Run a db script using jenkins

I need to run a db script to take oracle database backup in a remote server using Jenkins in Windows environment. Is it possible? I created a Free style project in Jenkins and installed SQLPlus script runner plugin. But it didn't work. Can anyone please advise me how to do this?
If you have a simple shell or python script to run from jenkins then you can do it like this :
Configure jenkins job to run as shell script
and write something like
#!/bin/sh
sh <location_of_shell_script_for_backup>
You can change sh as per your requirement.

Executing shell script using Jenkins UI

I would want to execute the shell script using Jenkins.
For example, I have a shell script MakeTest.sh which is on the server "test1.test2.net" location "/home/rt", how would I execute it? My Jenkins and script to be executed are in different servers.
Thanks!
Using git, you can download the file into the workspace of your job.
After you just have to launch the script ./MakeTest.sh

Difference between execute groovy script and the execute system groovy script in jenkins?

Can anyone explain the different between the execute groovy script and the execute system groovy script in jenkins? And how to call the script to slave using execute system groovy script.
To execute a groovy script on the slave machine, you should use groovy plugin
Quote
The plain "Groovy Script" is run in a forked JVM, on the slave where
the build is run. It's the basically the same as running the "groovy"
command and pass in the script.
First part of your question is answered in the same page
The system groovy script, OTOH, runs inside the Hudson master's JVM.
Thus it will have access to all the internal objects of Hudson, so you
can use this to alter the state of Hudson. It is similar to the
Jenkins Script Console functionality.
Another point on system Groovy scripts to be aware of. While the documentation says that it always runs on the Jenkins master, I've found through painful means that it isn't true if it's in a job that is triggered by another job. In that case, make sure you specifically restrict it to run on the master or bad things will happen.

Resources