jenkins tutorial: Publish Over SSH plug-in remote ssh execution command

Publish Over SSH

Insert image description here

After the jenkins build is completed, it needs to be published with one click. The structure is as follows

A server svn

B server jenkins

C server application server

B pulls the code from A and packages it into a war, and then copies the war package to the C server.

What is solved here is the remote copy problem

1. First install the jenkins SSH plug-in

打开Jenkins的“系统管理>管理插件”,选择“可选插件”,在输入框中输入“Publish over SSH”进行搜索,
如果搜索不到可以在“已安装”里确认是否已经安装过。在搜索结果中选中“Publish over SSH”,
点击页面的“直接安装”按钮,系统会自动安装,此插件安装后不需要重启Jenkins。

如果插件安装成功在“系统管理>系统设置”会出现相关配置项。

If you cannot install it online, you can download the hpi file yourself and upload it manually.

img

2. Configure ssh

There are two methods: password method and key-free login

Here first write the key-free login connection method

2.1 Key-free login

After the plug-in is successfully installed, it needs to be configured in "System Management > System Settings" before use, as shown in the figure:

First configure Jenkins SSH Key

img

参数说明
Passphrase:
SSH的密码
使用用户名/密码登录时为用户名的密码
使用私钥登录时为私钥的密码。
Path to key:
SSH私钥的文件路径
私钥文件的路径,可以是绝对路径,也可以是相对$JENKINS_HOME的相对路径
Key:私钥
私钥导出后的文本内容

如果“Key”和“Path to key”都设置,则“Key”的优先级较高,私钥的密码是“Passphrase”中设置的内容。

Disable exec:禁止在目标机上执行命令
勾选后将会忽略在Job配置中“Exec command”选项中设置的命令。
Jenkins的说明文档中的“The Disable exec in the advanced settings for individual configurations will be ignored.”
没有完全理解,从实际效果来看,只要“Disable exec”被勾选后,不管SSH Server中是否勾选“Disable exec”,Job中设置的命令都将补忽略。

Configure SSH Servers again

img

参数说明
Name
SSH节点配置的名称,在Job中使用Publish over SSH插件时,此名称将出现在“SSH Server”中“Name”的下拉列表中,如下图:

Hostname
通过SSH连接到的机器的主机名或IP
Username
SSH服务使用的用户名,使用key进行连接时为key指定的用户名
Remote Derictory
运程机器上真实存在的目录,并且“Username”指定的用户要有访问此目录的权限,插件将把文件传送到此目录下。

advanced settings

In the advanced settings, you can set the SSH port, retry time and reset the options in "Global Settings". Since the functions are similar, repeated explanations will not be repeated. The settings are as follows:

img

Important note! ! !

Important note! ! !

Important note! ! !

1. Determine the target host copy path

The Remote Derictory option in the above configuration refers to the root directory of the remote host.
It must match the Remote Derictory option in the following configuration.

Example: Copy the file war package to the /usr/local directory of the remote host.
There are two ways to write it.

  1. The Remote Derictory in jenkins SSH is configured as /usr/
    ​ The Remote Derictory in the Post-build Actions step in the Jenkins configuration is configured as local

or

  1. The Remote Derictory in jenkins SSH is configured as /
    ​ The Remote Derictory in the Post-build Actions step in the Jenkins configuration is configured as /usr/local

2. Determine the source host copy path

Source relative path problem, not .Jenkins/workspace, but jenkins/workspace/[project]/

How do I determine the relative path on the server? jenkins home directory + workspace + task name created on the jenkins page (can be found in the workspace directory)

/var/lib/jenkins/workspace/1/abcd/a.war
示例:
如果  /var/lib/jenkins/workspace/1/abcd/a.war
的话 在下面source file 中只要填写 1/abcd/a.war
**/war包 可以匹配,但是不精确

3. Remove prefixes

If the prefix is ​​not removed, the above example will generate the following directory /usr/local/1/acbd/a.war based on the remote directory.

What we want is only the war package, so we can remove 1/acbd/ through the remove prefix option and only generate the bian.war package file.

Configure Job in Jenkins

In the Job settings, select "Send files or execute commands over SSH" in "Add build step" to use the Publish over SSH plug-in in the Job. The configuration is as follows:

img

参数说明
Name:
“系统管理>系统设置”设置的SSH Sverver的名字列表。
Source files:
复制到运程机上的文件,相对workspace的路径,也支持表达式,如上图中的“**/*.war”。
Remove prefix:
文件复制时要过滤的目录,如上图中的target目录。
Remote directory:
文件得到到远程机上的目录,此目录是相对于“SSH Server”中的“Remote directory”的,如果不存在将会自动创建。
Exec command:
在这里可以填写在运程机器上执行的脚本

Note: matching can be used in the source files option

*.war      所有war包
*          只传输文件,文件夹不会传输
**         所有文件

links:

https://www.cnblogs.com/centos2017/p/12191337.html

Guess you like

Origin blog.csdn.net/a772304419/article/details/132838133