How to use git credentials from Jenkins pipeline input into docker file? How to use git credentials from Jenkins pipeline input into docker file? jenkins jenkins

How to use git credentials from Jenkins pipeline input into docker file?


You should pass your git username and password as environment variables during docker build and then call these variables inside Dockerfile.

Example Dockerfile -

FROM testARG usernameARG passwordRUN git clone https://${username}:${password}@github.com/private-repo-name.git

Build command:

docker build --build-arg username=$git_username --build-arg password=$git_password -t <your tag> .


Do you have the requirement to always prompt for the credentials?If not, you could store them in the Jenkins credential store and retrieve them via withCredentials step from the Jenkins Credentials Binding plugin. That way they are hidden in the logs if you do the build within the closure.

withCredentials([usernamePassword(  credentialsId: 'privateGitCredentials',  usernameVariable: 'USERNAME',  passwordVariable: 'PASSWORD')]) {  sh "docker build --build-arg username=$USERNAME --build-arg password=$PASSWORD -t <your tag> ."}