Create Github Actions and be smart

Automate your tedious job using Github Actions.

Akash Panchal
Analytics Vidhya

--

Source: https://lubus.in/blog/github-actions-to-release-wordpress-plugin-3656

What is Github Actions?

GitHub Actions enables you to create custom software development life cycle (SDLC) workflows directly in your GitHub repository.

Github Actions are custom code/instructions that interact and perform some operations in your Github repo any possible way you would like. (really!!!)

CI/CD are the legacy examples of it. If you know CI/CD then know that Github Actions is ++ to it.

More about it?

  • Github Actions is fully integrated into Github hence doesn’t require any external site and can be managed in your repo only.
  • Github Actions provides multiple templates for all kind of CI configurations and one can create own custom action and publish on the Marketplace.
  • Github Actions is completely free for every open-source repository and include 2000 free build minutes per month for all your private repositories which is comparable with most CI/CD free plans.

Github Actions

Let’s understand the basic terminologies about Actions before creating one.

Actions:

Small portable code that can be used to create a Job. You can use Action from the Marketplace Or create one on your own.

Job:

A job is made up of multiple steps and runs in the virtual environment instance. Jobs can run independently or sequentially.

Steps:

A step is a set of tasks that can be executed by a job. Steps can run commands or actions.

Workflow:

A Workflow is an automated process that is made up of one or multiple jobs and can be triggered by an event. Workflows are defined using a YAML file in the .github/workflows directory.

Create a Custom Action

Problem

Automatically create fetch content and append to README.md file

I’ve created a repo where I want to publish python tricks. The main content is the python tricks and piece of content resides in a separate file in /data folder.

Content of each file is in JSON format, which looks like the following:

I want to read each file and copy the content to README.md file which should look like the following:

Possible Solutions:

  1. Copy and paster manually and push README.md — Nah…
  2. Write a python script that reads every file and copy to the README.md and push.

What If Github does everything for you???

Yes, let’s create a Github Action that does that.

Step1: Create a script for your Job

Let’s create a python file which will read all the JSON files from the /data folder and append the content to the README.md file

python-tricks/action-generate-readme/main.py

I’m keeping this file in a separate folder which will be the action directory. This file will be executed in the choice of your VM.

Step2: Create a Docker file

Github allows you to build Docker and Javascript actions. Here, we will create it using Docker.

Let’s create a Docker file and add the following content:

https://gist.github.com/akashp1712/17714c60bb07a650b20025922d27fb55

  • We’ll use the python:3.8.1-alpine3.10 image in our docker
  • And set the entry point as main.py file
ADD main.py /main.py                       
ENTRYPOINT ["python", "/main.py"]

Step3: Put the Action at work(flow!)

Now, this is an important part of the Github Actions: configure the workflow.

Create a workflow file main.yml in the folder .github/workflows/

Let me explain the workflow file:

Trigger

on:
push:
paths:
- 'data/**.json'
-'!data/0_metadata.json'
  • The workflow says it should trigger the job when push happens for update/new JSON files in the /data folder.
  • Read more about Triggers here. The sky is the limit.

Jobs

jobs:                         
generate-metadata:
runs-on: ubuntu-latest
  • The job named, “generate-metadata” will run on ubuntu VM
  • You can have multiple jobs running in your workflow.
steps:                             
- uses: actions/checkout@v1
- name: Add list of files to a metadata file and generate Readme
uses: ./action-generate-readme
- name: Commit File changes
# skipping the commit code
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

There are 4 steps in this job:

  1. actions/checkout@v1 — is a Github action from the marketplace which checkouts the code in the VM.
  2. ./action-generate-readme — is our local action which generates Readme file
  3. Commit file changes — this is vanilla git commands
  4. ad-m/github-push-action@master — is Github action from the marketplace which pushes the changes to the remote.

And That’s it. Really.

Our first Action is ready, It’s basic and doing a simple operation on our repo. Let’s see it in action. All I need to do is to push a JSON file.

It triggers the workflow which you can see from the Actions tab

Clicking on the running workflow would display the detailed execution.

You can see that all the steps of jobs are being executed sequentially, it will show the green tick once it completes.

And Voilla, it pushes the commit automatically. (You can configure to generate a Pull request too)

That’s it, guys.

We’ve created a simple Gighub Action in the repo which automates my workload for me.

What else one can use Github Actions for?

  • Automatically publish WordPress plugin
  • Lint check your code

--

--