Jenkins CLI - create and update views - jenkins

I'm not sure how to properly use the Jenkins CLI to create and update views.
For example, I am exporting a view in this way:
java -jar jenkins-cli.jar -s http://jenkins.com/ get-view "TEST" --username $username --password $password >> $WORKSPACE/test.xml
The xml file contains the entire tree structure along with all the Jenkins jobs.
As I try to create a new view:
java -jar jenkins-cli.jar -s http://jenkins.com/ create-view "TEST2" < $WORKSPACE/test.xml --username $username --password $password
It creates a view with all the folders, but when I click on a folder it moves me to the Jenkins home page.
This is my structure:

Related

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

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>

Getting "Error: you must authenticate to access this jenkins" when trying to run "Java -jar jenkins-cls.jar -s http://localhost:8080 /help"

Getting below error
Error: you must authenticate to access this jenkins" when trying to run "Java -jar jenkins-cls.jar -s http://localhost:8080 /help
Steps performed as below,
1. Copied jenkins-cls.jar to the local drive.
2. Opened command prompt and navigate to path where jenkins-cls.jar is saved.
3. Type "Java -jar jenkins-cls.jar -s http://localhost:8080 /help" and press enter.
It should ask for pass-code but instead, it displayed message "Error: you must authenticate to access this jenkins".
Any help to resolve this query is highly appreciated.
Go to Manage Jenkins -> Configure Global Security ->
Enable Security Checked
Authorization -> Logged-in Users can do anything -> Checked allow anonymous
read access
Restart Jenkins and try running
java -jar jenkins-cls.jar -s http://localhost:8080 help --username user --password pwd
or
java -jar jenkins-cls.jar -s http://localhost:8080 login -auth username:(password/token).
You have to pass your username and password while executing the command like as below:
java -jar jenkins-cls.jar -s http://localhost:8080 /help --username <userName> --password <password>
The file name is now changed from jenkins-cls.jar to jenkins-cli.jar.
Make sure the user is registered in jenkins front-end and Enable Security is checked for the user.
java -jar jenkins-cli.jar -s http://localhost:8080 -auth username:password
Go into Configure global security section, search for Allow anonymous read access, check it as true, apply and save.
Or you can just choose Anyone can do anything instead of Logged-in users can do anything
Test your command now, it should be work

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

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