amazon-web-services - 从 jenkins SSH 到 ec2 并运行命令
问题描述
我正在尝试配置我的 Jenkinsfile,以便我的主 jenkins ssh 进入远程 ec2 服务器并在远程服务器上运行命令。到目前为止,我已将我的主 jenkins 服务器公钥添加到我的远程 ec2 服务器的 authorized_keys 列表中,并且我能够通过 ssh 进入我的远程服务器。我的 Jenkinsfile 的相关部分:
echo "===> about to SSH into the dev environment.."
sh '''#!/bin/bash
echo "===> in bash script now"
ssh -tt ubuntu@xxx.xx.xx.xxx
ls
pwd
git pull origin master
rm -rf node_modules
npm install
node app.js
'''
我知道我能够 ssh 进入我的远程 ec2 实例,因为ls
在我的远程服务器上打印文件的内容。
但是,pwd
打印出来/var/lib/jenkins/workspace/jenkinsfile_master
这意味着我仍在我的 jenkins 主服务器上。此外,我的git
和npm
命令不会运行,因为git
并且npm
没有安装在我的 Jenkins 主服务器上。
因此,我的第一个问题是我真的 ssh 到我的远程服务器吗?如果我这样做了,为什么要pwd
在我的 Jenkins 服务器而不是我的远程服务器上打印工作目录?其次,如何才能真正在远程服务器上运行命令?
解决方案
推测您遇到的每个命令的行为都被单独调用到命令行(这意味着每个命令都作为不同的会话运行)。如果是这种情况,您需要在没有任何其他插件的情况下将它们全部链接起来,例如下面的示例。
ssh -tt ubuntu@xxx.xx.xx.xxx && ls && pwd && git pull origin master .....
有一个名为SSH Steps的 Jenkins 插件,它允许您运行每个命令,如下所示
sshCommand remote: remote, command: "ls"
sshCommand remote: remote, command: "pwd"
sshCommand remote: remote, command: "git pull origin master"
或者,如果您不想使用此插件,AWS 还有一个名为Run Command的功能,允许您使用 AWS CLI/SDK 在服务器上远程调用命令。有了这个,当您使用git pull
命令时,您需要确保远程服务器的磁盘上存在凭据。
推荐阅读
- excel - 谁能解释FC的输出?
- android - NDK 解析结果:项目设置:Gradle 模型版本=5.4.1,NDK 版本为 UNKNOWN 错误
- powerbi - 如何在 RELATEDTABLE 列的筛选行上使用 DATEDIFF() 创建计算列?
- python-3.x - 链接到另一个 python C 扩展
- php - 如何对一个查询使用多个 if
- python - 使用稀疏向量进行查询的 Elasticsearch 运行时错误
- webpack - 如何在不使用 webpack 生成新块的情况下拆分 webpack 条目
- android-studio - 在没有白色背景的android studio中显示imageview
- c - 一次释放堆上多个区域的机制
- java - Hybris 后台:WYSIWYG 中的图像配置不起作用