Jenkins Multi-Branch Pipeline Trigger Fails with KeyError: 'Location' in Bitbucket Pipeline

Jam Nadeem December 23, 2024

Description:

I’m encountering an issue while triggering a Jenkins multi-branch pipeline from a Bitbucket Cloud pipeline using the atlassian/jenkins-job-trigger pipe. The pipeline fails with a KeyError: 'Location' when attempting to retrieve the Location header from the Jenkins API response. Below are the detailed logs, pipeline configuration, and troubleshooting steps I have tried so far.

Pipeline Script

Screenshot 2024-12-24 at 11.21.53 AM.png

Bitbucket Pipeline Error 

Screenshot 2024-12-24 at 11.23.07 AM.png

Error Logs:

Below are the detailed logs captured during the pipeline execution:

INFO: None INFO: Submitting jenkins job... DEBUG: Starting new HTTPS connection (1): jenkins.abccompany.com.au:443 DEBUG: https://jenkins.abccompany.com.au:443 "POST /job/dev-multi-branch-pipeline/build HTTP/11" 302 0 DEBUG: https://jenkins.company.com.au:443 "GET /job/dev-multi-branch-pipeline/ HTTP/11" 200 None Traceback (most recent call last): File "/pipe.py", line 200, in <module> pipe.run() File "/pipe.py", line 191, in run self.wait_for_job(build_response.headers['Location']) File "/usr/local/lib/python3.10/site-packages/requests/structures.py", line 52, in __getitem__ return self._store[key.lower()][1] KeyError: 'location' N/A: version "v21.1.0" is not yet installed

 

Issue Details:

  • Problem: The atlassian/jenkins-job-trigger pipe expects a Location header in the response from Jenkins, but this header is missing. The pipe fails when attempting to access build_response.headers['Location'].
  • Expected Behavior: Jenkins should return a Location header pointing to the queue item or build status URL after the job is triggered.
  • Observed Behavior: Instead of the expected Location header, the Jenkins API responds with an HTTP 302 redirect to the job URL and no further details.

 

Troubleshooting Steps Tried:

  1. Verified Jenkins API Response:

  2. Adjusted Pipeline Configuration:

    • Verified that JOB_NAME is correctly pointing to the multi-branch pipeline.
  3. Checked Jenkins Logs:

    • No errors related to API requests or authentication were found in jenkins.log.
  4. Updated Plugins:

    • Ensured the following plugins are up-to-date:
      • Pipeline: Multi-branch
      • Bitbucket Branch Source
      • REST API Plugin
  5. Verified CSRF Settings:

    • Checked that CSRF protection is enabled and tested with both tokens and crumbs. No difference in behavior was observed.
  6. Tried Alternative API Endpoints:

    • Attempted to query queue items directly, but the expected build process did not start.

Questions/Help Needed:

  1. Why is the Location header missing in the Jenkins API response?
  2. Is this a known issue with Jenkins' handling of multi-branch pipelines when triggered via the REST API?
  3. Are there any workarounds or alternative methods to reliably trigger and monitor Jenkins multi-branch pipeline jobs from Bitbucket Cloud?
  4. Could this be a misconfiguration in Jenkins or a limitation of the jenkins-job-trigger pipe?

1 answer

1 accepted

1 vote
Answer accepted
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 26, 2024

Hello @Jam Nadeem ,

and welcome to the Community!

It may be that Jenkins is redirecting the request to the login page because the credentials are incorrect. When you sent a manual request using curl and received an HTTP 302 (redirect), did the job on Jenkins correctly trigger? What is the URL it's redirecting you to?

Also, the Location header might not be supported in case you're using a considerably old version of Jenkins. Could you confirm which exact Jenkins version you're using ?

Thank you, @Jam Nadeem !

Patrik S

Jam Nadeem January 9, 2025

Thank you  @Patrik S I fixed the issue 

Like Patrik S likes this
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 10, 2025

That's great @Jam Nadeem .

If you feel comfortable in sharing the solution here in the community, feel free to do so, as it can help other users that may come across the same issue in the future.

Thank you, @Jam Nadeem !

Patrik S

Jam Nadeem January 12, 2025

@Patrik SI identified and fixed an issue in the Bitbucket pipeline script where the JOB_NAME prefix was incorrect.

Incorrect Format:
JOB_NAME: "job/dev-multi-branch-pipeline/"

Correct Format:
JOB_NAME: "job/dev-multi-branch-pipeline/job/$BITBUCKET_BRANCH"

 

Like Patrik S likes this
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 13, 2025

Awesome! Really appreciate your time in sharing the solution here :)

If you ever need help in the future, feel free to reach out to the community!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin Site Admin
TAGS
AUG Leaders

Atlassian Community Events