Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Deploy after Pull Request merge to target branch

First I have to point out that Atlassian has very bad technical support or. none, even for paying customers !!!! Very bad!

I don't think anyone here can help me, but I'll ask anyway.

I can't handle the bitbucket-pipeline.yaml configuration.

I need to deploy to my own VPS over SSH, but only after merging the pull request from into the develop or master branch.

Is it even possible? If not, can anyone advise an alternative solution? Unless If that's not possible, I'm afraid we'll have to move to CircleCI.

1 answer

1 vote
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Oct 19, 2021

Hi Dusan,

I can see that you have created a support ticket for this issue and our engineers have responded there. If you have any further questions, you can let us know in the support ticket and we can assist there.

I just wanted to leave a reply here as well for any other users who may come across your post:

There is no direct way to trigger Pipelines after a PR is merged, based on the destination branch. Pipelines defined for pull-requests run when a PR is created and are based on the source branch. We have a feature request for what you are asking in our issue tracker:

In the meantime, you can look into either of the following options to work around this issue:

1st Option - Use Branch Triggers and a script to check if the commit is a merge commit.

Firstly, you would have to use branch triggers and specify the branch. This can be configured in the "bitbucket-pipelines.yml" file.
As per the config below, the build will only run only when the commit is pushed to the master / develop branch.

Once that condition is met, you can add a step using a script to check if the commit the build is running on is a merge commit or not. A merge commit will always have two parent commits.

This is only on the condition that the commit is merged using the "Merge Commit" strategy. The reason is with Squash Merge, we can't identify if it's a merge commit as it does not have two parent commits. If you are using Squash merge, this option wouldn't be a solution.

The script checks if the commit is a "Merge Commit" by validating that it has two parent commits, then only it will proceed with the next steps, else it will exit the build.

pipelines:
  branches:
    master:
      - step:
          name: Branch Trigger to Master
          script:
            - echo "Checking Merge Commit"
            - chmod +x MergeCommitCheck.sh && ./MergeCommitCheck.sh
            - <deployment commands>
    develop:
      - step:
          name: Branch Trigger to Develop
          script:
            - echo "Checking Merge Commit"
            - chmod +x MergeCommitCheck.sh && ./MergeCommitCheck.sh
            - <deployment commands>

scirpt:

#!/bin/bash

parent_commit_count=$(git cat-file -p $BITBUCKET_COMMIT | grep -o -i parent | wc -l)
echo $parent_commit_count

if [ "${parent_commit_count}" = "2" ]
then
    echo "Commit $BITBUCKET_COMMIT has two parents. Hence this is a merge commit"
    echo "Proceeding with build"
else
    echo "Commit $BITBUCKET_COMMIT is not a merge commit, Exiting Build"
    exit 1 
fi

2nd Option - Implement Branch Permissions that only allows a Merge Via Pull Request.

This approach depends on how your workflow is designed. With this approach, the workflow should be to avoid direct pushes to "master" and "develop".

Here, you can implement branch permissions that allow only "Merge via Pull Request".
You will have to block users from writing directly to the branch.
This way, that each time a commit is written into the "master" OR "develop" branch, it's through a PR Merge. Hence we can be assured that only merge commits are allowed into this branch.

Then you can configure Bitbucket Pipelines to use branch triggers to run your deployment similar to the config above but excluding the need to use a script.

You can read more about Branch Permissions here:

https://support.atlassian.com/bitbucket-cloud/docs/use-branch-permissions/

Kind regards,
Theodora

Thank you for that.

In my situation I needed to deploy some files to a server after a pull request merges the pull request (source) branch into the target branch (master in my case); the directory on the server is based on the PR's source branch name.  As I understand it pull request pipelines do not currently support that strategy.  Since Bitbucket Pipelines follows a (pull request #n) comment pattern (see below), I was able to naively hook into it at the master pipeline level.

Merged in feature/someone/something (pull request #777)

PR_SOURCE=$(git log -1 --merges origin/master --format="%s" --grep '(pull request #.*)' | sed -e 's/Merged in //g' -e 's/(pull request #.*)//g' | xargs);

The above is what I used to retrieve the branch name of the last pull request.

@Theodora Boudale That issue was created in 2017? And it's status is still a suggestion? Is there any timeline on fixing the workaround(s) above?

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Jul 11, 2023

Hi,

The feature request is still open, I'm afraid I don't have an ETA to share. Implementation of features is done as per our policy here:

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events