Zen Quotes and the Art of GitHub Actions

Amit Mukherjee
4 min readMay 22, 2021

--

Photo by Denis Oliveira on Unsplash

Weird title, right. Yeah, I thought the same as I was writing this but I feel that it identifies with my current thought process and so I went with it. I have been a spiritual seeker for the last few years of my life and I also passionately love technology, especially all things Cloud and Code.

Due to the pandemic over the past year and half or so (though it feels a lot longer) we have all be cloistered in our homes stuck in a mental echo chamber which can tend to accentuate both of our positives and negatives. And starting the week every Monday and going into the weekend every Friday filled me with a sense of dread. I felt that this was because of the change it represented from a predictable routine which my mind had just adapted to. I was thus looking for inspiration and stumbled upon this really cool quotation site https://zenquotes.io/ . I wanted to leverage their quotes to inspire me at the beginning and end of the week so as to be able to look forward to them instead. At about the same time thanks to a good friend, I got introduced to this cool CI/CD tech. called GitHub Actions and it blew me away.

Photo by Roman Synkevych on Unsplash

GitHub Actions is a workflow automation which lets us build, package and deploy our apps right from within our code repository. As developers, we are extremely familiar with coding toolchains but we (at least I) tend to get sweaty when it comes to their deployment. We have long been hearing of these things called Jenkins, Chef, Puppet etc. etc., you know things we would ideally not want to know but which magically deploy our code to the places where they are supposed to run. With GitHub Actions, we can make our lives really simple by deploying them using simple YML. So, I decided I would develop a simple script in NodeJS that would send me an inspiring quote automatically using my new best CI/CD friend. So, lets get on with it, shall we!

Ok, first things first. We start with creating the Slack Channel on which we would be sending the Notifications to. Once the Slack channel is created, this link here describes how to create an Incoming Webhook which is a fancy word for a URL where we could send our notifications to. You can even test this by sending a simple Post request as shown below.

curl -X POST -H 'Content-type: application/json' --data '{"text": "Hello there!"}' YOUR_WEBHOOK_URL

You can even add some advanced formatting as described here. Now on to some NodeJS. As can be seen below, the code is quite simple. This is the code repository and the main function is as below. It gets the Zen quote, then sets the Slack payload and finally sends out the Slack message. Since the Slack URL is a secret, it is stored in a .env file which gets loaded as an environment variable when the function runs with the dotenv library. On GitHub, in the repository a secret has been created with the name SLACK_URL with the Incoming Webhook URL. This lets us seamlessly operate between GitHub and our local development environment.

NodeJS Code for sending the notifications

Now for some GitHub Actions magic. So what I want from Mr. Actions is the following:

  1. Checkout the Code
  2. Install NodeJS and its dependencies
  3. Run the Code and Send out the Notification periodically.

So, in order to do that a special directory called /.github/workflows/ is created within the repository and then a simple file called action.yml is created as shown below which does the 3 steps described in YML. Github automatically picks it up as an action and it is visible under the Actions Tab in the repo once it is set up this way.

Action.yml for GitHub Actions

The on section tells it when to run the specified action. The schedule and the cron sections describe when to run the action. The jobs section describes the steps to run in order to complete the actions specified in the file with the env section indicating the environment variable which the action needs to run the job and to get it using the SLACK_URL key stored within Github Secrets section of this repository. Once the above file is in place, a Slack Notification is sent which looks like this.

Slack Notification received when the job runs

And that’s it. You are all set to awesome Zen Quotes periodically powered by the amazing GitHub Actions.

--

--