Issues connecting to bitbucket in pipeline

Hey, I am using Jenkins multi-agent setup with both Linux and Windows agents. My version control system is BitBucket. I am currently trying to clone, checkout repos from jenkins scripted pipeline.

Credentials show that git repo is reachable in pipeline configurations. However, I tested from within the Windows agent that it needs manual input to authenticate. A window popups.

How can I overcome this and remove manual input? My current pipeline fails because it does not get reply from that manual input.

Any ideas would really help.

Welcome back, @KarooolisZi. :wave:

To overcome the manual input for authentication when cloning and checking out repositories from a Jenkins pipeline, I guess you could use a credential manager or store the credentials in Jenkins and use them in your pipeline script (if that’s not already the case).
Here is an example of how to configure your Jenkins pipeline to use stored credentials for cloning a BitBucket repository:

1. Store Credentials in Jenkins:

  • Go to Jenkins Dashboard.
  • Navigate to Manage Jenkins → Credentials.
  • Add a new credential with your BitBucket username and password or an SSH key.

2. Use the Credentials in Your Pipeline Script:

pipeline {
    agent any
    stages {
        stage('Clone Repository') {
            steps {
                script {
                    // Use the credentials stored in Jenkins
                    withCredentials([usernamePassword(credentialsId: 'bitbucket-credentials-id', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
                        // Clone the repository using the stored credentials
                        if (isUnix()) {
                            sh 'git clone https://${USERNAME}:${PASSWORD}@bitbucket.org/your-repo/your-project.git'
                        } else {
                            bat 'git clone https://${USERNAME}:${PASSWORD}@bitbucket.org/your-repo/your-project.git'
                        }
                    }
                }
            }
        }
    }
}

You might be trying to use a Windows git client that is configured with a Windows credentials manager. Windows credential managers are designed to be used by a person that is working from a Windows Desktop so that they can respond to the periodic prompts from the credentials manager. Using the same configuration of command line git for a Jenkins agent can lead to authentication prompts from git for Windows.

I use MinGit for Windows on my Windows agents to avoid the risk of credential managers that interfere with command line operations from Jenkins. Instructions are included in the git client plugin documentation.

1 Like

Yes, I use stored credentials but the pipeline just stucks on that manual input step (I guess). Because there is no error telling about invalid credentials

1 Like

Hello thank you for an answer. Should this work after installation as per instructions? I had followed the steps to make it an automated installation yet my checkout still stuck on same step.

If your MinGit installation on the agent is the git implementation that is used by the agent, then yes, it should work after installation. If your agent is still using the git for Windows installation with a configured credential manager, then no, it will not change after installation.

When my jobs run on Windows agents, their console output includes lines like:

using credential markewaite-bitbucket-username-password
Cloning the remote Git repository
Avoid fetching tags
Cloning repository https://markewaite@bitbucket.org/markewaite/bin.git
 > C:\tools\MinGit-2.47.0.2\mingw64\bin\git.exe init C:\J\OX\workspace\Hosting-Providers\bitbucket\bitbucket-bin-https # timeout=10
Fetching upstream changes from https://markewaite@bitbucket.org/markewaite/bin.git

That output shows me that I am using MinGit 2.47.0.2 to clone from a private Bitbucket repository.

Interesting thing that I put Mingit path higher than old Git install and jenkins still picks older one up. Despite command prompt showing mingit version as default.
Perhaps you know how to work around that?

c:\jenkins-agent\workspace\test winagent and bitbucket>git --version
git version 2.39.2.windows.1
[Pipeline] bat

c:\jenkins-agent\workspace\test winagent and bitbucket>echo C:\Program Files\Eclipse Adoptium\jre-19.0.2.7-hotspot\bin;C:\Python311\Scripts;C:\Python311;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Amazon\cfn-bootstrap;C:\Program Files\Docker;C:\Program Files\Java\jdk-19\bin;C:\Program Files\Git\cmd;C:\Program Files\Amazon\AWSCLI\bin;C:\ProgramData\chocolatey\bin;C:\TDM-GCC-64\bin;C:\Program Files\CMake\bin;C:\Program Files\OpenSSL-Win64\bin;C:\vcpkg;;C:\Program Files\Docker\Docker\resources\bin;C:\Program Files\Go\bin;C:\Program Files\PowerShell\7;C:\Program Files\Mingit\mingw64\bin;C:\Windows\system32\config\systemprofile\go\bin
C:\Program Files\Eclipse Adoptium\jre-19.0.2.7-hotspot\bin;C:\Python311\Scripts;C:\Python311;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Amazon\cfn-bootstrap;C:\Program Files\Docker;C:\Program Files\Java\jdk-19\bin;C:\Program Files\Git\cmd;C:\Program Files\Amazon\AWSCLI\bin;C:\ProgramData\chocolatey\bin;C:\TDM-GCC-64\bin;C:\Program Files\CMake\bin;C:\Program Files\OpenSSL-Win64\bin;C:\vcpkg;;C:\Program Files\Docker\Docker\resources\bin;C:\Program Files\Go\bin;C:\Program Files\PowerShell\7;C:\Program Files\Mingit\mingw64\bin;C:\Windows\system32\config\systemprofile\go\bin
[Pipeline] bat

c:\jenkins-agent\workspace\test winagent and bitbucket>“C:\Program Files\Mingit\mingw64\bin\git.exe” --version
git version 2.47.0.windows.2

looks like I got correct git version in the pipeline yet it does not work. Stucks on checkout or cloning, too. I used mingit in that executable path.

@MarkEWaite @poddingue perhaps you know why it could behave like that?

No, I don’t know why it could behave like that. I’ve offered all the ideas that came to mind for me.

What I do not understand is this:

If I put such credential management in pipeline, it works on same winagent:

withCredentials([usernamePassword(credentialsId: ‘bitbucket-jenkins’,
usernameVariable: ‘USERNAME’,
passwordVariable: ‘PASSWORD’)]) {
def repoUrl = “https://${USERNAME}:${PASSWORD}@bitbucket.org/workspace/repo.git
checkout([
$class: ‘GitSCM’,
branches: [[name: ‘*/feature/test’]],
userRemoteConfigs: [[
url: repoUrl
]]
])

However, if I use SCM where pipeline configs are in jenkins UI and jenkinsfile in bitbucket, it is stuck on checkout step. Same credentials, same agent.

@MarkEWaite @poddingue it looks like the credentials taken by jenkins from jenkins credentials are fine as I inspect the logs.
The scm steps are properly made - jenkinsfile from bitbucket is taken. However, second time when jenkins checkouts to the same command inside the pipeline, it does not work.

I am using git configuration in jenkins pipeline SCM. Perhaps you see why the first time is working fine (before starting the pipeline) but inside the pipeline it is not good?

1 Like

it might be related to OS cause controller is starting (which is Linux) but pipeline steps are in windows agent.

However, I tried various git configs, editing many different fields. I still get the same output. Credentials are username and password and are 100% correct as I can use git commands on the same windows agent while specifying them in pipeline itself (not in Pipeline → Definition → SCM → Repositories → Credentials).

Pipeline → Definition → SCM → Repositories → Credentials configuration in UI provides stuck action as I understand due to manual input (I guess)