DEV Community

protium
protium

Posted on • Edited on

Publish your blog articles everywhere with this github action

Long ago I made this comment in this article:

This is a really good idea with great potential. 
Imagine a standarized API for different blogs. 
You can automatize publishing and editing, multiple collaborators.
And use a git provider as unique source of content.

And you can also make your git repo as some sort of blog.
I'll use it for my next posts for sure.

Thanks!
Enter fullscreen mode Exit fullscreen mode

It was an idea that I had left on the back of my head and I didn't come back to it because I wasn't writing articles actively. But this has changed in the last months and I have published 6 articles during December. So I decided to revisit the idea and finally develop it.

blogpub

blogpub is a github action developed with typescript with the purpose of allowing you to use a github repository as source of truth for your blog posts. The action supports:

  • Automatically publishing to Medium and DEV.to
  • Templating
  • Metadata/config section (published, tags, title, etc)

In fact, this article was automatically published by blogpub and you can see it's source in this folder.

Template Support

Sometimes we want to add different content to our blog posts depending on the platform, for instance for dev.to I want to use liquid tags, which medium doesn't support.
For this purpose I have used handlebars to make use of conditionals. Example:

{{#if medium}}
This is only for Medium
{{/if}}
{{#if devto}}
This is only for DEV.to
{{/if}}
Enter fullscreen mode Exit fullscreen mode

For the first release of the action, the template context contains:

{
  medium: boolean;
  devto: boolean;
}
Enter fullscreen mode Exit fullscreen mode

Article metadata

I have added support for a metadata section (similar to dev.to) where we can specify the following attributes:

  • title: [string] The title of the article. If not specified, the first H1 heading will be used.
  • description: [string] Description for dev.to API.
  • tags: [string] Comma separated tags. Note: Medium allows up to 5 tags whereas Dev.to only 4.
  • license: [string] Medium license type. Refer to Medium API Docs. Default: public-domain
  • published: [boolean]. Default: true

Example:

---
title: "First blogpub test"
tags: test, ci
---
# I'm using `blogpub`
Enter fullscreen mode Exit fullscreen mode

Adopting CI/CD practices for blogging

Imagine the following scenario:

  • Write an article
  • Create PR
  • CI checks the spelling
  • Your peers review the article and propose changes or approve it
  • Merge the PR to main and it gets published everywhere
  • Use send tweet action to promote your new article.

I can imagine this workflow in a company where different colleagues write articles together. I like the idea that we could also automatize tweets, this is why blogpub outputs the URLs of the article after publishing it.

Usage

Ideally you want to run this action for every push to your main branch. This setup should be enough.

name: 'publish'

on:
  push:
    branches:
      - 'main'
    paths:
      - 'articles/*'
jobs:
  publish:
    name: publish new article
    runs-on: ubuntu-latest    
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: blogpub
        uses: protiumx/blogpub@v0.2.3
        with:
          articles_folder: articles
          devto_api_key: ${{secrets.DEV}}
          gh_token: ${{secrets.GH}}
          medium_token: ${{secrets.MEDIUM}}
          medium_user_id: <user_id>
Enter fullscreen mode Exit fullscreen mode

Few things to take into account:

  • We only want to run the action for files inside a folder.
  • You need to setup repository secrets with your tokens and api keys.

Check this blog source to see this in action.

What is missing?

For the first release I just wanted to be able to publish the articles in Medium and DEV.to. But there are a few features that would be handy:

  • Uploading images: Imagine also versioning images and then uploading those images automatically
  • Support for updates: If you want to make a correction, it should be possible to update the source and it should get reflected in all the platform the article was published to.
  • Platform specific configuration: maybe we want different tags per platform
  • Support multiple articles: at the moment blogpub publishes only 1 article per run.

What else would you like to have? Let me know in the comments!

That's it!
As usual, any help is well received and I have a TODO list if you would like to collaborate with this project.

GitHub logo protiumx / blogpub

Github action to publish your blog articles from Markdown to Medium or Dev.to

blogpub

CI

Github action to publish your blog articles to Medium or Dev.to using their respective REST APIs The action searches for markdown files in the commit of the push event and uses first md file that finds.

Updating articles

Currently it's not supported to update the articles on the different platforms. If the markdown file found in the push event already exists on the commit before, the action will skip it. This avoids publishing the article again.

Pre-requisites

In order to interact with both platforms API's you will need:

Usage

The action will grab an markdown file from a push event to a branch. The following workflow configuration will publish articles that are committed to the main branch:

on:
  push:
    branches:
      - 'main'
    paths:
      - 'articles/*'
Enter fullscreen mode Exit fullscreen mode

Note

🤖

Top comments (1)

Collapse
 
bcouetil profile image
Benoit COUETIL 💫

Looking forward to the update option 🥹. I mostly update my articles, the publish part can stay manual for me.