Building GitHub Actions using go

Alexey Grachov
Sep 8 · 3 min read

Feel the passion
And see progression
It’s introduction
To GitHub Actions

Hello and welcome to practical introduction to GitHub Actions.

Today we’ll build an Action that will notify build status to Telegram messenger.

Creating Action from Template

GitHub Action can be one of two types: javascript or Docker. We don’t want to mess with js so our choice is Docker. Go to the Container Action Template and click “Use this template” button to start.

GitHub will create a repository for with bootstrapped action for you. Now you can clone it and start building!

Ok, let’s examine what we have here. Dockerfile:

Dockerfile

Nothing GitHub Actions specific, just some simple docker file with some entry point. And an interesting part, the definition of the action itself,action.yml:

action.yml

Here we can see basic meta info like name , description and author and also important for us sections inputs and runs.

Inputs section describes input variables that will be available from action runtime (in the form of environment variables or command-line arguments).

Runs section describes what actually will be executed. In our case using: 'docker' means GitHub Action is of Docker type. This may be using: 'node12' for js actions. As a Docker image, we use Dockerfile because we’re going to build it from our Dockerfile found above.

You can find full official docs about metadata syntax here.

Telegram Notification application

Ok, step back from Actions stuff for a moment. To continue let’s write a simple program that will receive inputs from Actions runtime and send notifications to Telegram. I use tbot for interactions with Telegram so this should be easy:

Fine, now we can see the requirements to Actions runtime for our application. We clearly need three inputs:

  • chat where to send notifications
  • token Telegram Bot token
  • status of the build

It’s high time to write action definition:

The only thing to note is that we mark all inputs as required for our action to work. All inputs should be provided in a workflow file that will use this action. Here is an example of usage:

Here chat and token values are stored in repository Secrets and the user has access to it using secrets object. On the other hand status value get from special job object available during action execution.

Publish

Push everything to your GitHub repo and you should see a special notification from GitHub that you can publish your action to the Marketplace:

GitHub uses releases mechanism for version management of actions, so create new release is necessary. Just follow the instructions on publishing and enjoy!

Test

You can see an example of full workflow in tbot repo here. The last thing to do is push something to the repo and enjoy the result:

You can find full code and example usage in yanzay/notify-telegram repository https://github.com/yanzay/notify-telegram and published Action in Marketplace https://github.com/marketplace/actions/notify-telegram

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade