Categories
Uncategorized

Continuous Integration with Jenkins ssh Jenkins cli advanced part of the

Series catalog

Jenkins Cli Introduction

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, such as job creation configuration 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. Therefore, when the 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 to enhance the team’s automation levels of these indicators do not think the use of high levels cli on behalf of the blind use cli leading to reduced efficiency or increasing problem.

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.

Cli use to perform their own client jenkins

We opened 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 open a command-line tool in this directory, then execute java -jar jenkins-cli.jar help will be able to see its output help information

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

The above statement, help for the command to be executed.

Note that the command executed here is the direct command name can not be added as other command-line tools – – and / or logo

If you can not find Jenkins-cli.jar, can enter in the browser http: // localhost: 8080 / jnlpJars / jenkins-cli.jar download save, pay attention to the above address is replaced with your actual address

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.

2) 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.

3) 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

4) clear-queue Clear build queue. Jenkins no practical experience with friends may not know their own reasons or due Jenkins bug our test script to inadequate, and sometimes can cause obstruction build Jenkins, has been in a state unable to complete the building, which when the web administration interface click cancel to build 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 api 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 column

Click Add new token generates token there will be a small screen, enter the name of the token, which can be any name, then click generate, this time it will generate a token

Note that this token must take down the copy and save it somewhere else, next time to find 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 that the way to use the 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 / war / WEB-INF directory create a text file called password.txt, 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.

Simple encapsulation of jenkins cli

Using simple packaging bat

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 that each time only need to enter the command, no longer need each re-enter 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

As 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

Ssh designated landing approach in the above manner, and specify the -user, do not need to specify the port, you can use ssh way connected. We can simply look at using closed closed method mentioned above, so you do not have to keep entering repetitive, fixed content.

Leave a Reply