Using Makefiles to Automate Workflows
Tags: makefiles, python, automation
Published: 2020-08-07 19:34:44

---
title: Using Makefiles to Automate Workflows
published: true
description: A brief intro to the why and when of Makefiles
tags: makefiles, python, automation
cover_image: https://i.imgur.com/uS6cUHJ.png
---

As a junior developer, I was unsure why anyone would want to use a Makefile when they could use bash or write a script in the language of their choice. I had no idea what a Makefile is and why you'd want to use one so I did some reading.

## A really really quick intro to Makefiles
Makefiles are used to automate workflows and are composed of chunks of instructions (much like functions) that build on one another to accomplish multi-step processes. Many teams use Makefiles to automate processes both within single projects and across projects/teams. 

## 1. Why use a Makefile instead of a script (bash, python, etc)? 
Makefiles have the advantage (like bash) of being language agnostic. This is good for teams with a diversity of experience. For example, a front-end junior developer and a senior dev ops engineer. 

Unlike bash or a regular programming language, the make application that runs Makefiles is smart about the work it does. This is because it builds dependency trees under the hood. It knows if you are using a config file to build other files. If you haven't updated the config, then it won't bother building the other files that depend on it. 

While this dependency tree functionality might not seem revolutionary at first glance, it can be used to create some very powerful workflows. Check the article references at the end for some examples.

Lastly, Makefiles are extremely modular and flexible. You can use small blocks of logic and combine them to create very powerful chains that are linked together. And then you can easily re-use those blocks in other Makefiles just like you would in any other programming language. 


## 2. What are some examples of things I can do with a Makefile?

- clean-up unwanted files
- combine the multiples steps documented in the README to get an app up and running into one simple command
- as an alias for really long commands with lots of flags (which you can change when you call the make command!)
- project templates to get you and your team writing code faster without worrying about common dependencies, tooling, and file structure
- any of the above in combination, and more!

## 3. How do I know when a Makefile is the right tool?

First, there are some cases where it might NOT make sense to use Makefiles: 

- your team doesn't use Makefiles and the time it would take to get them familiar/comfortable with it isn't justified

- what you need to accomplish is not complicated and can be easily written in a common script like bash, python, etc.

Here are some cases where it does make sense:

- your project involves more than one language or tool. For example, helm/k8s + docker + python + git hooks and you need a simple interface to complete tasks across those tools.

- you want short, easy to remember commands to take the place of long cli commands (without giving up the ability to inject options!)

- your team is working across many languages on its projects and you want a common language for automation. For example, installing and running any project locally on your machine whether it's written in python, javascript, or go. 

## 4. What do I need to know to get started with Makefiles?

While I am still very much a beginner at Makefiles, I found these pages to be helpful:

- [makefiletutorial](https://makefiletutorial.com/)
- [How to use Makefiles to Automate Repetitive Tasks...](https://www.digitalocean.com/community/tutorials/how-to-use-makefiles-to-automate-repetitive-tasks-on-an-ubuntu-vps) by Justin Ellingwood
- [What is a Makefile and How Does it Work?](https://opensource.com/article/18/8/what-how-makefile) by  Sachin Patil

## Further resources

Here are the articles I used to write this article, each of which have some great examples of Makefiles and why you'd want to use them. 

- [Why you need a Makefile on your Project](https://blog.theodo.com/2018/05/why-you-need-a-makefile-on-your-project/) by Martin Guillier
- [Why Use Make] (https://bost.ocks.org/mike/make/) by Mike Bostock
- [Makefiles in python projects](https://krzysztofzuraw.com/blog/2016/makefiles-in-python-projects.html) by Krzysztof Zuraw
- [Simple Makefile to automate Python projects] (https://medium.com/@habibdhif/simple-makefile-to-automate-python-projects-e233af7681ad)
- [How to use Makefiles to Automate Repetitive Tasks...](https://www.digitalocean.com/community/tutorials/how-to-use-makefiles-to-automate-repetitive-tasks-on-an-ubuntu-vps) by Justin Ellingwood
- [Automation Using Makefiles](https://matthewkudija.com/blog/2018/03/15/makefile-automation/) by Matthew Kujija