Jenkins cli与Jenkins ssh

Jenkins Cli introduce
a cli tool to Jenkins Jenkins Cli provided by this tool is very powerful, can be done such as restarting jenkins, create / delete the job, view job console output, add / delete nodes and other functions, but the actual work, like creating this configuration task is clear cli very difficult, as directly in the web management interface operation, but more convenient for the restart Jenkins, view diagnostic information, perform a manual build tasks, etc., is used directly cli than entering the web management interface operation. so when web management interface, when to use cli, depends on whether there is conducive to enhancing productivity, whether it helps enhance their ability, whether it helps improve the automation level team of these indicators, do not think using the cli on behalf of the blind use of high levels leading to cli decreased or increased efficiency issues.

jenkins jenkins cli can jar package provided by the environment or to create a cli cli performed using the ssh client, this section will also introduce them separately.

Performed using jenkins their clients cli
we open jenkins installation directory, go to war \ WEB-INF directory, this directory has a jenkins-cli.jar file, which is used to create jenkins cli environment. We are in the open a command-line tools directory, and then execute java -jar jenkins-cli.jar help will be able to see its output help information

$ java -jar jenkins-cli.jar help
Neither -s nor the JENKINS_URL env var is specified.
Jenkins CLI
Usage: java -jar jenkins-cli.jar [-s URL] command [opts...] args...
Options:
 -s URL              : the server URL (defaults to the JENKINS_URL env var)
 -http               : use a plain CLI protocol over HTTP(S) (the default; mutually exclusive with -ssh)
 -ssh                : use SSH protocol (requires -user; SSH port must be open on server, and user must have registered a public key)
 -i KEY              : SSH private key file used for authentication (for use with -ssh)
 -noCertificateCheck : bypass HTTPS certificate check entirely. Use with caution
 -noKeyAuth          : don't try to load the SSH authentication private key. Conflicts with -i
 -user               : specify user (for use with -ssh)
 -strictHostKey      : request strict host key checking (for use with -ssh)
 -logger FINE        : enable detailed logging from the client
 -auth [ USER:SECRET | @FILE ] : specify username and either password or API token (or load from them both from a file);
                                 for use with -http.
                                 Passing credentials by file is recommended.
                                 See https://jenkins.io/redirect/cli-http-connection-mode for more info and options.

The available commands depend on the server. Run the 'help' command to see the list.

You can see the -s option to specify a url, the url is the url jenkins web management interface, is now testing environment, url is http: // localhost: 8080, of course, want to perform cli, also you need to enter your user name and password here specify the user name and password by -auth option.

We put the information together to form the following command

java -jar jenkins-cli.jar -s http://localhost:8080 -auth tylerzhou:密码 help

Click Enter, you can see a brief description of all Jenkins cli executable commands and commands. Here are a few may be more commonly used commands

  1. list-jobs, Jenkins can be listed in all of the job, like opening jenkins web management interface Home seen.

The following command

java -jar jenkins-cli.jar -s http://localhost:8080 -auth 您的账户:您的密码 list-jobs

Note that, when Jenkins cli command on every execution need to bring java -jar jenkins-cli.jar -s http: // localhost: 8080 -auth your account: Your password + command to be executed, and enter the command only after entering an environment not on the line.

  1. build + To build the job name can be triggered by a build cli.

After building a successful, we open the web management interface can see more than one building.

cli command building is a very useful command, although most of our tasks are based on an automated process git, but sometimes also need to perform some manual script automation to accomplish some work in. If every time you open web management interface obviously not as good as executed manually to perform high-efficiency cli.

  1. restart / safe-restart service can restart jenkins, you can see the safe-restart is safe to restart by their names, it will wait until all operations are completed and then carries out a restart

  2. clear-queue Clear build queue. jenkins no practical experience with friends may not know their own reasons or due Jenkins our test script due to insufficient bug, sometimes causing obstruction build Jenkins, has been in a state building can not be completed, by this time click cancel to build web management interface can not be canceled at this time in an attempt to stay the order. Of course, it may still not be terminated at this time need to use violence restart command to restart the service.

Use Token landing

We are all above username: password to log in the way of cli, so that the clear-text passwords exposed is not desirable, in fact, Jenkins cli can also Username: to achieve landing cli apitoken Here we introduce five ways how to set api. token.

We open jenkins web management interface, click on the button to the left of the People, the interface will appear in the list may be more than one user, then click on the user's own login name jenkins, click the configure button to enter the interface, the interface appears find Api token bar, click Add new token generates token there will be a small screen, enter the token name can be any name, then click generate, this time it will generate a token
attention must take this token and then copy down save to other places, to find the next open when he can not see it.

Click Save when done.

At this point we enter the following command in cli

