Need guidance on merging files from stag barnch to prod branch ignoring certain files

Desh Deepak Dhobi
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 24, 2024

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:

bitbucket.png

Help me achieve my use case using the bitbucket pipelines

This is what I am trying to achieve:

  1. A pull request is made from stag to prod branch
  2. The bitbucket pipeline will sense that and start the run
  3. 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)
  4. The pipeline will move ahead with doing the actual merge and ignoring the appspec.yml, buildspec.yml file and scripts folder.

1 answer

0 votes
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 26, 2024

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

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events