Tweet daily using Github Actions
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)
yml1name: Tweet23on:4 schedule:5 - cron: '0 10,16 * * *'6
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.
js1const Twitter = require('twitter');23const 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});910client.post('statuses/update', { status: tweet }, function (error, tweet, response) {11 if (!error) {12 console.log(tweet);13 }14});15
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.
json1"scripts": {2 "tweet": "node scripts/getQuote.js",3}4
Last step
What's remaining now is that we tell our Action to run yarn tweet
and that's it.
yml1jobs:2 build:3 runs-on: ubuntu-latest45 steps:6 - name: Tweet it yo7 run: |8 yarn install9 yarn tweet10 env:11 CI: true12 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 }}16
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.
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
https://twitter.com/learningcurvpod/status/1259425283003822080
Take care.
By Aravind Balla, a Javascript Developer building things to solve problems faced by him & his friends. You should hit him up on Twitter!