java -jar jenkins-cli.jar -s http://localhost:8080 -auth tylerzhou:11f52cef1324556a41d966083ffcf0ac1b

Which tylerhzou user name, is behind the token we just created

If successful, the output of the above command on all of jenkins cli command.

Command information saved to a separate file
, we can see a way to use token landing will lead to the command line is very long, seriously affect the readability of command, in fact, we can save the information to a file auth file. I Jenkins installation directory / create a text file called password.txt at war / WEB-INF directory, as follows

tylerzhou: 11f52cef1324556a41d966083ffcf0ac1b
That username above, enter the auth: apitoken

Then we specify a username (password or token) file by specifying the file name @ -auth option in the way to achieve land.

code show as below

java -jar jenkins-cli.jar -s http://localhost:8080 -auth @password.txt

Above the back of the @ password.txt is the password file we created

SSH to log in Jenkins execute cli

Use SSH to log in Jenkins needs to be configured, by default is a random port number jenkins SSHD port to use, this is clearly not conducive to ssh login using ssh landing we need to explicitly know the port number is how much we open the Manage jenkins> Configure Global Security find SSH Server field, the default random option to cut fixed and enter a different port number, I am using the 16022 click save configuration. port configured, we also need to add the public key to the current user profile entry, refer to a landing above api token, enter the current user interface, this time we do not configure apitoken, but pulled down to find the SSH Public Keys option, the copy we generate ssh public key here. see About generate ssh key other information about the previous chapters, not repeat them here.

After completion, we can execute commands via ssh way.

Enter the following command

ssh -l tylerzhou -p 16022 localhost help

-L which specified user account when we landed jenkins.
If the configuration is successful, the above command will list all the commands and a brief description of Jenkins.

Ssh command executed by the above command is executed jenkins-cli.jar like can refer to each other.

Jenkins cli package for simple
use bat simple package
can be seen, whether by performing the above cli tool or ssh, always need to bring some fixed login information, very tired, we can make a simple package, so every time only need to enter the command, no longer need to re-enter each fixed content

We put it into a bat encapsulated command, the command is

@echo off
cls
:start
set /p arg="请输入您的命令: "
java -jar jenkins-cli.jar -s http://localhost:8080 -auth @password.txt %arg%
goto start

The above command to create a start producing first label, and then prompts the user to input commands, input later spread to the jenkins cli tool, and then execute the goto statement to jump start the label. Repeat the above command, you want to terminate need to press ctrl + c to termination

Using powershell script package
due to the author of the bat is not very skilled, and therefore can write a complex script feel more strenuous, there were about using powershell package to support clear screen to view the execution status and exit options, the script reads as follows

[System.Console]::ForegroundColor=[System.ConsoleColor]::Green
 $writeout= 
  "  退出请按1或者输入exit
  清屏请按2或者输入cls或者clear
  查看执行状态请按3
  查看帮助请输入help
  查看提示信息请按4"
  Write-Host $writeout
[System.Console]::ForegroundColor=[System.ConsoleColor]::White

while ($true) {
$myvar=Read-Host "请输入命令 "
if(($myvar -eq 1)-or($myvar -eq "exit")){break}
elseif (($myval -eq 2) -or ($myvar -eq "cls") -or($myvar -eq "clear")) {
    Clear-Host
}
elseif ($myvar -eq 3) {
    if($LASTEXITCODE -eq 0){
      Write-Host "执行成功"
    }else{
        [System.Console]::ForegroundColor=[System.ConsoleColor]::Red
        Write-Host "执行失败"
        [System.Console]::ForegroundColor=[System.ConsoleColor]::White
    }
}
elseif($myvar -eq 4){
[System.Console]::ForegroundColor=[System.ConsoleColor]::Green
Write-Host $writeout
[System.Console]::ForegroundColor=[System.ConsoleColor]::White
}
else {
   java -jar jenkins-cli.jar -s http://localhost:8080 -ssh -user tylerzhou $myvar.split(" ")
}
}

We create a ps1 file, copy the above into account and follow the instructions, you can execute the script.

windows no problem ssh client

Some children's shoes when doing follow the above way of landing ssh, ssh may be entered at the command line prompt command not found, because only the latest version of windows 10 (not all versions have). Fortunately, Jenkins-cli .jar also provides ssh landing way. we enter the following command

java -jar jenkins-cli.jar -s http: // localhost: 8080 -ssh -user tylerzhou help
specify ssh way way above the landing, and specify the -user, you do not need to specify the port, you can use ssh to connect us. you can use a closed method mentioned above simply closed it, so you do not have to keep entering repetitive, fixed content.

Published 33 original articles · won praise 0 · Views 3924

Guess you like

Origin blog.csdn.net/erhaiou2008/article/details/103973851