Jenkins CLI - How to build project ignore trigger remote job - jenkins

Here is my Jenkins trigger setting
and param
And I use the following command to build the project and got success.
$ java -jar jenkins-cli.jar -s http://localhost:8080 -auth USERNAMR:PASSWOED build PROJECT_NAME -p branchName=master -p buildEnv=UAT
If build success it will trigger another Jenkins to build next platform, I just want to build single one don't tigger another platform build, can do it using CLI command only?

Related

How can I run a Curl command from Jenkins A in order to trigger a build in Jenkins B

I want to trigger a build in Jenkins B by running build in Jenkins A, I know that I can use a Curl command from Jenkins A but actually I couldn't figure it how and where to write the command, inside the pipeline or as power-shell script?
Here is described how it works.
You need to download the jenkins-cli.jar from the jenkins server you want to trigger a job on.
wget http://YOUR_JENKINS_HOSTNAME/jnlpJars/jenkins-cli.jar
You have to verify that you can authenticate to the jenkins server.
ssh-keygen -t rsa && cat ~/.ssh/id_rsa.pub
Copy the public key to http://YOUR_JENKINS_HOSTNAME/user/YOUR_USERNAME/configure. On the same page you can generate a API Token. You will need it for the remote call. You can save it as file with the following command:
echo 'YOUR_USERNAME:YOUR_API_TOKEN' > jenkins_secret
Now you can call the remote jenkins server and trigger builds
java -jar ./jenkins-cli.jar -s http://YOUR_JENKINS_HOSTNAME list-jobs
To integrate it to your pipeline just add a sh step and take the last line (the jenkins-cli.jar call) and add it to your pipeline.

Cannot execute a shell script with docker command from Jenkins Pipeline step

I am using Jenkins version 2.121.1 with Pipeline On MacOS-HighSierra.
I've a shell script called build_docker_image.sh that builds a docker image using the following command:
docker build -t test_api:1 -f test-dockerfile
test-dockerfile is a Dockerfile and has instructions to build an image.
From CLI the whole set up works!
However, when I run it from Jenkins server Pipeline context, it is failing at the above line with an error: docker: command not found
The step that triggers from Jenkins server is simple. Call the script:
stage ('Build-Docker-Image') {
steps {
sh '/path/to/build-docker_image.sh'
}
}
In Jenkinsfile, I made sure the $PATH is including the path to Docker.
The issue was that I was appending the actual docker application like this /Applications/Docker.app/Contents/Resources/bin/docker, instead of the directory /Applications/Docker.app/Contents/Resources/bin

Jenkins CLI : ERROR: anonymous is missing the Overall/Read permission

I'm stuck with this problem and I have no idea to solve it.
I have written a Shell script which will invoke my job using Jenkins CLI by passing my private key.Jenkins version is 2.121.1
java -jar jenkins-cli.jar -s http://localhost:8080 -i ~/.ssh/id_rsa build RTT/RTT-CI-Tools/RTT-CI-Tools-Distribute -s -p SLAVE_REGEX=testserver
Getting Error message as :
ERROR: anonymous is missing the Overall/Read permission
The same script works in another Jenkins (2.7.4). How to fix this issue.
You can also use auth param but you should to type your password in console
java -jar jenkins-cli.jar -s http://localhost:8080/ -auth myLoggin:myPassword list-jobs
Please check for below points
1) USER exist on jenkins server as same on linux machine.
2) SSH Public key shared on Jenkins server is correct.(manage jenkins --> manage user --> click on ${USER} --> click on configure --> then check ssh public key is correct).
3) CMD i used(working) --> java -jar jenkins-cli.jar -ssh -user ${USER} -i ~/.ssh/id_rsa -s http://localhost:8080/jenkins/ build ${JOB_NAME}
please check if you are executing cmd from same user.
4) SSH port should be enable on Jenkins (go to manage Jenkins--> configure Global security --> SSH Server... set SSHD Port Fixed eg 38844)
This issue cropped up for me, too, recently (using the cli to automate installing jenkins). I was able to work around it by setting the denyAnonymousReadAccess flag to false in jenkins' config.xml file, and restarting jenkins:
<authorizationStrategy class="hudson.security.FullControlOnceLoggedInAuthorizationStrategy">
<denyAnonymousReadAccess>false</denyAnonymousReadAccess>
</authorizationStrategy>

Jenkins run Shellscript via SSH is not leaving console

I'm using Jenkins to deploy my play application for this I've added SSH support to jenkins and I connect via ssh to the test server and then I run a shel script via ssh.
Thats working fine.
Not working ist finishing the job in jenkins.
The command in the the shell script is the following:
/usr/src/activator-dist-1.3.10/bin/activator "~ run" &
that only should run the activator, build and run the project
But then when The application is build and the activator runs the Jenkins job dosn't finish ... it always hang in console
looks like this:
When you run a script via ssh it will stay open until stdout/stderr are closed or a timeout occurs. In Jenkins it seems as if the script hangs.
So if you run a script as background job, make sure to redirect all its output to somewhere:
nohup yourCommand < /dev/null > /dev/null 2>&1 &
or
nohup yourCommand < /dev/null >> logfile.log 2>&1 &
See SSH Frequently Asked Questions for more details.

Jenkins CLI -- how to build a job in a subdirectory

I am trying to build a job using jenkins-cli.jar, but I cannot figure out how to tell Jenkins the job is in a subdirectory.
$ java -jar jenkins-cli.jar -s https://myserver/ list-jobs util --username Jerry --password swordfish
TheJobIWant
AnotherJob
SomeOtherJob
So the job is clearly there, inside that folder. But when I try to run it, I get:
$ java -jar jenkins-cli.jar -s https://myserver/ build util/TheJobIWant -s --username Jerry --password swordfish
No such job 'util/TheJobIWant'
How am I supposed to call this job from the CLI?
When you were saying 'subdirectory', do you mean 'subview' ?
I've created a job named 'job1' under the netsted view-->subview folder.
When I tried to trigger a build from jenkins command line, I was using:
java -jar jenkins-cli.jar -s http://localhost:8080/ build job1 --username admin --password admin
And as you can see, 'job1' has successfully been triggered.
Hope this helps!
May want to check out https://issues.jenkins-ci.org/browse/JENKINS-11024. There is a workaround by allowing anonymous users read-access to the jobs, but that's not always suitable for every installation.
Use -i option instead of --username --password. Or curl as mentioned in https://issues.jenkins-ci.org/browse/JENKINS-12543

Resources