jenkins - 如何通过 ssh 进入 Jenkinsfile 中的服务器
问题描述
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
块之外引起的。此外,由于声明性语法不支持直接在steps
块中的语句,因此您还需要将那部分代码包装在script
块中。
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: '.'
}
}
}
推荐阅读
- tiff - 如何确定 tiff 是否以 bigtiff 格式编写
- gatsby - 在没有 GraphQL 的情况下使用 gatsby-image?
- azure - 将 Azure 托管实例中的数据库从一个订阅恢复到另一个订阅
- image - Flutter:在 Dart 中合并两个图像以创建单个图像
- javascript - 使用 find 方法对 typescript 中的所有元素更改类数组属性值
- gremlin - Gremlin GraphTraversalSource 生成一个随机的 GraphTraversal
- c++ - 从 C++ 程序编辑 .exe 文件
- python - 通过单击另一个选项卡中的按钮来更改选项卡的 UI
- delphi - 允许用户从密码字段中复制文本
- c# - WPF 异步加载 UI 元素