All Articles

Github action for adding a markdown post to Medium

Alt

Share your posts on Medium

Here is another yet article about automation of the blogging process with a self-hosted website.

Today we’re going to explore the medium API and take a look at GitHub action, which copies your website’s blog post to the medium in no pain.

The following code snippet publishes a blog post written in markdown using Github Actions directly to Medium.

Use cases

You may ask “How this could be applied in the real world?”

It really could if you automate your blog with Github Actions.

Here are a couple of examples of where to use it:

  • blogging system, to automate the process of publishing
  • exporting your existing blog to medium

Setup steps

  • Go to medium’s profile settings here > Integration tokens.
  • Enter “token name” and press Get integration token.
  • Now you need to get into a Github repository
  • Go to the repository settings page and secrets with previously saved keys.
MEDIUM_ACCESS_TOKEN="generated token"

Here is a quick example:

name: publish-to-medium
on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - id: files
        uses: jitterbit/get-changed-files@v1
      - id: posts
        name: Detecting posts from the changes
        run: |
          i=0
          for changed_file in ${{ steps.files.outputs.added_modified }}; do
            echo "Do something with ${changed_file}."
            if [[ "${changed_file}" == "content/posts"* ]];
            then
              echo "File ${changed_file} matched post."
              echo "::set-output name=post${i}::${changed_file}"
              ((i=i+1))
            fi
          done
      - if: steps.posts.outputs.post0
        name: Publish to medium
        uses: infraway/post-medium-action@main
        with:
          app_id: ${{ secrets.MEDIUM_APP_ID }}
          app_secret: ${{ secrets.MEDIUM_APP_SECRET }}
          access_token: ${{ secrets.MEDIUM_ACCESS_TOKEN }}
          markdown_file: ${{ steps.posts.outputs.post0 }}
          base_url: https://andrew.red
          post_url: https://andrew.red/posts
  • When the following code snippet is added, you can push anything into the repo and see a blog post also added to your Medium account.

Interested in giving it a try?

Find more useful DevOps automation tools at:

Of course, all the source code is open-sourced.

Any feedback, contribution, or help is highly appreciated.

Thanks for your time and have a nice coding! 👋

Resources

Published Apr 15, 2022

Passionate software engineer with expertise in software development, microservice architecture, and cloud infrastructure.