Using Bitbucket in the AWS CodePipeline

Mounick
Technology Stack
Published in
3 min readNov 19, 2019
AWS Architecure

CodePipeline is a very popular tool aiming to provide continuous integration and delivery for your project. CodePipeline builds, tests, and deploys your code every time there is a code change, based on the release process models you define. This enables you to rapidly and reliably deliver features and updates. Generally, CodePipeline has three stages Source, Build and Deploy where Source stage is compulsory and a pipeline should contain minimum two stages.

But the problem is CodePipeline does not support Bitbucket as it source. Currently it supports only Github, S3 and CodeCommit. In order to integrate the bitbucket in CodePipeline the AWS recommended method is bit of complex with creating service API gateway, Lambda, SNS topic etc. In this blog we are going to see a simple workaround in order to use bitbucket in the AWS CodePipeline.

The trick is CodePipeline does not support Bitbucket but the CodeBuild supports. We are going to use CodeBuild and CodePipeline independently in order to achieve this.

  1. Open CodeBuild dashboard in your AWS console.
  2. Give a project name. In source section click on connect to Bitbucket using OAuth and connect your bitbucket account with the AWS and choose Bitbucket in the source provider drop down menu. Select the type of repository where you have your code and remaining things are straight forward.

3. Click on WebHook check box if you want to automatically trigger the code build every time a new code change is pushed to your repository.

4. In Environment section select Managed Image and choose ubuntu in the operating system drop down menu and select standard environment in the Runtime drop down menu and choose aws/codebuild/standard:1.0 as the image and keep the remaining things as default.

5. In Buildspec section select insert build commands and click on switch to editor and paste the following commands in the text box.

version: 0.2phases:
post_build:
commands:
- if [ $CODEBUILD_BUILD_SUCCEEDING -eq 0 ]; then rm -rf $CODEBUILD_SRC_DIR/*;rm -rf $CODEBUILD_SRC_DIR/.[a-zA-Z_]*; else echo "Build Succeeding"; fi
artifacts:
files:
- '**/*'
discard-paths: no

6. In Artifacts section choose s3 bucket in the type drop down menu. Type . your bucket name and give a name for your artifacts. Select Zip radio button in the artifacts packaging.

7. Keep the remaining things as default and Click on Create build project.

So whenever new code change is pushed to your repository the build is triggered which creates a zip file of your code and push it to the S3 bucket.

In CodePipeline we should use that s3 bucket as our source. So whenever there is new artifacts pushed to the bucket, CodePipeline is triggered. So, this way we can achieve integrating the bitbucket support in the CodePipeline.

Make sure your CodeBuild IAM role has permission to your S3 bucket and its objects. And also that Bucket should be versioned.

That’s it now you have integrated Bitbucket to your CodePipeline. Feel free to comment if you have any doubts and issues.

--

--

Mounick
Technology Stack

Passionate about cloud solution, automation, and driving innovation in tech. Sharing insights on tech evolution.