How to run Python scripts in your GitHub actions

Jcdan
2 min readJun 5, 2021

--

Intro

Python is one of the most used general-purpose programming languages. It is particularly well suited for automation and DevOps activities, as you can do a lot with few lines of code.

In this post, we will go step by step about how we can call your Python script from GitHub Action.

This can be very helpful for your automation and DevOps processes.

Requirements:

  • GitHub account
  • VSCode or any other editor
  • Basic understanding of GitHub actions

Here we are going to interact with 2 repos:

It’s always good practice to keep scrips in a different repo since it allows you to use it anywhere just by cloning the repo.

For this exercise, we will use the file on_push.yml located inside the .github/workflows folder of the medium repo.

We are going to import the python scripts from the devops repo inside the medium repo.

Workflow file content

Let’s take a look line by line:

1: The name of our workflow.

3: Event that triggers this workflow, meaning that for every push we do, this workflow will run.

5–6: In our environment, we are going to use the DEVOPS_DIR variable. The value is set to devops. This is where we the repository jcdan3/devops will be cloned.

8–12: You can name your workflow whatever you like. Also decide to run on ubuntu-latest, which is directly on GitHub’s servers.

13–18: Simply put, we are cloning the repository jcdan3/devops and putting it in the DEVOPS_DIR dir, as defined on line 6.

20–22: Using the shell (use cmd if you run the workflow on windows), we run the python script hello_word.py which outputs to the console a simple string.

Devops repository

From the Devops repository, we have the python script hello_world.py:

Now simply push any modification to a repo with the content of the file on_push.yml and it should run as follow:

The code is available in my GitHub repos: https://github.com/jcdan3/Medium and https://github.com/jcdan3/Devops

Feel free to fork or clone the repo.

That’s it for now. Make sure to take a look at my other post with 4 ways to speed up your Github workflows!

--

--

Jcdan
Jcdan

Written by Jcdan

Experience software developer in a quest to democratize software development. More here: https://www.linkedin.com/in/jcdansereau

No responses yet