Automate your publishing on Hashnode with GitHub Actions.

John ShodipoJohn Shodipo
3 min read

Introduction

Have you ever imagined a streamlined process where all your articles, neatly organized in Markdown format, reside within a GitHub repository? Imagine the convenience of managing contributors through GitHub pull requests or effortlessly maintaining a history of changes, enabling easy reversion to previous versions.

In this article, we explore the development of a GitHub Action designed to automate the publishing process on Hashnode. Moreover, the action goes beyond, allowing you to import articles from popular platforms such as Notion, Dev.to, and Medium directly into Hashnode.

Building

This GitHub Action leverages the power of Hashnode's GraphQL API, seamlessly integrating with Notion's npm package and Dev.to's Developer API.

For Notion, the action retrieves the page using Notion's package and returns a group of blocks (a piece of content), which are then converted to markdown and posted to Hashnode. See implementation here.

Similarly, for Medium articles, the medium-to-markdown NPM package was used to directly convert Medium articles using their URLs to Markdown format.

Dev.to provides a public API to retrieve articles in markdown and html formats. Using the API Key provided, a list of articles by a user is retrieved and filtered out by the slug of the target article. The markdown body is retrieved and published to Hashnode.

Using the Github Action

Inputs

title (required)

The title of the blog post.

subtitle (optional)

The subtitle or description of the blog post.

cover_image (optional)

The cover image URL for the post

tags (optional)

Comma-separated string for the post tags

publication_id (required)

The ID of the publication to which the post belongs.

format (required)

Format of the content to be published [markdown, notion, medium, devto].

source (required)

File path (Markdown) or URL (Notion, Medium, Dev.to) of the content.

access_token (required)

Hashnode developer access token. Obtain this token from your Hashnode account settings.

notion_token (optional)

Notion integration token (only required if the format is notion). Read here to create an integration.

devto_token (optional)

Dev.to API Key (only required if the format is devto). Create one here.

Publish a markdown file

This code snippet below publishes a markdown file in our repository to Hashnode.

name: Publish Post
on:
  push:
    branches:
      - main
jobs:
  create_post:
    runs-on: ubuntu-latest
    name: A job to publish markdown to Hashnode

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
      - name: Publish
        id: publish
        uses: Johnkayode/hashnode-pub@v1.0.0
        with:
          title: Publish Markdown
          publication_id: '[Publication ID (from URL)]'
          format: markdown
          source: '[Path to Markdown file e.g README.md]'
          tags: "api hackathon, software"
          access_token: '${{ secrets.HASHNODE_ACCESS_TOKEN }}'
      - name: Get the publication URL
        run: echo "The Publish URL was ${{ steps.publish.outputs.url }}"

Publishing a sample Notion Page

name: Publish Post
on:
  push:
    branches:
      - main
jobs:
  create_post:
    runs-on: ubuntu-latest
    name: A job to publish notion page to Hashnode

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
      - name: Publish
        id: publish
        uses: Johnkayode/hashnode-pub@v1.0.0
        with:
          title: Publish Notion Page
          publication_id: '[Publication ID (from URL)]'
          format: notion
          source: '[URL to Notion page]'
          access_token: '${{ secrets.HASHNODE_ACCESS_TOKEN }}'
          notion_token: '${{ secrets.NOTION_TOKEN }}'
      - name: Get the publication URL
        run: echo "The Publish URL was ${{ steps.publish.outputs.url }}"

In this example, the actions will only be triggered by a push commit to the main branch. You can edit this to suit your repository structure.

You can add secret keys to Github and access them by using ${{secrets.MY_SECRET_KEY}} in your Github action file. To add environment variables to Github, you can set them on Settings > Security > Secrets and Variables > Actions. Then, click the Secrets Tab and create a new Repository Secret.

The basic features of this action allow you to publish articles to Hashnode from different sources. Future versions of this package will allow users to update existing articles and sync the articles on Hashnode with the repository.

Check out the GitHub repository: https://github.com/Johnkayode/hashnode-pub/

5
Subscribe to my newsletter

Read articles from John Shodipo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

John Shodipo
John Shodipo

Software Engineer