The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Detect merge done through Pull Request

Jason Kim
August 20, 2024

Hi 
The scenario is as follows.
1.  A user makes a pull request through the Bitbucket UI

2. The use clicks on the "Merge" button

3. The merge occurs and the bitbucket-pipeline gets exectued

4. the bitbucket-pipeline code tries to detect if this particular  build got called because of merge.

I Tried detecting BITBUCKET_PR_ID variable but it doesn't work. 
I tried counting the number of parents but there are times when there are 2 headers or less where there was an actual merge.

How would you recommend detecting a merge?

Below is the bitbucket pipeline code

 

image: docker/compose:latest # Docker image with Docker Compose pre-installed

pipelines:
default:
- step:
name: Detect Merge Commit
image: atlassian/default-image:3 # Switch to an image with Git pre-installed
script:
- |
if [ -n "$BITBUCKET_PR_ID" ]; then
echo "This build was triggered by a pull request. PR ID: $BITBUCKET_PR_ID"
else
echo "This build was not triggered by a pull request."
fi
- git rev-parse HEAD # Output the current commit hash
- echo "Checking if the current commit is a merge commit..."
- git rev-list --parents -n 1 HEAD
- |
# Get the parent commits of the current commit
PARENTS=$(git rev-list --parents -n 1 HEAD)
# Count the number of parents
NUM_PARENTS=$(echo $PARENTS | wc -w)
# Check if there is more than one parent
if [ $NUM_PARENTS -le 2 ]; then
echo "Not a merge commit. Exiting..."
exit 0 # Exit the step if it's not a merge commit
fi
# If it's a merge commit, the following scripts will run
echo "Merge commit detected! Running merge-specific steps..."
echo "Deploying to staging environment..."
- |

 

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

0 votes
Answer accepted
Syahrul
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 22, 2024

G'day, @Jason Kim 

Welcome to the community!

First, could you tell me why you must locate the merge commit in your pipeline build? I want to understand your goals so we are on the same page.

That said, when a merge occurs, it should trigger a pipeline build based on that merge, so you don't have to locate a specific commit or PR as a merge.

Also, I noticed that you have paid workspace so if you want to get a quicker response, I suggest raising a ticket directly to us at our Support portal

Regards,
Syahrul

Jason Kim
August 24, 2024

I will raise the issue through the paid portal

Like Syahrul likes this
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events