Making the path seen by SSH from Jenkins the same as that for a "user" SSH shell login

Apologies if this is answered somewhere here already, I know it is an old question.

The path that a Jenkinsfile sees when SSH’ing into an agent to run a command with sh is different from that seen by a normal user SSHing into the same agent, something to do with interactive versus non-interactive shells. This is a pain in the bum as there are things added to the path by ~/.profile that matter, like the path to the user’s Python scripts in ~/.local/bin.

Without me having to rejigger where everything is kept on all my agents, or start hard coding paths in strangely named and obscure files, is there a setting somewhere I’ve missed in Jenkins which makes it log in as an interactive SSH shell, making the two environments the same? This would save me hours of banging my head against a brick wall in the future.

For anyone else who has this problem, here is an excellent article which explains the subtleties of login/interactive/non-interactive shells on Linux and what environment is read by each:

By experiment, for whatever reason, when Jenkins makes an SSH connection to a Linux agent the environment it gets does NOT include the contents of the .profile file of the user it is logging-in as; you can tell this by looking at the environment variables that Jenkins helpful prints when it launches the agent, any stuff added in the user’s .profile is absent.

The way to fix this is to go into the agent configuration, find the Advanced tab of the SSH Launch Method for the agent, and in the Prefix Start Agent Command enter:

. ~/.profile &&

This will cause the contents of .profile to be sourced at the point of login and all is good with the world, no bruising-of-the-forehead required :slight_smile: .