首页 > 解决方案 > 如何确保在使用 Ubuntu SSH 的路径上正确找到应用程序

问题描述

语境

我有一个 shell 脚本,它通过 SSH 从 Jenkins 远程调用。如果我手动 SSH 到用户帐户,然后运行它的脚本。但是,当 Jenkins 执行此操作(相同的用户帐户但执行命令作为 SSH 命令的参数)时,它无法在路径上找到应用程序。

环境

lsb_release -a 没有可用的 LSB 模块。发行商 ID:Ubuntu 描述:Ubuntu 16.04.5 LTS 版本:16.04 代号:xenial

uname-a Linux 4.4.0-1066-aws #76-Ubuntu SMP 2018 年 8 月 16 日星期四 16:21:21 UTC x86_64 x86_64 x86_64 GNU/Linux

主目录的内容

ls -la
total 76
drwxr-xr-x 7 app  app   4096 Sep  3 07:49 .
drwxr-xr-x 4 root root  4096 Aug 19 14:31 ..
-rw------- 1 app  app  12235 Sep  3 07:52 .bash_history
drwx------ 3 app  app   4096 Aug 28 17:06 .cache
drwx------ 2 app  app   4096 Aug 22 16:07 .docker
-rw-rw-r-- 1 app  app   6064 Sep  1 15:19 docker-compose.yml
drwx------ 4 app  app   4096 Aug 22 14:37 .local
drwxrwxr-x 2 app  app   4096 Aug 28 17:15 .nano
-rw-rw-r-- 1 app  app     97 Aug 22 16:00 .profile
-rwxrw-r-- 1 app  app    177 Aug 28 07:45 run-all-apps.sh
-rwxrw-r-- 1 app  app    476 Aug 28 07:45 run-apps.sh
-rw-rw-r-- 1 app  app     66 Aug 28 17:15 .selected_editor
drwx------ 2 app  app   4096 Sep  3 07:49 .ssh
-rw------- 1 app  app  10496 Sep  3 07:49 .viminfo

内容.profile

cat .profile 
# set PATH so it includes user's private bin directories
PATH="$HOME/bin:$HOME/.local/bin:$PATH"

失败脚本的内容

cat ./run-apps.sh 
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

# Authenticate for ECR
eval $(aws ecr get-login --region eu-west-1 --no-include-email)

脚本如何运行

在 Jenkinsfile 中,我们有这个:

sshagent(credentials: ['server-ssh']) {
                        sh "ssh -o StrictHostKeyChecking=no -p 22 -l app ${ENVIRONMENT_HOST_NAME} './run-apps.sh'"
}

运行脚本的观察结果

./run-apps.sh:第 12 行:aws:找不到命令

其他信息

如果我使用 Jenkins 使用的同一用户 SSH 到服务器,我可以运行 AWS CLI:

whoami
app

which aws
/home/app/.local/bin/aws

最后:

aws --version
aws-cli/1.15.83 Python/2.7.12 Linux/4.4.0-1066-aws botocore/1.10.82

这可能与使用 eval 和未传播的环境有关吗,例如.profile未运行,因此应用程序不在路径上。

标签: bashssh.profile

解决方案


推荐阅读