Automatically Tweet Hashnode blogs using GitHub Actions

Automatically Tweet Hashnode blogs using GitHub Actions

How to post your Hashnode blogs to Twitter using GitHub Actions (pipeline)

Play this article

This is how I post my Hashnode blogs to Twitter automatically by using GitHub Actions. You can see these tweets on my Twitter account.

Costs

Setup Hashnode API

  • First, check out this awesome blog by Catalin about the Hashnode API and make sure you can call the API and your blogs

  • Load the Hashnode API playground api.hashnode.com

  • Setup your Hashnode API playground like so, including your own blog name if hasnode playground hasn't done it for you already

    {
    user(username: "<ADD_YOUR_BLOG_USERNAME") {
      publication {
        posts(page: 0) {
          title
          brief
          slug
    
        }
      }
    }
    }
    

Create a GitHub Action

  • Follow this my previous blog on how to tweet using GitHub actions

  • Add some more steps before the tweet step

  • Add fetching Hashnode API

        # API request https://github.com/marketplace/actions/http-request-action
        - name: Deploy Stage
          id: myRequest
          uses: fjogeleit/http-request-action@master
          with:
            url: 'https://api.hashnode.com/'
            method: 'POST'
            data: '{"query":"{\n  user(username: \"<ADD_YOUR_USERNAME>\") {\n    publication {\n      posts(page: 0) {\n        title\n        brief\n        slug\n      }\n    }\n  }\n}\n"}'
            customHeaders: '{"headers": {"Content-Type": "application/json"}}'
    
  • Add another step to check your response

        - name: Show Response
          run: echo ${{ steps.myRequest.outputs.response }}
    
  • Add a JavaScript executor to run some JavaScript to modify the query

    # Sort Json array https://github.com/marketplace/actions/execute-javascript-inline
        - name: Execute JavaScript inline
          id: quoteFiltered
          uses: satackey/action-js-inline@v0.0.2
          with:
            # Edit the following line to install packages required to run your script.
            required-packages: axios
            script: |
              //setup
              const core = require('@actions/core')
              const axios = require('axios')
    
              //set output from other step            
              const quoteJson = ${{ steps.myRequest.outputs.response }}
    
              //Filter what blog to receive 
              randomNumber = Math.floor(Math.random() * 6);            
              const title = (String(quoteJson['data']['user']['publication']['posts'][randomNumber]['title']));
              const slug = (String(quoteJson['data']['user']['publication']['posts'][randomNumber]['slug']));
              //What tweet contents will be
              const finalQuote = (title+' '+'https://blog.azcodez.com/'+slug+'{ by @az_codez } from @hashnode #softwaredevelopment #coding #softwareengineering #programming #techblog') 
    
              console.log(finalQuote)
    
              //Output
              core.setOutput('finalQuote', finalQuote)
    
  • Make sure you run your GitHub action multiple times to check what you are going to tweet is correct and GitHub Action passes

  • Now add your tweet to Twitter step

    # Tweet to twitter https://github.com/marketplace/actions/tweet-to-twitter
        - name: TweetToTwitter
          uses: InfraWay/tweet-action@v1.0.1
          with:
            status: ${{ steps.quoteFiltered.outputs.finalQuote }}
            api_key: ${{ secrets.TWITTER_API_KEY }}
            api_key_secret: ${{ secrets.TWITTER_API_SECRET_KEY }}
            access_token: ${{ secrets.TWITTER_ACCESS_TOKEN }}
            access_token_secret: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
    

Credits

Shameless Plugs

Now you don't have to worry about reposing your blogs!

Hope this helps😁

Feel free to comment with questions or feedback✌️

Happy coding,

Az 👨🏾‍💻

Did you find this article valuable?

Support Asrin Dayananda by becoming a sponsor. Any amount is appreciated!