ForwardAgent in Jenkins
Solution 1:
I see this question has been unanswered for over a year, but here is how I solved the problem.
What you want to do is make sure that the user that runs jenkins
- checks to see if ssh-agent is running (if not, start it)
- checks to see if a key is loaded (if not, load one)
Put this in your ~/.bash_profile for the user that runs jenkins of the user that needs to forward agent, to ensure it runs with each new shell:
SSH_ENV="$HOME/.ssh/environment"
function start_agent {
echo "Initializing new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
ssh-add .ssh/id_rsa
cat .ssh/id_rsa.pub
}
#Source SSH Settings
if [ -f "${SSH_ENV}" ]
then
. "${SSH_ENV}" > /dev/null
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent > /dev/null || {
start_agent;
}
else
start_agent;
fi
if [ `ssh-add -l | grep "The agent has no identities." | wc -l` == 1 ]
then
ssh-add .ssh/id_rsa
cat .ssh/id_rsa.pub
fi
About 50% of the code here I took from somewhere else but can't remember where to give credit. This should be fairly portable, and its use need not be limited to jenkins it should work in any ssh-agent forwarding situation.