Connecting Bitbucket and Jenkins

Alexander Spetko September 7, 2012

Hi,

I am using git (at bitbucket) as a source code repository. Since I could not make the git jenkins plugin run, I have set up a shell skript (on Linux Ubuntu) that checks out my source code from git.

At the moment, I am running this job every day, but I was wondering, if it is possible to connect the git repository to the jenkins task, as some kind of hookup.

So every time, I change something on the source files, the build will be triggered to run.

This is interesting for older projects, where are not so many changes and therefore I don't need to run a build on a daily base.

Thanks in advance and please excuse my english. It 's not my mother language.

Best regards,

Alexander Spetko

29 answers

Suggest an answer

Log in or Sign up to answer
3 votes
Claudio De Pace November 6, 2012

OK. I'm getting close. The error message was not clear. I've fixed the git executable directory path and now the new message is

Failed to connect to repository : Command "/usr/local/git/bin/git ls-remote -h git@bitbucket.org:myname/myproject.git HEAD" returned status code 128: stdout: stderr: Host key verification failed. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists

dinesh_arumugam July 17, 2018

Hi Claudio,

when connecting to Bitbucket from jenkins repo
the actual url given by bit bucket is "ssh://git@bitbucket.org:dinesh_arumugam/vsci.git"
the above url will give the beow error:

Failed to connect to repository : Command "git ls-remote -h ssh://git@bitbucket.org:your_name/vsci.git HEAD" returned status code 128:
stdout:
stderr: ssh: Could not resolve hostname bitbucket.org:dinesh_arumugam: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

To solve the above error just change the bitbucket repo link to
ssh://git@bitbucket.org/your_name/vsci.git
by just replacing the ':' in to slash '/' before your name 

to configure it in jenkins jobs in the GIT repo option.

jenkins-config-1.png

then try to run the run the below command in your terminal
"git ls-remote -h ssh://git@bitbucket.org/your_name/vsci.git HEAD"

just replace the ':' into a slash '/'

once the command is run you will get it working in the jenkins tool
and your ssh will be generated.

jenkins-config-edited.png

regards,

Dinesh Arumugam

Kanthi Saranya Remella August 6, 2018

Hi,

Im facing similar issue. But when I ran the "git ls-remote -h ssh://git@bitbucket.org/your_name/vsci.git HEAD" command, it doesn't return anything. No error, nothing.. Can you pls help.

Like KosmosasRaketa likes this
1 vote
Claudio De Pace November 6, 2012

I'm not sure THAT is the problem cos I've changed the permissions for the the project directory with

chmod 777 MyProjectAdHocBuild

but still....nothing....

at hudson.model.Executor.run(Executor.java:236)

Caused by: hudson.plugins.git.GitException: Command "/usr/local/git/bin/git clone --progress -o origin git@bitbucket.org:myname/myproject.git /Users/Shared/Jenkins/Home/jobs/myprojectAdHocBuild/workspace" returned status code 128:

stdout: Cloning into '/Users/Shared/Jenkins/Home/jobs/myprojectAdHocBuild/workspace'...

stderr: Host key verification failed.

fatal: Could not read from remote repository.

Please make sure you have the correct access rights

and the repository exists.

0 votes
Vivienne Ansah March 20, 2021

I resolved connecting Bitbucket and Jenkins by following the steps below:

I created a private and public key using ssh-keygen.

In Bitbucket

Save your public Key in your Bitbucket Account  under https://bitbucket.org/YOURUSERNAME/test/admin/access-keys/

In Jenkins

  1. I navigated to credentials section under Manage Jenkins > Manage Credentials .
    Screenshot 2021-03-20 at 10.31.46.png
  2. Click Add Credentials
  3. Paste your private key and click okay.
    Screenshot 2021-03-20 at 10.35.15.png
  4. Go to the job and assign your private key to the git repo.
  5. Run your job and it should be successful.
    Screenshot 2021-03-20 at 10.39.13.png
0 votes
Green Elephant October 4, 2018

