DEV Community

Cover image for [Article as Code] Syncing Articles Between Dev.to and Multiple Blogging Platforms
Jack
Jack

Posted on • Updated on

[Article as Code] Syncing Articles Between Dev.to and Multiple Blogging Platforms

In the world of content creation, each platform offers unique advantages. Publishing articles on various platforms helps us expand our audience. However, managing and synchronizing your articles across multiple platforms can become a tedious task.

To address this need, I've developed an application called "Article as Code," which boasts several key features:

  • Collects articles from your blog and stores them on GitHub as a "source of truth".
  • Synchronizes all articles located in the repository to all your desired platforms.
  • Allows you to write new articles by committing directly to this repository.

Setup your own AAC

Step 1: Create A New Github Repo

Step 2: Create Github Action Secretes

Go to Repo > Setting > Secrets and Variables > Actions > New repository secrete
Enter fullscreen mode Exit fullscreen mode

You will need to create the following secrets:

  • DEVTO_TOKEN: Your Dev.to authentication token.
  • DEVTO_USERNAME: Your Dev.to username.
  • HASHNODE_TOKEN: Your Hashnode authentication token.
  • HASHNODE_USERNAME: Your Hashnode username.

Step 2: Schedule sync process

To automate the synchronization process, we'll set up a GitHub Action workflow. Create a new file named .github/workflows/cronjob.yml in your repository with the following contents:

name: "Cronjob"
on:
  schedule:
    - cron: '15 */6 * * *'
  push:
    branches:
      - 'main'

jobs:
  sync:
    permissions: write-all
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-go@v4
        with:
          go-version: '1.21.0'
      - name: Prepare
        run: export PATH=$PATH:$(go env GOPATH)/bin
      - name: Install
        run: go install github.com/huantt/article-as-code@v1.0.3
      - name: Collect
        run: |
          article-as-code collect \
          --username=${{ secrets.DEVTO_USERNAME }} \
          --article-folder=articles
      - name: Sync to dev.to
        run: |
          article-as-code sync \
          --username=${{ secrets.DEVTO_USERNAME }} \
          --auth-token=${{ secrets.DEVTO_TOKEN }} \
          --article-folder=articles \
          --destination="dev.to"
      - name: Sync to hashnode.dev
        run: |
          article-as-code sync \
          --username=${{ secrets.DEVTO_USERNAME }} \
          --auth-token=${{ secrets.HASHNODE_USERNAME }} \
          --article-folder=articles \
          --destination="hashnode.dev"
      - name: Commit
        run: |
          git config user.name github-actions
          git config user.email github-actions@github.com
          git add .

          if git diff --cached --exit-code; then
            echo "No changes to commit."
            exit 0
          else
            git commit -m "update"
            git rebase main
            git push origin main
          fi
Enter fullscreen mode Exit fullscreen mode

