Jenkins Pipeline使用sshPut复制文件文件夹至Windows机器失败求助
解决sshPut复制文件到Windows机器失败的问题
原命令的核心问题
你的sshPut命令主要存在两个常见问题:
- Windows路径转义错误:字符串中的
\是转义字符,D:\DATA会被解析为D:DATA,导致目标路径无效。 - 缺少递归复制参数:默认情况下
sshPut不会自动递归复制子目录,需要显式开启递归选项。
修正后的sshPut命令
方法1:使用正斜杠路径+递归参数
sshPut remote: remote, recursive: true, from: './', into: 'D:/DATA'
- 正斜杠
/在Windows SSH环境中可正常识别,避免转义问题。 recursive: true确保子目录及其中文件被一并复制。
方法2:转义反斜杠路径
若坚持使用反斜杠,需对路径中的\进行双重转义:
sshPut remote: remote, recursive: true, from: './', into: 'D:\\DATA'
额外排查步骤
- 确认远程目录存在:如果
D:\DATA未创建,先通过SSH执行创建命令:sshCommand remote: remote, command: 'mkdir D:\\DATA' - 检查权限:确保SSH连接用户对
D:\DATA目录有写入权限,Windows下可能需要调整文件夹安全权限。 - 查看错误日志:执行命令时查看详细日志,定位具体错误(如路径不存在、权限拒绝、正则匹配异常)。
- 调整正则过滤规则:若必须使用
filterRegex,避免匹配当前目录(.),可使用^(?!\\.$).*排除根目录本身:sshPut remote: remote, recursive: true, filterRegex: '^(?!\\.$).*', from: './', into: 'D:/DATA'
替代方案:使用scp命令
如果sshPut仍有问题,可直接用scp命令完成递归复制:
scp -r ./ your-windows-user@windows-host-ip:D:/DATA
内容的提问来源于stack exchange,提问作者LevanM
相关产品推荐
相关产品推荐

