12

I have a repository with two branches: master and Dev and I want to configure that pipline in such a way that when I push code to Dev branch and code build was successfull, the Dev was merged to master. Unfortunatly I can't find any information about merge in bitbucket piplines docs.

That's my yml file:

pipelines:
  branches:
    Dev:
      - step:
          script:
            - ant deployCodeCheckOnly -Dsf.username=$SF_USERNAME -Dsf.password=$SF_PASSWORD

Could somebody help me with that case? If it possible?

--Edit

I try to change script as sugest:

pipelines:
  branches:
    Dev:
      - step:
          script:
            - ant deployCodeCheckOnly -Dsf.username=$SF_USERNAME -Dsf.password=$SF_PASSWORD
            - git remote -v
            - git fetch
            - git checkout master
            - git merge Dev
            - git push -v --tags origin master:master

Result:

git remote -v
+ git remote -v
origin  git@bitbucket.org:repository/project.git (fetch)
origin  git@bitbucket.org:repository/project.git (push)

git fetch origin
+ git fetch origin
Warning: Permanently added the RSA host key for IP address ..... to the list of known hosts.

And error:

+ git checkout master
error: pathspec 'master' did not match any file(s) known to git.

--Solution

  Dev:
       - step:
           script:
            - ant deployCodeCheckOnly -Dsf.username=$SF_USERNAME Dsf.password=$SF_PASSWORD
             - git fetch
             - git checkout -b master
             - git merge Dev
             - git push -v --tags origin master:master
Adamo
  • 586
  • 1
  • 9
  • 28
  • 2
    The reason that `git checkout master` failed with the `pathspec` error, but `git checkout -b master` succeeded, is because Bitbucket Pipelines only pulls the branch being built down to the build agent running the pipeline. There is no `master` branch in its repo clone, so you have to create it. And then indicate that your push should go to the upstream `origin master`. – mcw Nov 09 '19 at 22:17

2 Answers2

13

I was facing the same issue, but wanted to use pull requests instead of simple git merge. So I ended up utilising bitbucket API for the job:

  1. Create "App password" -- Create "App password" so you don't have to push your own credentials to pipelines (bitbucket settings -> app passwords)

bitbucket app password

  1. Set environment variables for pipelines --

    • BB_USER = your username
    • BB_PASSWORD = app password
  2. Create bash script -- I have a bash script that creates pull request from $BITBUCKET_BRANCH and merge it immediately

#!/usr/bin/env bash

# Exit immediately if a any command exits with a non-zero status
# e.g. pull-request merge fails because of conflict
set -e

# Set destination branch
DEST_BRANCH=$1

# Create new pull request and get its ID
echo "Creating PR: $BITBUCKET_BRANCH -> $DEST_BRANCH"
PR_ID=`curl -X POST https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_OWNER/$BITBUCKET_REPO_SLUG/pullrequests \
  --fail --show-error --silent \
  --user $BB_USER:$BB_PASSWORD \
  -H 'content-type: application/json' \
  -d '{
    "title": "Automerger: '$BITBUCKET_BRANCH' -> '$DEST_BRANCH'",
    "description": "Automatic PR from pipelines",
    "state": "OPEN",
    "destination": {
      "branch": {
              "name": "'$DEST_BRANCH'"
          }
    },
    "source": {
      "branch": {
              "name": "'$BITBUCKET_BRANCH'"
          }
    }
  }' \
  | sed -E "s/.*\"id\": ([0-9]+).*/\1/g"`

# Merge PR
echo "Merging PR: $PR_ID"
curl -X POST https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_OWNER/$BITBUCKET_REPO_SLUG/pullrequests/$PR_ID/merge \
  --fail --show-error --silent \
  --user $BB_USER:$BB_PASSWORD \
  -H 'content-type: application/json' \
  -d '{
    "close_source_branch": false,
    "merge_strategy": "merge_commit"
  }'
  1. Finally in pipelines -- just call the script:
Dev:
       - step:
           script:
            - chmod +x ./merge.sh
            - ./merge.sh master

Benefits:

  • Pipeline will fail if there is conflict (if you want it to fail)
  • better control of what's happening
GetoX
  • 4,225
  • 2
  • 33
  • 30
svestka
  • 755
  • 1
  • 8
  • 13
5

In the “script” section of the YAML configuration, you can do more or less anything you can do at the shell, so (although I’ve never tried it) don’t see a reason why this shouldn’t be possible.

In other words, you’d have to:

  • Switch the branch to master
  • Merge dev (optionally, using the predefined BITBUCKET_COMMIT environment variable, which identifies your dev commit)
  • Commit to master (and probably also push)

As git is available in script, you can use normal git commands and do not need anything specific to Bb Pipelines, like so:

 script:
    - git fetch
    - git checkout -b master
    - git merge Dev
    - git push -v --tags origin master:master

To make sure this is only done when your Ant job is successful, you should make sure that in case of an error you’ll get a non-zero exit status (which, I assume, is Ant’s default behaviour).

mcw
  • 3,500
  • 1
  • 31
  • 33
BlueM
  • 3,658
  • 21
  • 34
  • 2
    I simplified things a bit. Your branch `master` is not automatically present, but you have to `git fetch` first (the remote is defined, *but*: you have to insert a deployment key, as otherwise you are not allowed to fetch from the remote) before you can `git checkout master`. – BlueM May 16 '17 at 11:10
  • I try many things. Please look again of my edit, and advice me how the pipline should look. Thanks. – Adamo May 16 '17 at 16:36
  • One more thing: 'git branch' retrieves: Dev and remotes/origin/Dev – Adamo May 16 '17 at 17:34
  • Ok. I found the solution. Please look on it, and confirm if it's a good way to do that. – Adamo May 16 '17 at 17:49
  • I can’t tell if it’s a good solution, as I never tried to do that myself. But if it works for you, it can’t be too bad ;-) [And it looks pretty straightforward, so I don’t see anything to worry about.] – BlueM May 17 '17 at 05:09