How can I ensure an application is correctly found on the path with Ubuntu SSH

65 views Asked by At

Context

I have a shell script which is called from Jenkins remotely via SSH. If I SSH to the user account manually and then run the script it works. However, when Jenkins does this (same user account but executes command as an argument to the SSH command) it fails to find an application on the path.

Environment

lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16.04.5 LTS Release: 16.04 Codename: xenial

uname-a Linux 4.4.0-1066-aws #76-Ubuntu SMP Thu Aug 16 16:21:21 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Contents of Home Directory

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

Contents of .profile:

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

Contents Of Failing Script

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)

How The Script Is Run

In a Jenkinsfile we have this:

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

Observed Result Of Running The Script

./run-apps.sh: line 12: aws: command not found

Other Information

If I SSH to the server using the same user that Jenkins uses I can run the AWS CLI:

whoami
app

and

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

finally:

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

Is this perhaps something to do with the use of eval and the environment not being propagated or something, e.g. the .profile isn't run and therefore the application isn't on the path.

0

There are 0 answers