Edward Thomson

This is day 30 of my GitHub Actions Advent Calendar. If you want to see the whole list of tips as they're published, see the index.

Advent calendars usually run through Christmas, but I'm going to keep posting about GitHub Actions through the end of December. Consider it bonus content!

Yesterday we built an action that posted to a GitHub issue. It showed how easy it is to work with GitHub APIs from within an action. But actions are generally written in JavaScript, so they can easily integrate with APIs from other services. This lets you create actions that can create an issue in Jira, send a text message, or upload security information about your project.

One of the things that I want to automate is sending a tweet – this is something I do whenever I release a new version of something, to announce its availability.

And I'd love an action that I can use in a workflow to automate this. If you look in the GitHub Actions Marketplace, you can see that there is actually an action already that can tweet. Unfortunately, it's built as a docker container.

You can create actions as either a Docker container or a JavaScript application. It might seem easier to get started with a container-based action, but there are limitations: only Linux based workflows (those that run on ubuntu-latest) can use them. Using a JavaScript action ensures that your action can be used in any workflow.

So if I want to send a tweet whenever any of my projects are released – including the ones that run on macOS or Windows – then I'll need to create a JavaScript action that can send a tweet.

To do this, I'll start with the sample TypeScript action and add in the twitter npm package so that I can communicate with the Twitter API. This is quite straightforward – I'll need to take the authentication as inputs to the action and use those to create a Twitter API instance.

const twitter = new Twitter({
  consumer_key: core.getInput('consumer-key'),
  consumer_secret: core.getInput('consumer-secret'),
  access_token_key: core.getInput('access-token'),
  access_token_secret: core.getInput('access-token-secret')
})

Then I can send a tweet by calling the /statuses/update API. I'll take another input to the action that will be the status to tweet.

twitter.post(
  '/statuses/update',
  {status: core.getInput('status')},
  (error, data, response) => { if (error) throw error }
)

Once I've got that written, all I need to do is package it up – creating a distribution branch, pushing that up to GitHub, and then creating a release.

You can find the finished product at github.com/ethomson/send-tweet-action, so you can use ethomson/send-tweet-action@v1 as part of a workflow.

Now any time I push to this repository, it will send a tweet.

You can use this action to send a tweet whenever a release is created, a wiki is updated, or a deployment occurs. Or, just for fun, like what I've done.

It's easy to integrate other APIs into an action to let you automate other tools based on activities in your repository. I can't wait to see what you build!