unable to use sshagent in jenkinsfile to run on jenkins master

33 Views Asked by At

Team, I am observing that sshagent is loading the private key file however during git push it is using default user jenkins and failing to talk to git server. any hint how can I assure it uses user from the key it is loading?

SSH_AGENT_PID=3342
Running ssh-add (command line suppressed)
Identity added: /home/jenkins/agent/workspace/submit@tmp/private_key_14400311938405161904.key (/home/jenkins/agent/workspace/submit@tmp/private_key_14400311938405161904.key)
[ssh-agent] Started.
[Pipeline] {
[Pipeline] sh
+ date +%T
01:51:12
+ git push origin HEAD:main
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
[Pipeline] }
$ ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 3342 killed;
[ssh-agent] Stopped.
        stage('Push') {
            steps {
                sshagent(credentials: ['git-ssh']) {
                       sh '''
                            date "+%T"
                            git push origin HEAD:$TARGET_BRANCH
                            date "+%T"
                        '''.stripIndent()
                 }
            }
        }
    }
0

There are 0 best solutions below