Skip to content

A GitHub Action for publishing Posts to a Publication in Hashnode

License

Notifications You must be signed in to change notification settings

OmenApps/publish-github-to-hashnode

Repository files navigation

Publish GitHub to Hashnode GitHub Action

This GitHub Action publishes blog posts from a GitHub repository to a specific publication on Hashnode. It reads markdown files, processes the frontmatter and content, and uses the Hashnode API to create, update, or delete posts.

Features

  • Create new posts on Hashnode if they do not exist.
  • Update existing posts on Hashnode if they exist.
  • Delists posts on Hashnode if the corresponding markdown file is not present in the current commit.
  • Handles correct linking of cover images and inline images in the markdown content.

Inputs

  • access-token (required): Your Hashnode API Personal Access Token. See: Hashnode Developer Settings
  • added-files (required): The list of added files in the repository, automatically provided by the tj-actions/changed-files action in the examples below.
  • changed-files (required): The list of changed files in the repository, automatically provided by the tj-actions/changed-files action in the examples below.
  • publication-host (required): The publication host (e.g., blog.mydomain.com).
  • posts-directory (optional): The local directory in this repo containing the blog posts, if different from the root directory. Default: .. Example: content/posts.

Outputs

  • result_json: Publishes result as a JSON string.
  • result_summary: Publishes result summary formatted as text.

Usage

1. Create a .github/workflows/publish.yml file

Create a new workflow file in your repository to define the steps required to publish the posts.

name: Publish My Hashnode Blog Posts
on:
  push:

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
        - uses: actions/checkout@v4
          with:
            fetch-depth: 0

        - name: Get changed files
          id: changed-files
          uses: tj-actions/changed-files@v44

        - uses: actions/setup-python@v5
          with:
            python-version: '3.x'

        - name: Publish to Hashnode
          uses: actions/publish-github-to-hashnode@v1
          with:
            added-files: ${{ steps.changed-files.outputs.added_files }}  # Uses output from changed-files action
            changed-files: ${{ steps.changed-files.outputs.all_changed_files }}  # Uses output from changed-files action
            access-token: ${{ secrets.HASHNODE_ACCESS_TOKEN }}
            publication-host: 'blog.mydomain.com'  # Your publication host
            posts-directory: 'content/posts'  # Dir within your repo containing the markdown files, if different from root dir

2. Store your Hashnode API Access Token as a GitHub Secret

  1. Obtain your Hashnode API Personal Access Token. See: Hashnode Developer Settings
  2. Go to your repository on GitHub.
  3. Click on Settings.
  4. Scroll down to Secrets and variables and click on Actions.
  5. Click New repository secret.
  6. Add a new secret with the name HASHNODE_ACCESS_TOKEN and your Hashnode API token as the value.

3. Prepare your repository structure

Ensure your repository contains the markdown files you wish to publish in the specified directory (default is the root of the repository).

4. Markdown Post Frontmatter

Frontmatter Fields

Full list of frontmatter fields that can be used in the markdown files:

  • title (required): Title of the post.
  • subtitle (optional): Subtitle of the post.
  • slug (required): Slug of the post.
  • tags (optional): Tags of the post (comma-separated).
  • enableTableOfContents (optional, default: false): Enable table of contents.
  • publish (optional, default: true): Should the post be published at this time.
  • coverImage (optional): Cover image relative path within the repository starting from posts-directory (as specified in pubish.yml) if provided.
  • coverImageAttribution: Information about the cover image attribution (optional)
  • publishedAt: Date and time when the post was published (optional)
  • disableComments (optional, default: false): Disable comments on the post.

Example Frontmatter

---
title: Creating Spaghetti in Docker Compose
slug: creating-spaghetti-in-docker-compose
tags: docker,docker-compose
enableTableOfContents: true
coverImage: images/cover.jpg
---

## Introduction

This is an introduction to creating spaghetti in Docker Compose.

## Ingredients

- Docker Engine
- Spaghetti
- Sauce
- Cheese
- Love

## Steps

1. ...
2. ...
3. ...

5. Handling Image URLs

The action will automatically convert relative image URLs to absolute URLs that point to the raw content on GitHub. Ensure your image paths in the markdown are correct relative paths.

Example Workflow Using result_json

You can utilize the result_json output in subsequent steps to get the result of the publish operation in json format. The approach below can also be used with result_summary for a text summary.

name: Publish My Hashnode Blog Posts
on:
  push:

jobs:
    publish:
      runs-on: ubuntu-latest
      steps:
        - uses: actions/checkout@v4
          with:
            fetch-depth: 0

        - name: Get changed files
          id: changed-files
          uses: tj-actions/changed-files@v44
        
        - uses: actions/setup-python@v5
          with:
            python-version: '3.x'
                
        - name: Publish to Hashnode
          id: publish
          uses: actions/publish-github-to-hashnode@v1
          with:
            added-files: ${{ steps.changed-files.outputs.added_files }}
            changed-files: ${{ steps.changed-files.outputs.all_changed_files }}
            access-token: ${{ secrets.HASHNODE_ACCESS_TOKEN }}
            publication-host: 'blog.mydomain.com'
            posts-directory: 'content/posts'
        
        - name: Get the output JSON
          run: echo "${{ steps.publish.outputs.result_json }}"

Development

To contribute to the development of this action, clone the repository, make your changes, and submit a pull request. Please ensure you submit a detailed Issue describing the work you are planning to do prior to submitting a PR.

License

This project is licensed under the MIT License.

Questions and Support

If you have any questions or need support, please open an issue in the GitHub repository. I will do my best to help.