Git Clone Fails in Powershell Script During Jenkins Build


So I’m stumped after a day of troubleshooting.

My Jenkins Project will clone the repo where the jenkinsfile is stored in, the bitbucket repo is called “ifc4”.

One of the stages within the jenkinsfile will also clone a separate repo named “ifc3” which contains a single powershell script.

The following stage will run an execution command of the powershell script. The powershell script contains a command to clone the same initial repo, “ifc4”.

The issue here is that the script will not clone the repo “ifc4”; however when executing the powershell locally on the Linux build agent - the script will in fact clone repo “ifc4”.

This is what is shown on the jenkins console of the build:

Cloning into 'ifc4'...
fatal: could not read Username for '': No such device or address

Here’s my jenkinsfile:

pipeline { 
    agent {
        docker {
            image "xxxxxx" 
            label "xxxxxxx"
    environment {
        SHORT_COMMIT = "${GIT_COMMIT[0..7]}"
    stages {
       stage('ifc3: Checkout Repository') {
            steps {
              sh 'pwd'
                git credentialsId: 'service', branch: 'master', url: '' 
        stage('Build Change: Execute Script') {
            steps {
              sh 'pwd'
              pwsh "./buildChange.ps1"

Is the Jenkins agent running as the same user that you are running the script as? Also, the environment will generally not be loaded into PowerShell, can you check the environment variables when running the script manually and compare to what is there when the script is run in the Jenkins job?

Hey Alex,

  1. The jenkins agent is running as root - I’m also executing the script on the linux build agent as root

  2. Is there something I should be looking at specifically within the list of environment variables?

    • adding the command “Get-ChildItem -Path Env:” to the script and running it locally, i’m seeing the user & username as root.
    • now running the same script with the update via Jenkins provides all of the Jenkins, Build, and, GIT vars.

Are you using the same directory to clone the repo as was done in the initial clone? You may need to add a remote as origin if so, I don’t think the git plugin sets an origin remote.

Hey Alex,

Thanks for the help thus far!

That’s right - the powershell script is trying to clone the same repo as the initial clone within the same directory. During my troubleshooting, what I was seeing lead me to believe this was the issue; however, I couldn’t figure out a remedy.

How would I go about adding a remote as origin?

You can run this command to set the remote git remote add origin <URL>, but if its cloned already, why are you cloning again? Wouldn’t you just want to do a checkout of a specific revision or something?

So running the command right before the git clone command I get:

fatal: remote origin already exists

So the purpose of this build is to build files from the current commit, then building files from a different (older) commit, then running a comparison of the files (old vs new). When I run a git checkout within the powershell script, the current folder of the initial clone is being replaced with the new clone. I would like to have the folder from the initial clone and folder from the new clone in the same directory.

just fyi, by default declarative (pipeline {) automatically check out the repo associated with the pipeline script.

what is the type of credential “service”
is the url literally ‘’ `?

googling seems to say you either can’t use https urls, or that username isn’t provided to git credential helper.

Edit: Re-reading your message, it sounds like the powershell step is failing to checkout. Doesn’t that just mean that you don’t have a credential helper for raw git commands

I would say you either way to tell git to use ssh instead of http, and use an ssh agent credential block, or update the url its trying to checkout to include credentials.

I believe the git step actually provides its own temporary credential helper, so the credentials wouldn’t be cached.

1 Like

The source will by default be checked out into the workspace, there are options for the checkout that change this. I think you would want to specify a path to checkout to for the initial checkout and then checkout into another directory.

As trivial as it was, cloning the repo the second time within a dir() block (Specifying another directory here) in the jenkinsfile solved my issue.

Thanks guys for taking the time out to help me!!

1 Like