Configure Webhooks on Bitbucket Server for automated Jenkins job trigger.
Hello, we will configure the webhooks in the bitbucket server for the automated Jenkins build trigger.
We will integrate the Jenkins with Bitbucket Server to create a webhook so that a build is automatically triggered on any commit in the bitbucket repo. (You will need Jenkins v2.164.1+ with the credential plugin and git plugin installed. Bitbucket Server 5.5+)
Step 1: Configure the Bitbucket server plugin in Jenkins.
Go to Jenkins > Manage Jenkins > Manage Plugins > Available Tab > Search for Bitbucket Server.
Go ahead and click Install without restart.
Step 2: Configure Jenkins to work with Bitbucket Server.
Go to Jenkins > Manage Jenkins > Configure System. Because we have installed the bitbucket plugin there will be a Bitbucket Server Integration section.
Click on Add a Bitbucket Server Instance and add your Bitbucket server details.
Now to configure the personal access token (it is used to trigger a build, whenever a code is pushed in bitbucket) we need to configure it at the bitbucket side.
Step 3: Configure Personal Access Token for Webhook on Bitbucket.
Go to Bitbucket > Manage Account > Personal Access Token > Create Token. Make sure the token project permissions are set to Admin and create Token.
You’ll see the token on the next page. Copy the token in the clipboard. Navigate back to the Jenkins server and add the personal token. Make sure the Kind is selected as Bitbucket personal access token and paste the access token, add the description to it and click Add as shown below.
Add the personal access token in Jenkins. Next, add the Credentials (for build auth) this is the bitbucket credentials to perform git actions on the repo. After adding click on Test connection to check if our integration is configured correctly and save the configuration.
Now we will be creating our first pipeline.
Step 4: Create a Pipeline job to test the Bitbucket Server Integration.
Click New Item > Give Name to your project > Pipeline > OK
Now in the job configuration scroll down to Build Triggers, select Bitbucket Server trigger build after push.
Scroll down to the Pipeline section and select Pipeline script from SCM, in SCM select Bitbucket Server and then select your configured server details that we configured earlier and save.
Click the Build Now. You will see the pipeline getting executed.
Because we chose Bitbucket Server trigger build after push clicking saves automatically created the webhook inside of the bitbucket server. You can see this by going back into your bitbucket server.
Go to your Repository > Repository Settings > Webhooks
This will show up whenever anything is pushed to the repo.