Git push in automation
Configure
sshto proceedgit pushsuccessfully on dispensable automation servers.
By default, ssh prompts message to ask if adding the current server host key into known_hosts
when first time connecting a certain server.
Read more here.
This could also cause git push failure in automation jobs on a new server/container,
if your git repo is configured to push via SSH.
In jenkins job, it usually ends up with such error:
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
even the ssh_key is totally valid and has needed privilege.
So in pipeline, using withEnv to replace GIT_SSH_COMMAND like:
withEnv(['GIT_SSH_COMMAND=ssh -o StrictHostKeyChecking=no']) {
  sshagent(credentials: ['your-git-ssh-key']) {
    // Your git push command
    // sh "git push origin ${some_branch}"
  }
}
Or set this environment var before your git command in shell:
$ GIT_SSH_COMMAND='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  git fetch origin --tags
END
    Written on May 23, 2019