Automatically Build and Deploy Your Python Application in Four Easy Steps
Tags: Python, Continuous Integration, Continuous Delivery, Software Development, Programming

3. Create a New Pipeline and Add Actions

We’re ready to build a new pipeline. Click the “Add a new pipeline” button and fill out the form as follows:

Creating a new pipeline.

We will run this pipeline every time something is pushed to the main branch. Alternatively, you can build your application recurrently or manually as well.

Click the big blue button to add the new pipeline. Buddy has already taken a look at our code and will come up with a few suggestions:

Buddy suggesting several actions.

Choose the Python action and fill out the next form. If you want to follow along, you can copy/paste this:

pip install pipenv

pipenv install

pipenv run python3 tests.py

These actions install pipenv, install all the requirements, and finally run the unit tests within our newly created environment.

Create a Python action.

You can choose the Python version here as well. Buddy runs each step in a Docker container, so you can even use your own custom Docker container if needed. For us, the default Python containers are fine. I picked version 3.8.6 because that’s what I know will work with this project.

You can try the build step right now if you want — just to make sure it all works before you continue. As you can see below, I needed four tries because I forgot to check in some files to the GitHub repo. That’s no problem. We can keep trying until it works:

Our build step works!

Now go back to your pipeline and click the little + below your first action. It allows you to add more actions. By default, the next action only runs if the previous one is finished without errors.

The next action to add is another one of the suggestions: the Dockerfile linter. It checks our Dockerfile for mistakes. That’s a nice-to-have extra. All the defaults are fine, so all we need to do is click “Add this action.”

Now it’s time to build the Docker image and push it to Docker Hub. Add another action and again pick from the suggested actions. This time, it’s the one called “Build Image” (the one with the Docker icon inside it).

The defaults on the “Setup” tab are fine. Head over to the “Options” tab and fill in your Docker Hub details. This is also the place where you can define your image tag.

We’ll do something straightforward: We always tag the image as latest . To keep the version history as well, we add an additional tag that uses a Buddy variable.

A what? Buddy automatically defines a list of environment variables you can use in your project. A full list and more details can be found in the documentation. One of them is the shortened Git revision number that is accessible under ${BUDDY_EXECUTION_REVISION_SHORT} . We can use it to give our image a unique ID:

Set up the Docker image build.

The last action you want to add is a notification so you’ll get notified of successful builds. I’ll demonstrate with a good old email, but there are many other options:

Slack

Telegram

MS Teams

Discord

SMS

The process is simple again. Click the little plus button under the last action, scroll through the available actions, and click email. You’ll get a screen like this:

Adding email notifications.

As you can see, you can use all the available environment variables to customize the message to your liking.

In a more advanced setup, you probably want to tag your Git repo with a version number and create a build trigger that triggers on new tags. This is a nice way of separating dev builds from release builds since this tag can be used as a variable to tag the Docker image as well.