Creating a job with credentials in jenkins only in command lines
Asked Answered
T

2

1

I am only allowed to work with the command line terminal on Ubuntu. I need to create a job in Jenkins with security credentials.

I already installed Jenkins in my machine, but the access is open. If I set-up security credentials on Jenkins, how can I specify these on the command line?

Toothbrush answered 10/7, 2013 at 12:31 Comment(7)
You have not really asked a question here. I'm not sure what you using command line on Ubuntu has to do with Jenkins. Can you not access the Jenkins web page using another computer?Lenard
Ok. I know that is possible to configure jenkins only using command lines in Ubuntu. So, I need to make that configuration with a username, password and a job. But unfortunately I did not find this specific command yet.Toothbrush
Ok. I've updated your question based on what I understood. Please check and make sure it is what you wanted to ask.Lenard
Check out the SSH plugin in Jenkins. Credentials (usually an SSH key) are specified under the Jenkins settings.Neve
Thank you Sagar. That sounds better.Toothbrush
Hi Mark, I was trying to do that with a private key that was generated here : help.github.com/articles/generating-ssh-keys. But when I run the command "java -jar _jenkins-cli.jar -s localhost:8080 -i /home/ubuntu2/.ssh/id_rsa create-job test", it dont work. just return a exception error. I'll post it below. I don't know what I need to change anymore!Toothbrush
Exception in thread "main" java.lang.Exception: Failed to load key: /home/ubuntu2/.ssh/id_rsa at hudson.cli.CLI._main(CLI.java:228) at hudson.cli.CLI.main(CLI.java:199) Caused by: java.io.IOException: PEM is encrypted, but no password was specified at com.trilead.ssh2.crypto.PEMDecoder.decode(PEMDecoder.java:318) at hudson.cli.CLI.loadKey(CLI.java:301) at hudson.cli.CLI.loadKey(CLI.java:294) at hudson.cli.CLI._main(CLI.java:226) ... 1 moreToothbrush
T
1

Follow these steps:

  1. Generate a public ssh-key in the user you want to work with from the command line:

     ssh-keygen
    

    Just press enter whenever yo are asked to enter some value.

  2. Copy the content of the file ~/.ssh/id_rsa.pub.

  3. Go to your jenkins home screen in a browser and login with a user with full access, Then click on the right on Users and then on the user you are currently logged with. Then click on settings (or configuration??).

  4. In the field SSH public keys paste the content of the id_rsa.pub file. Save the changes.

And that's all! Now you are able to work with jenkins from the command line.

From the command line

The following is the configuration file of my jenkins main user (with all the privileges). Pay attention to the setting <authorizedKeys>. You should paste there the public ssh key. I haven't made that, but surely if you add the necessary lines to your config file it works. The file is in /path_to_jenkins/users/user_name/config.xml

<?xml version='1.0' encoding='UTF-8'?>
<user>
 <fullName>admin</fullName>
 <description></description>
 <properties>
  <jenkins.security.ApiTokenProperty>
   <apiToken>pP08W9tzs2jlCrVCY9I2o6y2RNu3Huw85Y2f99/Uif7dia1W7piGpzsrpstln/jw</apiToken>
  </jenkins.security.ApiTokenProperty>
  <com.cloudbees.plugins.credentials.UserCredentialsProvider_-UserCredentialsProperty plugin="[email protected]">
   <credentials/>
  </com.cloudbees.plugins.credentials.UserCredentialsProvider_-UserCredentialsProperty>
  <hudson.tasks.Mailer_-UserProperty plugin="[email protected]">
   <emailAddress>[email protected]</emailAddress>
  </hudson.tasks.Mailer_-UserProperty>
  <hudson.model.MyViewsProperty>
   <primaryViewName></primaryViewName>
   <views>
    <hudson.model.AllView>
      <owner class="hudson.model.MyViewsProperty" reference="../../.."/>
      <name>Alle</name>
      <filterExecutors>false</filterExecutors>
      <filterQueue>false</filterQueue>
      <properties class="hudson.model.View$PropertyList"/>
    </hudson.model.AllView>
  </views>
</hudson.model.MyViewsProperty>
<hudson.security.HudsonPrivateSecurityRealm_-Details>
 <passwordHash>1DF2ykjkkkjkkQXW</passwordHash>
</hudson.security.HudsonPrivateSecurityRealm_-Details>
<org.jenkinsci.main.modules.cli.auth.ssh.UserPropertyImpl>
 
<authorizedKeys>ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA4P1b/5RpibQgDZpKPD7lTQLjtyMrIQH43ns62PO72koL9zJe6qrAYcTIDNOUvSYNYyKfrgt6Z5zB8MvvENQLWezDKTWNXINhZml0PxOlc9ZaHbQX6UqyFbTS6o+ZEGs+K92Yi/XwK5hTmN5Igsw5BQYEs5cOsd5H2PoEZdhK1X0XAEBX/+p6aNy585+/scgZj0jSIvcX+pnzsCJLKmeYadlLnbrvebf9u6pu8MI9RuAY5dvPfpSL4WynWwS1QvY4z535TqPaaAlM3qXqH0pcOlxgW1iUkJqti3JnnxpBNXLmXalmq+4/d7mUrRBx+HKbh5ZpNZad9vaelAjAsNg+uw== user@machine_name</authorizedKeys>
</org.jenkinsci.main.modules.cli.auth.ssh.UserPropertyImpl>
  <hudson.search.UserSearchProperty>
   <insensitiveSearch>false</insensitiveSearch>
  </hudson.search.UserSearchProperty>
 </properties>
</user>
Transportation answered 16/7, 2013 at 15:20 Comment(7)
Hi @Nicolás, but following your answer, I need enter in the browser to put those information. I cant do that because I need to find a way to set up credentials (user and password) just in from terminal. That is my main problem and my time is ending. There is another way?Toothbrush
Yeaa! It seems to me that we now are talking about the same problem. How did you generate this '<passwordHash>'?Toothbrush
I didin't edit directly the file, just set the configurations using the browser. Take into account that the password hash may be a encrypted value. I don't know how to manage all jenkins capabilities from the console. From what I know it's necessary to use the browser to make the first configurations, specially to set the users and passwords.Town
We found the right commands and now we are able to enter with user and password.. This tips helped a lot. Thanks.Toothbrush
Unfortunatly I can't vote up to your answer. Any way, I'll open another discussion about how to correctly use ssh keys to run commands. This is the link: #17706793Toothbrush
Hey. I've seen the new question. In the instruction you are tryng to run, I think you have to pass as parameter the public key (id_rsa.pub), not the private key (id_rsa). (Sorry, I cannot comment on questions that are not mine). Other thing: With the jenkins-cli you have the option login, so I suppose you don't have to use ssh keys, just user and password. It's just and idea.Town
you are right about the public key(id_rsa.pub). Here we did something like that: *First we use gedit on terminal for opening the id_rsa.pub. *Second we copy the content into your config.xml above, where is said <authorizedKeys>. And finally, we pasted this .xml file into /path_to_jenkins/users/user_name/. After that, we just run the command root@ubuntu:/var/lib/jenkins# java -jar _jenkins-cli.jar -s http://my_localhost:8080/ restart. Was beautiful to see how the command was effective. Again.. thank you so much for your tips. Hope I can answer some problem too!Toothbrush
A
1

I just changed the starting prompt location to the folder in which the jar file is located. For example, if your jar file's location and name is:

C:\Users\Tod\file.jar

you can do two commands:

cd C:\Users\Tod
java -jar file.jar
Almeida answered 8/11, 2021 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.