Reputation: 7520
Since about 1 week now, Bitbucket doesn't (?) send a request to my Jenkins server.
I've set it all up like this:
Endpoint
http://username:apitoken@jenkinshost/
username
= username in Jenkins
apitoken
= apitoken connected to the username in Jenkins
jenkinshost
= my host where I run Jenkins
Project name
is a project
Token
: The token I can setup in the per-project configuration.
I've done this according to this website: http://felixleong.com/blog/2012/02/hooking-bitbucket-up-with-jenkins
.
It did work, but it doesn't anymore. Did Bitbucket change something? How can I fix this?
Upvotes: 49
Views: 125455
Reputation: 486
Okay so I have successfully established the connection between Jenkins and Bitbucket server but I'm having issues getting into the directory on the ec2 server where I have cloned my bitbucket repo>
Below is my script and Jenkins Console output
pipeline {
agent any
stages {
stage('Checkout Git Repository') {
steps {
// Set up the checkout directory
sh 'cd /var/lib/jenkins/test'
sh 'git status'
sh 'ls -la /var/lib/jenkins'
// Check out the current branch of the Git repository
dir('/home/ec2-user/Sona-Grafana/BitBucket-Repo/observability/soc-poc') {
git url: 'https://[email protected]/emmanuel-sona/soc-jenkins.git'
}
}
}
I cannot cd into /home/ec2-user/Sona-Grafana/BitBucket-Repo/observability/soc-poc. It only allows me entry into /var/lib/jenkins/test
Docker-compose file used in spinning up the Jenkins server that runs on the EC2 instance
version: '3'
services:
jenkins:
container_name: ops_jenkins_server
image: jenkins/jenkins:lts-jdk11
ports:
- "8080:8080"
volumes:
- jenkins_home:/var/jenkins_home
- /opt/jenkins_container_volume:/var
- /home/ec2-user/Sonalake-Grafana/BitBucket-Repo/observability/soc-poc:/var/lib/jenkins
environment:
- JENKINS_OPTS="--httpPort=8080"
- JAVA_OPTS="-Djenkins.install.runSetupWizard=false"
docker:
image: docker:latest
privileged: true
volumes:
- /var/run/docker.sock:/var/run/docker.sock
volumes:
jenkins_home:
networks:
default:
driver: bridge
Upvotes: 1
Reputation: 41
I had this problem and it turned out the issue was that I had named my repository with CamelCase. Bitbucket automatically changes the URL of your repository to be all lower case and that gets sent to Jenkins in the webhook. Jenkins then searches for projects with a matching repository. If you, like me, have CamelCase in your repository URL in your project configuration you will be able to check out code, but the pattern matching on the webhook request will fail.
Just change your repo URL to be all lower case instead of CamelCase and the pattern match should find your project.
Upvotes: 1
Reputation: 1093
In order to build your repo after new commits, use Bitbucket Plugin.
There is just one thing to notice: When creating a POST Hook (notice that it is POST hook, not Jenkins hook), the URL works when it has a "/" in the end. Like:
URL: JENKINS_URL/bitbucket-hook/
e.g. someAddress:8080/bitbucket-hook/
Do not forget to check "Build when a change is pushed to Bitbucket" in your job configuration.
Upvotes: 31
Reputation: 12033
By iterating I learned that the Token field and the token in an endpoint can be the same. So I set them to be the same as the user token and it works! Also check that the user has privileges to make a job.
Anyway, you can check access.log and see if Bitbucket makes a try or not.
P.S. Also a link to Bitbucket Documentation. May some day it will become more useful.
Upvotes: 4
Reputation: 743
I was just able to successfully trigger builds on commit using the Hooks option in Bitbucket to a Jenkins instance with the following steps (similar as link):
The endpoint did not require inserting the basic HTTP auth in the URL despite using authentication, I did not use the Module Name field and the Project Name was entered case sensitive including a space in my test case. The build did not always trigger immediately but relatively fast. One other thing you may consider is disabling the "Prevent Cross Site Request Forgery exploits" option in "Configure Global Security" for testing as I've experienced all sorts of API difficulties from existing integrations when this option was enabled.
Upvotes: 14
Reputation: 3665
I am not familiar with this plugin, but we quite successfully use Bitbucket and Jenkins together, however we poll for changes instead of having them pushed from BitBucket (due to the fact our build server is hidden behind a company firewall). This approach may work for you if you are still having problems with the current approach.
This document on Setting up SSH for Git & Mercurial on Linux covers the details of what you need to do to be able to communicate between your build server and Bitbucket over SSH. Once this is done, with the Git Plugin installed, go to your build configuration and select 'Git' under Source Code Management, and enter the ssh URL of your repository as the repository URL. Finally, in the Build Triggers section, select Poll SCM and set the poll frequency to whatever you require.
Upvotes: 1
Reputation: 5807
I had a similar problems, till I got it working. Below is the full listing of the integration:
ssh-keygen -t rsa
Copy the public key (~/.ssh/id_rsa.pub) and paste it in Bitbucket SSH keys, in user’s account management console:
Copy the private key (~/.ssh/id_rsa) to new user (or even existing one) with private key credentials, in this case, username will not make a difference, so username can be anything:
run this command to test if you can get access to Bitbucket account:
ssh -T [email protected]
git clone [email protected]:username/repo_name.git
Now you can enable Bitbucket hooks for Jenkins push notifications and automatic builds, you will do that in 2 steps:
Add an authentication token inside the job/project you configure, it can be anything:
In Bitbucket hooks: choose jenkins hooks, and fill the fields as below:
Where:
**End point**: username:usertoken@jenkins_domain_or_ip
**Project name**: is the name of job you created on Jenkins
**Token**: Is the authorization token you added in the above steps in your Jenkins' job/project
Recommendation: I usually add the usertoken as the authorization Token (in both Jenkins Auth Token job configuration and Bitbucket hooks), making them one variable to ease things on myself.
Upvotes: 18