2

I am running git commands remotely through the SSH protocol as the server doesn't accept HTTPS. I am running it through a bash script and would prefer if I could pass in a username and password as variables.

More specifically, I am doing a migrate of repositories and am having issues when running

git remote add origin ssh://${username}:${password}@server.com/repo.git

I am running the script through a Jenkins job and am not able to easily prompt for passwords. Is my best option to copy keys to the remote server?

2 Answers 2

3

Assuming you're using a recent version of Git, you can use the GIT_SSH_COMMAND environment variable:

  GIT_SSH, GIT_SSH_COMMAND

      If either of these environment variables is set then git fetch
      and git push will use the specified command instead of ssh when
      they need to connect to a remote system. The command will be
      given exactly two or four arguments: the username@host (or just
      host) from the URL and the shell command to execute on that
      remote system, optionally preceded by -p (literally) and the port
      from the URL when it specifies something other than the default
      SSH port.

      $GIT_SSH_COMMAND takes precedence over $GIT_SSH, and is
      interpreted by the shell, which allows additional arguments to be
      included.  $GIT_SSH on the other hand must be just the path to a
      program (which can be a wrapper shell script, if additional
      arguments are needed).

      Usually it is easier to configure any desired options through
      your personal .ssh/config file. Please consult your ssh
      documentation for further details.

So, before you call git, you can simply:

export GIT_SSH_COMMAND='ssh -i /path/to/key'

If you can't use an SSH key, you can use the the Jenkins Credential Binding plugin.

Sign up to request clarification or add additional context in comments.

1 Comment

How would the credentials binding work with GIT_SSH_COMMAND? Or can you just wrap a git command in Jenkins Credentail binding with your ssh key?
1
  1. Another way would be to use the credentials binding plugin. This plugin allows credentials to be bound to environment variables to use from miscellaneous build steps.

  2. You could always just login to the remote box and add the Github credentials manually

Cheers

2 Comments

um, I suggest you re-read the question. OP explicitly said https is not allowed.
Removed my 1st option. Thanks for catching that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.