Home > OS >  GIT push - Provide credentials without any prompts
GIT push - Provide credentials without any prompts

Time:06-12

Is there anyway to provide credentials to a git push without going through the git prompt for a username and password/token?

I am converting an automated Git push script from Java to Bash and my problem is that the first 'push' command will prompt for the username and password/token. I have executed the following commands to set the credentials:

git config --global user.name "your username"
git config --global user.password "your password"
git config credential.helper store

Here is the first pass at the bash script I am trying to execute:

#!/bin/bash
IFS='
'
for tbl in $(git remote show origin); do    
  if [[ "$tbl" == *"pushes to"* ]]; then
    branchName=$(echo "$tbl" | awk '{print $1}')
    echo "Pushing branch: $branchName"
    echo $(git push origin $branchName)
  fi
done

My version of git is 2.36 and this will be executed from within a docker container.

Do I need to create a custom git credential helper?

Thanks in advance for any help.

CodePudding user response:

First, as outlined in the Git FAQ, user.name is a personal name (that is, your human name), not a username. Similarly, user.password doesn't exist as a configuration option, and it's not secure to store passwords in your configuration file, so you should remove that.

If you're running this in a Docker container, the easiest thing to do is to pass the credentials in through the environment, and then read the credentials from the environment, as mentioned in the Git FAQ:

git config credential.helper \
    '!f() { echo username=author; echo "password=$GIT_TOKEN"; };f'

That will set the username to author and the password to whatever's in GIT_TOKEN.

If you don't want to set that credential helper permanently for the repository, say, because the repository is shared both inside and outside the container, then you can do your push like this instead:

git -c credential.helper= \
    -c credential.helper='!f() { echo username=author; echo "password=$GIT_TOKEN"; };f' \
    push origin $branch

That will first clear any existing credential helpers and then add the one from the environment.

Since you're using Docker, be sure to pass the environment variable as ENV, not ARG. ARG values are built into the container, and you don't want to embed secrets into your container.

  • Related