Refactoring your Bitbucket Pipelines with custom Pipes

Александр Жуков
2 min readSep 11, 2019

--

Bitbucket has an awesome feature called Bitbucket Pipes, which is basically a collection of docker images that allow developers to do various things inside Pipelines. For example, developers can deploy to AWS, GoogleCloudPlatform or Azure, send notifications to Slack, create PagerDuty alert etc.

However, these pipes don’t cover all use cases. So developers often still need to use plain pipelines with a list of bash commands to achieve some specific results. But fear not. Bitbucket Pipelines allow you to write a custom pipe that will exactly fit your need. This simple guide will demonstrate how to write a simple custom pipe using Python.

As an example, I’ll show how to create a pipe that will invalidate a CloudFront distribution.

To write a simple custom pipe you’ll need to:

  • Write a python script
  • Define the requirements for your script
  • Write a Dockerfile to package your code
  • Build and push a docker image to a public repository

Step 1: Create your main python script

We’re gonna use the bitbucket-pipes-toolkit python library for parameters validation, colorized logging and other cool things. You may want to check the official documentation for more details about this library.

Step 2: Create a Dockerfile

In the Dockerfile you install all dependencies, in our case we install boto3 and bitbucket-pipes-toolkit:

We also copy the python script into the root directory inside a container. The final Dockerfile looks like this:

Step 3. Build and publish your pipe

Now you need to publish your pipe. First, you’ll need to build a docker image:

docker build -t example-repository/example-image .

Then you’ll need to push it to the docker registry:

docker push example-repository/example-image

Aaand it’s done

After the last step you should be able to user your pipe. Note, that to run a pipe from a docker image, you’ll need to use a special reference:

docker://example-repository/example-image:latest

So your bitbucket-pipelines.yml should look like this:

Now you’re ready to go have fun with custom pipes.

Useful links:

--

--