Automation with GitHub Actions

aps08
7 min readJan 29, 2023

--

Automation plus GitHub Actions

If you are trying to automate stuff and searching for a platform and tool to run it, without any cost associated with it, then this article is for you. In this article, I will be going through (1) Automation and (2) GitHub actions in details and show you, how we can run (3) automation projects on GitHub using actions.

keywords — python, automation, GitHub and Actions

Automation

Automation is an application of technologies to produce desired results with minimum human interaction. It increases the efficiency, reliability and speed of many tasks. Automation is used on a repetitive task just like assembly line in manufacturing plants. Automation can we divided into three categories basic automation, process automation and integration automation. Let’s talk about integration automation as it matches our interest, you can read more about automation here.

Automation where machine can mimic human task with pre-defined rules and policies and can produce similar result repetitively or un-repetitively with minimum human interaction can be put under integration automation category.

GitHub Actions

As per GitHub doc, GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository or deploy merged pull requests to production. Now, here we won’t be diving into devops and other things, you can read more about GitHub Actions here, but I will teach you how you can deploy a python project on GitHub using GitHub Actions.

Before we dive into the python project, I would like you to bring your attention on GitHub Actions billing. If you are a free GitHub user, then can you run GitHub actions for 2000 minutes a month. For a pro user, you get 3000 minutes a month. If you exceed this limit, you will be charged as per GitHub actions billing according to the operating system per minute.

Automation Projects on GitHub Using Actions

I hope you understood what automation we are talking about, and what GitHub Actions are, now let’s dive into the project where we put things together and our code comes to life.

For this section, I will be referring to one of my projects named pywisher. It’s a mini python script, which integrates with your Gmail account and sends birthday wish using a HTML template, it is schedule to run every day at midnight.

https://github.com/aps08/pywisher

Now, let say you have your code ready, and you want to run it using GitHub Actions. One thing to note here, before even pushing your code to repository make sure you have removed all sensitive information likes token, key and password etc., and stored them in repository secrets. To run your code on GitHub, first of all you need an actions file. Since, I am referring to my project, I will be using the same actions file to explain the steps.

In a repository, the default location for all the actions files is in .github/workflows folder, that way GitHub could understand that these should run using actions. If you look into the actions.yml file of my project, you will find its content as given in the below image.

Actions file

Let’s go line by line, on the very first line the name is used to give name for your action, for now I have given it the repository name. You can even add a statement instead of a name.

The on key is used to defined when the job should run. It can have push, pull_request etc., but here we have to run a job on schedule, so we will be using schedule. After the schedule we have cron, which is command-line utility for job scheduler in a Unix-like system. It takes time in a UTC format, so here “30 18 * * *” means to run every day at 00:01 AM IST.

name, on and schedule

Till now, we have given name and when the jobs should run, now in the jobs key we configure what our jobs will use and step by step approach to run the job. Inside job, we first configure the name of the job, here we have named it as build. The runs-on keywords is used to configure the type of operating system on which you want to run the code, here we have given ubuntu-latest. The timeout-minutes is used to prevent waste of minutes quota, if the job run in a hung or repetitive state.

job, runs-on and timeout-minutes

Now let’s define all steps, every time you give name it starts a new step. For this particular repository I need to run the job in IST, so in the very first step I am setting the time zone to IST. The name keyword is the name of the step and in the run keyword is used to run command inside the operating system you have defined in runs-on keyword. The pipe operator (|) symbolizes that each line is a different command. So, on the very first step named set timezone, we are running three commands to change the time zone to IST. In next step named checkout repository, the uses keyword specifies that this step will run v3 of the actions/checkout action. This is an action that checks out your repository onto the runner, allowing you to run scripts or other actions against your code . The fetch-depth indicates number of commits to fetch. 0 indicates all history for all branches and tags. The ref tells you the branch of the repo, and token is by default github.token, but here I am pushing to a protected branch so I am using my PAT. You can remove the with part of checkout repository step if you are not pushing to protected branch.

In the next step named python setup, the uses keyword specifies that this step will run v4 of the actions/setup-python action. The with keyword specifies the version of python.

steps, name, uses and with

Now we have setup the time zone, repository and python, its time to run commands to run the python code. In the next step named install python packages we are running two command, first for upgrading pip, and second for installing the required package using the requirements.txt file in the repository. Now we should run our python code, in the next step named execute main.py the env is used to pass the environmental variable, here we are are passing three values which we have stored in the repository secrets. In the run command, we are passing command to run the main.py file inside the src directory. If everything is configured properly, your code will run successfully.

env and run

If you have a file which might change during the run, then you might need to commit back to the repository, in my case I have a file named pywisher.log. In order to commit back, you need to give permission to github action for read and write on the repository. Go to repository settings, then click Actions on the right side, then select general under Actions and under workflow permissions select Read and Write Permissions.

Read and write permission Workflow permissions

In the very last step named commit changes we are running commands to configure git and push the changes to the repository.

Git config to push back

--

--