Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

bitbucket pipeline design

chare
Contributor
August 6, 2020

I am working to get a pipeline built.  I have the following requirements from the rest of the team, and while I think I am getting close there are just few things that don't make sense.  I am hoping someone who knows more about bitbucket pipelines will weigh in here.

 

The Requirements:

A commit and push to a branch other than master or release executes the code in brach/default section
A commit and push to directly to master is denied
- unless the merge is done from a PR
A commit and push to directly to release is denied
- unless the merge is done from a PR
A PR on master branch is created to merge a branch, which executes the code in the pull requests/master section
when the PR is approved, and the merge button is pushed, merge the source branch into master and execute deployment code
A PR on the release branch is created to merge in master, which executes the code in the pull requests/release section
when the PR is approved, and the merge button is pushed, merge the master branch into release and execute deployment code

 

Does that make sense?

 

This is what I have in my bitbucket-pipeline. I think I am getting close, but I am not quite there yet.  This is a framework -- I still have to build out all the deployment code, but I am trying to get to a standard structure that will be used in all of the repositories.

 

Thanks!

 

# Only use spaces to indent your .yml configuration.
# -----
# Branching model
# This configuration assumes the following branch model:
# main/development branch is 'master'
# production branch is 'release'
# Predefined branches for 'bugfix/', 'feature/' and 'hotfix/'
# -----
# Skipping a pipeline:
# If you don't want to run a pipeline on a commit that would normally trigger one,
# you can include [skip ci] or [ci skip] anywhere in your commit message of the HEAD
# commit. Any commits that include [skip ci] or [ci skip] in the message are ignored by
# Pipelines.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: atlassian/default-image:2

# This sets up using blocks of common code using YAML anchors
# See https://support.atlassian.com/bitbucket-cloud/docs/yaml-anchors/

check: &check
- step:
name: Check Build Environment
script:
- |
echo "build $BITBUCKET_BUILD_NUMBER from Commit $BITBUCKET_COMMIT"
echo "Source Branch : $BITBUCKET_BRANCH "
if [ -n "$BITBUCKET_PR_ID" ]
then
echo "Checking a pull request : $BITBUCKET_PR_ID"
echo "Destination Branch: $BITBUCKET_PR_DESTINATION_BRANCH"
fi
# artifact:
# See https://support.atlassian.com/bitbucket-cloud/docs/use-artifacts-in-steps/

pipelines:
pull-requests:
master: # run when creating a pull request on master
- <<: *check
- step:
name: master
script:
# insert repository specific code here
- echo "this is executed by the PR to master step"
#- pipe: #Atlassian pre-configured pipes
after-script:
- echo "exit code $BITBUCKET_EXIT_CODE"
- step:
name: Deploy to us-east-1-dev
deployment: us-east-1-dev
script:
- echo "this is a sample deployment which must be pre-configued in the repository"
release: # run on push to release
- <<: *check
- step:
name: release
script:
- echo "this is executed by the PR to release step"
- echo "build $BITBUCKET_BUILD_NUMBER from Commit $BITBUCKET_COMMIT"
- echo "release PR $BITBUCKET_PR_ID"
- |
# Verify the source branch is master
if [ $BITBUCKET_BRANCH != "master" ]
then
echo "Merging to into release is only allowed from the master branch"
exit 1
fi
- echo "BRANCH $BITBUCKET_BRANCH DESTINATION $BITBUCKET_PR_DESTINATION_BRANCH"
#- pipe: #Atlassian pre-configured pipes
after-script:
- echo "exit code $BITBUCKET_EXIT_CODE"
# artifact:
# See https://support.atlassian.com/bitbucket-cloud/docs/use-artifacts-in-steps/
"**": # run when creating a pull request on any repo
- <<: *check
- step:
name: default PR
script:
# insert repository-specific code here
- echo "this is executed by the PR to any other repo step"
#- pipe: #Atlassian pre-configured pipes
after-script:
- echo "exit code $BITBUCKET_EXIT_CODE"
# artifact:
# See https://support.atlassian.com/bitbucket-cloud/docs/use-artifacts-in-steps/
branches:
master: # runs on the master branch
- <<: *check
- step:
name: Push to Master
script:
- echo "pushing directly to master is not allowed. Use a pull request."
# Putting an exit 1 here causes the pipeline to fail.
- exit 1
release: # runs on the master branch
- <<: *check
- step:
name: Step 1
script:
- echo "pushing directly to release is not allowed. Use a pull request."
- exit 1
after-script:
- echo "exit code $BITBUCKET_EXIT_CODE"
default: # runs on any branch not specified
- <<: *check
- step:
script:
- echo "Everything is awesome!"

1 answer

1 accepted

0 votes
Answer accepted
chare
Contributor
August 6, 2020

Indentation is correct in my file, it just didn't come out correctly here.  The pipeline works, but I am struggling with getting all of the requirements in place, and I am not sure exactly what I should be doing differently.

ktomk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 10, 2020 edited

@chare On first glance, some of the requirements you list upfront aren't covered by the Atlassian Bitbucket Pipelines Add-On, AFAIK. Esp. those for who can push to which branch is part of the branch management in Atlassian Bitbucket directly (and some of what you ask for might require a (higher) paid account [maybe!]).

For the indentation paste the yml as code-block, the formatting is then preserved and it's also much easier to read.

Apart from these little comments, it sounds rather straight forward to me and I don't see any actual blockers upfront. So is there specifically something that is not working for your scenario?

chare
Contributor
August 11, 2020

I have continued to refine it and I have it working.  the branch management permissions have to be set in the repository.  This has been through so many iterations since I originally posted it, I should have gone back and marked it closed :-)

There are a couple of things which will need a paid account - forcing merge checks for one, but I am not super worried about that yet.  Thanks for the comment.

Like • Som Mohanty likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events