GitHub Part-2: Automate Your Workflow with GitHub Actions: Harnessing the Power of Push Events

Laksh
4 min readJun 3, 2024

--

In the realm of software development, automation is the key to efficiency and productivity. GitHub Actions provides a robust platform for automating various tasks within your development workflow. One of the most commonly used triggers in GitHub Actions is the push event, which occurs whenever code is pushed to a repository. In this blog, we'll explore how you can leverage the push event in GitHub Actions to automate your development process.

What is a Push Event?

A push event in GitHub occurs when a user pushes commits to a repository. This event is triggered regardless of whether the push is made through the GitHub web interface, a Git client, or an API call. By harnessing the power of push events, you can automate various actions based on changes made to your codebase.

  1. Harnessing GitHub Actions for Push Events

GitHub Actions allows you to define custom workflows using YAML syntax, which are triggered by various events, including push events. Let's take a look at a simple example of a GitHub Actions workflow that is triggered by a push event:

Key Insight:

Utilizing the push event originating from a non-’main’ branch, I seamlessly initiated the generation of a Terraform plan. This method facilitated independent development and testing for each developer, resulting in considerable time savings, given the automatic triggering of the push event upon each branch push. Below are some of the parameters accepted by the push event for configuration.

Example

on:
push:
branches: # when someone pushes to main or feature branch
- 'main'
- 'feature/**'
branches-ignore:
- 'release/**'
paths:
- '**.js'
- '**.py'
- '**.tf'
paths-ignore:
- '**.txt'
tags:
- 'R1.**'
tags-ignore:
- 'F1.**'

In this workflow:

  • The on keyword defines the trigger for the workflow.
  • branches: when a push to a specific branch occurs.
  • branches-ignore:- Ignore push to release branch

#Note :- you can only use one of them at a time. you can either use branches or branches-ignore

  • path:- push that includes a change to a JavaScript (.js) or python (.py) file is made
  • paths-ignore:- ignore changes to text files

#Note :- you can only use one of them at a time. you can either use paths or paths-ignore

#Note :- you can only use one of them at a time. you can either use path or paths-ignore

  • tags:- workflow will run when someone pushes a tag that starts with R1
  • tags-ignore:- ignore pushes to a tag that start with F1

#Note :- you can only use one of them at a time. you can either use tags or tags-ignore

Before going into push workflow code.

lets study GitHub object. GitHub Object contains an event as an embedded object. You can print the object and see members of the object using the below code.

on:
push:
branches: # when someone pushes to the feature branch
- 'feature/**'
paths:
- '**.js'
- '**.py'
- '**.tf'
- '**.yml'
tags:
- 'R1.**'
jobs:
githubObject:
name: 'github object'
runs-on: ubuntu-latest
environment: 'dev'
steps:
- name: Check object
run: |
cat << OBJECT
${{ toJson(github) }}
OBJECT

add the above code to github-push-workflow.yml under the .github/workflows folder. This workflow will be triggered when you push any commit to your repo. Here the workflow will only trigger when push the commit to feature branch as mentioned under branches param of push event

A small snapshot of the push event as contained inside GitHub object is as shown below.

"event_name": "push",
"event": {
"commits": [
{
"author": {
"email": "",
"name": "",
"username": ""
},
"committer": {
"email": "",
"name": "GitHub",
"username": "web-flow"
},
"distinct": true,
"id": "",
"message": "Update github-push-workflow.yml",
"timestamp": "2024-05-01T11:42:55+05:30",
"tree_id": "",
"url": ""
}
],
"compare": "",
"created": false,
"deleted": false,
"forced": false,
"head_commit": {
"author": {
"email": "",
"name": "",
"username": ""
},
"committer": {
"email": "",
"name": "GitHub",
"username": "web-flow"
},
"distinct": true,
"id": "",
"message": "Update github-push-workflow.yml",
"timestamp": "2024-05-01T11:42:55+05:30",
"tree_id": "",
"url": ""
},
"pusher": {
"email": "",
"name": ""
},
"ref": "refs/heads/feature/dev",
"repository": #{ owner, description git urls master_branch}
"sender": #{ user whose commits triggered this event }

}

other few important members of GitHub Object are

  "server_url": "https://github.com",
"api_url": "https://api.github.com",
"graphql_url": "https://api.github.com/graphql",
"ref_name": "feature/dev",
"ref_protected": false,
"ref_type": "branch",
"secret_source": "Actions",
"workflow_ref": "{username} /learn-github/.github/workflows/github-push-workflow.yml@refs/heads/feature/dev",
"workflow_sha": " ",
"workspace": "/home/runner/work/learn-github/learn-github",
"action": "__run",
"event_path": "/home/runner/work/_temp/_github_workflow/event.json",
"action_repository": "",
"action_ref": "",

you can use ref and ref_name to run a job if the workflow has been initiated from the ‘feature/dev’ branch. workspace is your current working directory.

- name: Terraform Apply
if: github.ref == "refs/heads/feature/dev"
run: terraform apply -auto-approve -input=false

You can also use ref_name .

- name: Terraform Apply
if: github.ref_name== "feature/dev"
run: terraform apply -auto-approve -input=false

let’s see the complete code here

please refer to this file here for the complete code.

you can also see the GitHub object here

let’s explore the pull event in the next part GitHub Part-3

Prev

next

--

--