You can use plugin https://marketplace.atlassian.com/1219418 for  integrate Bitbucket and Jenkins

0 votes
Santhosh Reddy October 15, 2013

Hi,

I am completely new to Bitbucket and we have migrated from SVN, i am looking for detailed steps of integrating Bitbucket and jenkins for build setup, is there any detailed documentation available?

Can you please help in setting this process?

Thanks.

0 votes
Claudio De Pace November 8, 2012

OK,fixed it. I was entering the branch name in the configuration area in lower case and that field is case sensitive. Pretty stupid error,but nasty.


0 votes
Claudio De Pace November 7, 2012

Ok maybe I found the solution here: http://colonelpanic.net/2011/06/jenkins-on-mac-os-x-git-w-ssh-public-key/

My problem now is different

Started by user anonymous

Building in workspace /Users/Shared/Jenkins/Home/jobs/myprojectAdHocBuild/workspace

Checkout:workspace / /Users/Shared/Jenkins/Home/jobs/myprojectAdHocBuild/workspace - hudson.remoting.LocalChannel@25f3b536

Using strategy: Default

Fetching changes from 1 remote Git repository

Fetching upstream changes from git@bitbucket.org:myuser/myproject.git

No candidate revisions

ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.

Archiving artifacts

Finished: FAILURE

At least I found how to check it out

0 votes
Claudio De Pace November 7, 2012

Maybe this can be useful:

MacMini:~ myuser$ cat /Library/LaunchDaemons/

com.logmein.logmeinserver.plist com.microsoft.office.licensing.helper.plist org.jenkins-ci.plist

MacMini:~ myuser$ cat /Library/LaunchDaemons/org.jenkins-ci.plist

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>StandardOutPath</key>

<string>/var/log/jenkins/jenkins.log</string>

<key>StandardErrorPath</key>

<string>/var/log/jenkins/jenkins.log</string>

<key>EnvironmentVariables</key>

<dict>

<key>JENKINS_HOME</key>

<string>/Users/Shared/Jenkins/Home</string>

</dict>

<key>GroupName</key>

<string>daemon</string>

<key>KeepAlive</key>

<true/>

<key>Label</key>

<string>org.jenkins-ci</string>

<key>ProgramArguments</key>

<array>

<string>/bin/bash</string>

<string>/Library/Application Support/Jenkins/jenkins-runner.sh</string>

</array>

<key>RunAtLoad</key>

<true/>

<key>UserName</key>

<string>jenkins</string>

<key>SessionCreate</key>

<true />

</dict>

</plist>

MacMini:~ myuser$

0 votes
Claudio De Pace November 7, 2012

This is the output:

Jenkins:~ vejenkins$ ps axu | grep "/jenkins"

vejenkins 11660 0.0 4.6 2918124 97096 ?? S 6:59pm 1:05.63 /usr/bin/java -jar /Users/vejenkins/Library/Caches/org.jenkins-ci.jenkins/jenkins.war

jenkins 9896 0.0 9.0 2939824 188552 ?? Ss 4:06pm 17:55.91 /usr/bin/java -jar /Applications/Jenkins/jenkins.war

vejenkins 11930 0.0 0.0 2432768 588 s000 S+ 10:28am 0:00.00 grep /jenkins

Jenkins:~ vejenkins$ ps axu | grep tomcat

vejenkins 11932 0.0 0.0 2432768 588 s000 S+ 10:28am 0:00.00 grep tomcat

Jenkins:~ vejenkins$

0 votes
Claudio De Pace November 7, 2012

vejenkins is my user but it looks like the java process is running under another user called jenkins

0 votes
Thomas Einwaller November 6, 2012

what user do you use to start the tomcat jenkins is running in and how do you start tomcat?

running

ps axu | grep tomcat

should show what user the tomcat is running under

0 votes
Claudio De Pace November 6, 2012

I think the solution is here:

http://stackoverflow.com/questions/12097982/jenkins-finds-cannot-find-ssh-key

