Aravind Balla/writings

Tweet daily using Github Actions

Last edited on May 12, 2020

Photo by Charles Deluvio on Unsplash

Github Actions are really powerful. I like to see them as personal assistants. You tell your assistants to do some work for you. Similarly, you tell Github actions to do some job for you.

I’ve recorded a video while building this. If you’d like to watch me build, here is the video 👇

The main usecase for these actions, I feel, is when we use it to publish your library to npm or deploying your app on S3 or Github Pages whenever you push new changes. CI/CD - like the techies call it.

These actions can be triggered in different scenarios. In the above usecase, it is triggered on push. Similarly, you can also do it on a pull-request where we run a few checks or welcome new contributors to the project.

Another way to trigger an action is on a schedule, which is what I used to make the action Tweet everyday. 🗓We can give CRON expressions to schedule the triggers for the action.

Okay, where do we tell all these things to the Action? There’s got to be some config, right?

Yeah. We have a yaml file for every action and we specify all the configuration for it there. All the yamls for your actions lie in .github/workflows path in your GitHub repo.

This is the action config I wrote

(Trimmed version. Full code here)

1name: Tweet
2
3on:
4 schedule:
5 - cron: '0 10,16 * * *'

Now we know how to schedule things. Next step?

Tweet

We know there should be an API to post tweets. But for that, you need a few API credentials from Twitter. This was a big time taking process for me as the application for a dev account on Twitter had to go through a few reviews from Twitter employees.

Once you get the credentials, tweeting is as easy as the code below.

Full Code

1const Twitter = require('twitter');
2
3const client = new Twitter({
4 consumer_key: process.env.TWITTER_API_KEY,
5 consumer_secret: process.env.TWITTER_API_SECRET,
6 access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
7 access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
8});
9
10client.post('statuses/update', { status: tweet }, function (error, tweet, response) {
11 if (!error) {
12 console.log(tweet);
13 }
14});

I have this in a file scripts/getQuote.js and I run using yarn tweet. We add a script in package.json to be able to do this.

1"scripts": {
2 "tweet": "node scripts/getQuote.js",
3}

Last step

What’s remaining now is that we tell our Action to run yarn tweet and that’s it.

1jobs:
2 build:
3 runs-on: ubuntu-latest
4
5 steps:
6 - name: Tweet it yo
7 run: |
8 yarn install
9 yarn tweet
10 env:
11 CI: true
12 TWITTER_API_KEY: ${{ secrets.TWITTER_API_KEY }}
13 TWITTER_API_SECRET: ${{ secrets.TWITTER_API_SECRET }}
14 TWITTER_ACCESS_TOKEN_KEY: ${{ secrets.TWITTER_ACCESS_TOKEN_KEY }}
15 TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}

If you observe, we pass the Twitter API credentials as environment variables. This can be done in the Repo settings in GitHub. You can add a few secrets and they get passed to the Actions in that repo.

Screenshot of Github Repo settings

That takes us to the end of the article. Thanks for reading till end and if you think learnt something from this, please tweet it so your friends learn too.

If you’d like to check out the tweets in action, its here -https://twitter.com/learningcurvpod

Take care.


Aravind Balla

By Aravind Balla who is a friendly human, building things for himself, and sometimes for others. You should hit him up on Twitter!