Building GitHub Actions using go
Feel the passion
And see progression
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 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:
Nothing GitHub Actions specific, just some simple docker file with some entry point. And an interesting part, the definition of the action itself,
Here we can see basic meta info like
author and also important for us sections
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:
chatwhere to send notifications
tokenTelegram Bot token
statusof 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:
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.
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!
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