but I cannot find a way to implement all this command on the Macosx console....

It's definitely an user permission problem....any idea?

0 votes
Claudio De Pace November 6, 2012

found something here: https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin

but how to find the user Jenkinds is running as? that tomcat6 referenced in the documentation is definitely wrong

0 votes
Claudio De Pace November 6, 2012

Yes I have done but still error. I think the problem are in the permission for git. In the error log the command which is failing is

/usr/local/git/bin/git clone --progress -o origin git@bitbucket.org:myname/myproject.git/Users/Shared/Jenkins/Home/jobs/MyProjectAdHocBuild/workspace

If I try to run it in the console I get this:

fatal: could not create work tree dir '/Users/Shared/Jenkins/Home/jobs/MyProjectAdHocBuild/workspace'.: Permission denied

so if I run it in SUDO mode it works.

How can I give to Jenkings the permission to run git in Admin mode?

0 votes
Thomas Einwaller November 6, 2012

have you tried restarting jenkins after adding the host do the authorized hosts?

is the jenkins running under the same user?

0 votes
Thomas Einwaller November 6, 2012

can you call

git clone git@bitbucket.org:myname/myproject.git

from the command line?

0 votes
Claudio De Pace November 6, 2012

yes it's working

Jenkins:~ vejenkins$ git clone git@bitbucket.org:myname/myproject.git

Cloning into 'myproject'...

remote: Counting objects: 5494, done.

remote: Compressing objects: 100% (4113/4113), done.

Receiving objects: 19% (1071/5494), 13.12 MiB | 529 KiB/s

also if I run:

/usr/local/git/bin/git ls-remote -h git@bitbucket.org:myname/myproject.git HEAD

it doesn't give any error message. It looks like Jenkings cannot run the command


0 votes
Thomas Einwaller November 6, 2012

use git@bitbucket.org:myname/myproject.git as repository URL (without the ssh://)

0 votes
Claudio De Pace November 6, 2012

Ok I've done it

Jenkins:~ vejenkins$ ssh git@bitbucket.org

PTY allocation request failed on channel 0

conq: logged in as myname

You can use git or hg to connect to Bitbucket. Shell access is disabled.

Connection to bitbucket.org closed.

but it keeps on failing. In the URL config are I've provided it as ssh://git@bitbucket.org:myname/myproject.git

but the error is

stderr: ssh: Could not resolve hostname bitbucket.org:myname: nodename nor servname provided, or not known

0 votes
Claudio De Pace November 6, 2012

Done. Back to the other error

Failed to connect to repository : Command "/usr/local/git/bin/git ls-remote -h git@bitbucket.org:myname/myproject.git HEAD" returned status code 128:
stdout:
stderr: Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I really don't know how to fix it....

0 votes
Thomas Einwaller November 6, 2012

Host key verification failed

you need to make sure that the host key is in the allowed hosts. Simple do this by running the command

ssh git@bitbucket.org

and answer the question that asks to add the host with yes

0 votes
Thomas Einwaller November 6, 2012

use the url that is shown on the bitbucket repository front page, something like

git@bitbucket.org:blindjustis/public.git

0 votes
Claudio De Pace November 6, 2012

shall I use

ssh://git@bitbucket.org

0 votes
Claudio De Pace November 6, 2012

Hi Thomas,

so..what I've done:

ssh-keygen -t rsa
 pbcopy < /Users/jenkins/.ssh/id_rsa.pub 

and I added this SSH key to my BitBucket profile.

but in the configuration area,when I use /https://myname@bitbucket.org/myname/myproject.git

I get the error: Failed to connect to repository : Error performing command: git ls-remote -h https://...</span<>>

0 votes
Thomas Einwaller November 6, 2012

You need to create a ssh key by running ssh-keygen, this creates a id_rsa and a id_rsa.pub file in the directory ~/.ssh

then you have to add the content of the id_rsa.pub file to the bitbucket repository und deployment keys

TAGS
AUG Leaders

Atlassian Community Events