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

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

Related

Jenkins CLI - How to build project ignore trigger remote job

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?

Import Job to Jenkins from xml

I am new in jenkins and docker.I am trying to import jenkins new job from xml file.Current jenkins is running inside docker.(jenkinsci/blueocean).
Here is my command:
java -jar .\jenkins-cli.jar -s http://localhost:8080/ create-job jobname .\myjob.xml
Here is error:
anonymous is missing the Overall/Read permission
What I have done:
I already check this one
Anonymous is missing the Overall/Read permission
So I test with this cmd:
java -jar .\jenkins-cli.jar -s http://localhost:8080/ -auth username:password who-am-i
It return
Authenticated as : username
Authorities: authenticated
Then I also try like that :
java -jar .\jenkins-cli.jar -s http://localhost:8080 create-job jobname .\myjob.xml -auth username:password
its also happen error.
Pls help.
I will prefer job import at build time, Here is the working example that you can try.
FROM jenkinsci/blueocean
COPY config.xml /usr/share/jenkins/ref/jobs/job_name/config.xml
You can try working below command to run demo job import
git clone https://github.com/Adiii717/docker-jenkins-job-import.git
cd docker-jenkins-job-import;
docker-compose build
docker-compose up

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.

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 CLI List-Jobs with folders

While using the Jenkins Folders Plugin, is there a way to get a list of all jobs (including jobs in folders and possible the folder path) simular to how list-jobs in the default CLI works?
I have made a small PowerShell script to get info for the last build of every job in the default dashboard and export relevant info to excel. But now we started using folders, and it doesnt work for folders and the jobs in them.
My old import code:
java -jar jenkins-cli.jar -s http://localhost:8080 list-jobs --username $username --password $password > jobs.csv
http://pastebin.com/raw/rcj99rjx for my full code with comments
Solved it by running a groovy script.
import jenkins.model.*
import hudson.model.*
Jenkins.instance.getAllItems(AbstractProject.class).each { println(it.fullName) };
and this cli code to call for the script.
java -jar jenkins-cli.jar -s http://localhost:8080 groovy all_jobs.gsh --username $username --password $password > jobs.csv
The list-jobs command can show the jobs in a folder (or "view"). So you can iterate the top-level list (assuming it is a list of views) and then list the jobs in each of those views.
In a Windows cmd shell, that looks like this:
for /f "usebackq" %F in (`java -jar jenkins-cli.jar -s http://localhost:8080/ list-jobs`) do java -jar jenkins-cli.jar -s http://localhost:8080/ list-jobs %F

Resources