DEV Community

JackTT
JackTT

Posted 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


    GraphQL Types
    Field declaration By default, it's valid for any field in your schema to return null...

    03/07/2024



    thumbnail


    Graphql Fundamental
    GraphQL provides a complete and understandable description of the API, including both "API endpoints"...

    03/07/2024



    thumbnail


    Snowflake Schema vs. Star Schema: Pros, Cons, and Use Cases
    Star Schema Structure: Central Fact Table: Contains quantitative data for...

    11/06/2024



    thumbnail


    Is JWT Safe When Anyone Can Decode Plain Text Claims
    If I get a JWT and can decode the payload, how is it secure? Why couldn't I just grab the token out...

    06/06/2024



    thumbnail


    Understanding



  • 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 (0)