Running Black Formatter to Lint a Python file in Gitlab CI/CD

Stephen Olabode
3 min readOct 22, 2019

Hey guys, I will be giving a simple explanation on using Black to format your python file using the gitlab CI/CD tool. In this tutorial, the tools basically used here are all in your gitlab account, they are the runners to pick up your jobs and the ci/cd to build the project. I’ll proceed with the assumption that you already have a Runner configured and registered on your account, if you need help registering a runner, this gitlab docs will be useful in doing just that https://docs.gitlab.com/runner/register/ or you could check my other post on steps to install and register a runner here https://medium.com/@stevedot.olabode/creating-a-custom-runner-on-gitlab-800fec724d6e.

Moving on, you’ll need to create a new project and fell free to give it whatever name you like, I named mine ‘Python Black’. Once you have a new project created, you will then need to create a new .py file, this is the file that will be formatted by black. Perhaps, you want to use a random python file, you can easily get from https://www.pythonforbeginners.com/code-snippets-source-code/python-code-examples. Once you insert in a code, go ahead and save this new file in the .py extension format.

Next, you’ll need to create the .gitlab-ci.yml file. This .yml file is the “activator” that the gitlab CI/CD will run with(through the help of runners, either the shared runner or your customized runner) to help achieve your end goal which I believe is the smooth transition between building and production. It is automatically triggered with each commit you make on it provided the configuration(i.e. the code in it) is valid. The .yml file basically contains the steps you need the gitlab CI/CD tool to take to get to the intended stage you want it to and in this case, we are getting to the testing stage. In your .yml file, you’ll put in the following command image:

and then you save the file. When this is done, your repo should look more like this.

repository

If this is properly done, you should have a blue notification indicating a running project, if not, you could just click on CI/CD on the left pane then click on Jobs. You’ll see your latest job actively running. On a successful test run, you should have this just like i do.

So you’ve just built on your gitlab, black linting against a python file, using its CI/CD tool. Congratulations on the successful build, cheers.

--

--

Stephen Olabode

DevOps Engineer | AWS Enthusiast | Cloud Computing | Solutions Architect