Hello All,
I have a use case. I have two branches "stag" and "prod". The stag branch contains three files appspec.yml, buildspec.yml, commonfile.txt, and scripts folder.
The prod branch also contains files named the same appspec.yml, buildspec.yml, commonfile.txt, and scripts folder.
My use-case:
Here, I make a lot of changes in the files of the stag branch and then merge those on the prod branch. However, I want that every time I merge the stag branch with the prod branch, the files named buildspec.yml, appsepc.yml, and scripts folders remain the same on both branches and the changes to these files and folders are ignored every time a merge is done from the stag to the prod branch and vice versa.
I have accomplished this task locally using the "ours" merge driver. But I want this to happen using the bitbucket pipeline so that every time a pull request is made, a merge is done automatically with manual intervention and ignoring the appspec.yml, buildsepc.yml, and scripts folder.
This is my current bitbucket-pipelines.yml file:
pipelines:
pull-requests:
"**": # Matches all pull requests on any branch
- step:
name: Merge Pull Request Source into Target
image: atlassian/default-image:latest
script:
# Output source and target branch details
- echo "Pull Request triggered. Source Branch:$BITBUCKET_PR_SOURCE, Target Branch:$BITBUCKET_PR_DESTINATION"
# Clone the repository
- echo "Cloning the repository"
- git clone https://$BITBUCKET_USERNAME:$BITBUCKET_PASSWORD@bitbucket.org/de****/de*****.git
- cd deshMergeRepo/
# Fetch the branches involved in the PR
- git fetch origin $BITBUCKET_PR_SOURCE
- git fetch origin $BITBUCKET_PR_DESTINATION
# Checkout the target branch
- echo "Checking out the target branch:$BITBUCKET_PR_DESTINATION"
- git checkout $BITBUCKET_PR_DESTINATION
# Configure Git
- echo "Setting up Git configuration..."
- git config --global user.email "de****@gmail.com"
- git config --global user.name "de*****"
# Merge the source branch into the target branch without committing
- echo "Merging $BITBUCKET_PR_SOURCE into $BITBUCKET_PR_DESTINATION"
- git merge --no-commit origin/$BITBUCKET_PR_SOURCE || true
# Check for merge conflicts
- echo "Checking for conflicts"
- git diff --name-only --diff-filter=U || true # Check for conflicts
# Resolve conflicts if any
- echo "Resolving conflicts (if any) by keeping the destination branch version for specific files"
- git checkout --ours appspec.yml buildspec.yml scripts/ || true
- git add appspec.yml buildspec.yml scripts/ || true
# Commit the merge
- echo "Committing the merge"
- git commit -m "Merged $BITBUCKET_PR_SOURCE into $BITBUCKET_PR_DESTINATION, resolving conflicts as necessary" || true
# Push changes to the destination branch
- echo "Pushing changes to $BITBUCKET_PR_DESTINATION"
- git push origin $BITBUCKET_PR_DESTINATION
- echo "Merge completed successfully for pull request."
But every time the pipeline shows the error:
Help me achieve my use case using the bitbucket pipelines
This is what I am trying to achieve:
Hello @Desh Deepak Dhobi ,
and welcome to the Community!
The error you are receiving is because pull-request triggered pipelines are a special type of pipelines where additional to the clone of the repository during the build setup, the pull-request pipelines will also merge the source and destination branch to "mimic" what the code would look like once the merge actually happens. Since during this automatic merge there's a conflict, the pipeline is aborted.
That being said, since you are doing the clones and merges manually as part of your step's script, you can disable the automatic pipeline clone by adding this flag to your step :
pull-requests:
'**':
- step:
clone:
enabled: false
name: Build for PR
script:
- This pipelines will not clone the repository automatically
With this change, pipeline will not clone neither try to merge the changes during the Build Setup phase, allowing you to do it manually and with your custom commands in your step's script. You can learn more about the clone options in the Git Clone Behavior - Bitbucket Pipelines article.
I hope that helps! Let us know in case you have any questions.
Thank you, @Desh Deepak Dhobi !
Patrik S
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.