You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins sshScript传递构建参数执行远程脚本报错求助

问题解决思路

错误根源

出现java.lang.IllegalArgumentException的核心原因是:sshScript步骤的script参数被错误传入了带参数的字符串,Jenkins会把"deploy.sh ${version}"整体当作脚本文件名去远程服务器查找,而实际存在的只有deploy.sh,因此提示文件不存在。

修正步骤

1. 先修复Jenkinsfile的语法错误

原代码存在两处基础语法问题:

  • properties块中string定义末尾缺少闭合的)
  • remote.allowAnyHose拼写错误,正确写法是remote.allowAnyHost

2. 正确传递脚本运行参数

sshScript步骤需要通过独立的arguments参数传递脚本参数,不能把参数拼在script的路径字符串里。修正后的完整Jenkinsfile如下:

pipeline {
    agent { label "linux" }
    properties([
        string(description: 'pass the version', name: 'version')
    ])
    stages {
        stage('shell script to run with parameters') {
            script {
                def remote = [:]
                remote.name = "hostname"
                remote.user = "username"
                remote.password = "password"
                remote.allowAnyHost = true
                // 通过arguments单独传递version参数
                sshScript remote: remote, script: "deploy.sh", arguments: "${version}"
            }
        }
    }
}

3. 替代方案:用sshCommand直接执行命令

如果sshScript的参数传递仍不符合预期,可改用sshCommand步骤,先将本地脚本复制到远程,再执行完整命令,确保脚本和参数被正确解析:

pipeline {
    agent { label "linux" }
    properties([
        string(description: 'pass the version', name: 'version')
    ])
    stages {
        stage('shell script to run with parameters') {
            script {
                def remote = [:]
                remote.name = "hostname"
                remote.user = "username"
                remote.password = "password"
                remote.allowAnyHost = true
                // 将本地工作区的deploy.sh复制到远程临时目录
                sshPut remote: remote, from: "deploy.sh", into: "/tmp/"
                // 赋予执行权限并带参数运行脚本
                sshCommand remote: remote, command: "chmod +x /tmp/deploy.sh && /tmp/deploy.sh ${version}"
            }
        }
    }
}

验证要点

  • 确认Jenkins工作区的deploy.sh确实存在,且脚本本身逻辑正常
  • 构建时正确输入version参数,确保参数能被正确传递到远程服务器的脚本中

内容的提问来源于stack exchange,提问作者Shyam V

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 01:07:36