需要能够在 Jenkins 管道中切换目录并在远程服务器中执行 shell 脚本

问题描述 投票:0回答:1

要求是使用一些凭据登录到远程服务器,然后切换到另一个用户和路径并执行 shell 脚本。虽然我找到了一种登录远程服务器的方法,但无法弄清楚其他步骤。任何见解都会非常有帮助。以下代码块执行登录,但无法切换用户和更改目录。

def remote = [:]
remote.name = 'QA'
remote.host = '193.401.200.001'
remote.user =  'admin'
remote.password = 'admin'
remote.allowAnyHosts = true
                    
stage('Remote SSH') {
   sshCommand remote: remote, sh "pbrun su - myuser"
   sshCommand remote: remote, sh "cd /apps"
   sshScript remote: remote, script: "demo.sh"
}
jenkins jenkins-plugins jenkins-groovy cicd
1个回答
0
投票

不幸的是,我没有足够的声誉来发表评论,所以我会把我的想法作为答案。

如果您需要以其他用户身份运行,您只需使用该用户进行 SSH(在您的情况下为

myuser
用户)

此外,每个

sshCommand
都将在用户主目录中运行,因此运行
cd
会将您置于该目录中,只是为了执行该操作,而不是为了后续调用
demo.sh

我相信以下内容可以实现您的目标:

def remote = [:]
remote.name = 'QA'
remote.host = '193.401.200.001'
remote.user =  'myuser'
remote.password = 'myusercreds'
remote.allowAnyHosts = true
                
stage('Remote SSH') {
   sshCommand remote: remote, sh "/apps.demo.sh"
}
© www.soinside.com 2019 - 2024. All rights reserved.