gpt4 book ai didi

jenkins - 如何在 Jenkinsfile 中通过 ssh 连接到服务器

转载 作者:行者123 更新时间:2023-12-02 14:17:18 25 4
gpt4 key购买 nike

pipeline {
agent any
stages {
stage('Build React Image') {
steps {
...
}
}
stage('Push React Image') {
steps {
...
}
}
stage('Build Backend Image') {
steps {
...
}
}
stage('Push Backend Image') {
steps {
...
}
}
def remote = [:]
remote.name = '...'
remote.host = '...'
remote.user = '...'
remote.password = '...'
remote.allowAnyHosts = true
stage('SSH into the server') {
steps {
writeFile file: 'abc.sh', text: 'ls -lrt'
sshPut remote: remote, from: 'abc.sh', into: '.'
}
}
}
}

我遵循了此页面上的文档:https://jenkins.io/doc/pipeline/steps/ssh-steps/在 Jenkinsfile 中通过 ssh 进入服务器。我的最终目标是通过 ssh 连接到服务器,从 dockerhub 拉取、构建并安装它。

首先,我只想成功地通过 ssh 进入它。

这个 Jenkinsfile 给我 WorkflowScript: 61: Expected a stage @ line 61, column 9. def remote = [:]

不确定这样做是否正确。如果有更简单的方法可以通过 ssh 连接到服务器并像我手动执行的那样执行命令,那也很高兴知道。

提前致谢。

最佳答案

错误源于语句 def remote = [:] 和后续赋值在 stage block 之外。此外,由于声明性语法不支持直接在 steps block 中声明,因此您还需要将那部分代码包装在 script block 中。

stage('SSH into the server') {
steps {
script {
def remote = [:]
remote.name = '...'
remote.host = '...'
remote.user = '...'
remote.password = '...'
remote.allowAnyHosts = true
writeFile file: 'abc.sh', text: 'ls -lrt'
sshPut remote: remote, from: 'abc.sh', into: '.'
}
}
}

关于jenkins - 如何在 Jenkinsfile 中通过 ssh 连接到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59040218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com