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:[email protected]/des*******/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:
Have anyone gone through this use case.
This is what I am trying to achieve:
- A pull request is made from stag to prod branch
- The bitbucket pipeline will sense that and start the run
- There will be a manual approval before the main merge (The developer will check the changes and trigger the manual approval for the actual merge)
- The pipeline will move ahead with doing the actual merge and ignoring the appspec.yml, buildspec.yml file and scripts folder.