This GitHub Action will run every 6 hours or whenever you push a new commit to the main branch. Here's what it does:

  1. Collect all your articles from dev.to then store into the articles folder.

  2. Sync articles to dev.to
    (In this case, it's redundant, but it will cover the scenario when you write a new article by committing directly to this repository.)

  3. Sync articles to hashnode.dev

References

  • See my complete repository here:

    GitHub logo huantt / articles

    This is the "single source of truth" that stores all my articles.

    About

    This is the "single source of truth" that stores all my articles.

    It utilizes huantt/article-as-code to collect, store, and sync all my articles to various platforms, including dev.to and hashnode.dev.

    GitHub Action

    I have created a GitHub action in the .github/workflows directory that runs every 6 hours or whenever you commit to the main branch.

    My Recent Articles

    thumbnail Migrate Redis to AWS ElastiCache
    How to Migrate Redis from One Server to Another Starting from Redis version 5.0.0, Redis...
    19/10/2023
    thumbnail Writing Resume as Code - Why not?
    Why I Developed Resume as Code I've explored various CV builder platforms over the...
    12/10/2023
    thumbnail Showing more Article info on Daily.dev
    Daily.dev is a very good extension that helps us aggregate news from several sources. When...
    05/10/2023
    thumbnail [Article as Code] Syncing Articles Between Dev.to and Multiple Blogging...
    In the world of content creation, each platform offers unique advantages. Publishing articles on...
    03/10/2023
  • Source code:

    GitHub logo huantt / article-as-code

    Article as Code: Collect articles into files and synchronize them with publications

    About

    License Go Report Card

    AAC a.k.a. Article as Code helps you collect articles from data sources, such as dev.to, and then stores them as static files.

    It also helps you sync static files to create articles on your publications.

    Install

    go install github.com/huantt/article-as-code@latest
    Enter fullscreen mode Exit fullscreen mode

    Add GOPATH/bin directory to your PATH environment variable, so you can run Go programs anywhere.

    export GOPATH=$HOME/go
    export PATH=$PATH:$(go env GOPATH)/bin
    Enter fullscreen mode Exit fullscreen mode

    Usage

    Collect articles

    Usage:
       collect [flags]
    
    Flags:
      -f, --article-folder string   Article folder (default "data/articles")
      -h, --help                    help for collect-articles
          --rps int                 Limit concurrent requests (default 5)
      -u, --username string         Username
    Enter fullscreen mode Exit fullscreen mode

    For example

    article-as-code collect \
    --username=jacktt \
    --rps=5 \
    --article-folder=static
    Enter fullscreen mode Exit fullscreen mode

    Sync articles

    Usage:
       sync [flags]
    
    Flags:
      -f, --article-folder string   Article folder (default "data/articles")
      -a, --auth-token string       Auth token
      -h, --help                    help for sync-articles
          --rps int                 Limit concurrent requests (default 5)
      -u, --username string         Username
    Enter fullscreen mode Exit fullscreen mode

    For

I will appreciate it if you contribute to support other platforms.

Top comments (6)

Collapse
 
softwaresennin profile image
Mel♾️☁️

great article, thanks so much. Got a question. I get this error in my actions

Restore cache failed: Dependencies file is not found in /home/runner/work/articles/articles. Supported file pattern: go.sum

Also, I created the articles subfolder as well so the articles are added to it but though the job runs and shows that error i mentioned, the folder still does not get filled up with the articles. Any idea why?

Collapse
 
jacktt profile image
Jack

Could you share me your repo?

Collapse
 
softwaresennin profile image
Mel♾️☁️ • Edited

yes i definitely can. Here it is github.com/apotitech/articles.git

Thread Thread
 
jacktt profile image
Jack • Edited

I found the problem. Please update the Commit action with the following content:

      - name: Commit
        run: |
          git config user.name github-actions
          git config user.email github-actions@github.com
          git add .

          if git diff --cached --exit-code; then
            echo "No changes to commit."
            exit 0
          else
            git commit -m "update"
            git pull --rebase
            git push origin main
          fi
Enter fullscreen mode Exit fullscreen mode

I also updated something in this file, please rebase before trying it.

Thread Thread
 
softwaresennin profile image
Mel♾️☁️ • Edited

thanks so much for your help. I have adjusted all that. Now I get an error in the hashnode.dev process. Here is the error i get

Run article-as-code sync \
article-as-code sync \
--username=*** \
--auth-token=*** \
--article-folder=articles \
--destination="hashnode.dev"
shell: /usr/bin/bash -e {0}

[debug]/usr/bin/bash -e /home/runner/work/_temp/e078871f-4e65-4b6e-b0fb-e533f11e19a0.sh

time=2023-10-05T17:24:33.645Z level=ERROR msg="[map[extensions:map[code:DATASOURCE_ERROR] locations:[map[column:3 line:2]] message:It seems you have provided incorrect data path:[createStory]]]"
Error: Process completed with exit code 1.

Any idea what may cause this ?

I also did some changes in the sync.yml file to try and fix but same error
github.com/apotitech/articles/comm...

Thread Thread
 
jacktt profile image
Jack

Please update to the latest version from /huantt/articles. Then, please let me know if you still encounter any errors.