iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🚀

How to Automatically Post Articles to DEV.to with GitHub Actions

に公開

Introduction

This article introduces how to automatically post articles to the English-language tech blog platform "DEV.to" using GitHub Actions.

We will build a workflow where simply pushing a Markdown file to GitHub automatically publishes the article to DEV.to (providing a user experience similar to Zenn or Qiita).


Final Result

Push Markdown to GitHub repository
    ↓ (GitHub Actions)
Auto-post to DEV.to (published: true)

Benefits:

  • Manage articles using Markdown files
  • Track changes with Git history
  • Multi-site distribution with a single push (e.g., Quarto Blog + DEV.to)

Prerequisites

  • GitHub account
  • DEV.to account
  • GitHub repository (an existing blog repository is fine)

Step 1: Obtain a DEV.to API Key

1. Access DEV.to Settings

https://dev.to/settings/extensions

2. Generate an API Key

  1. Enter a name in the Description field (e.g., GitHub Actions Auto Publish)
  2. Click Generate API Key
  3. Copy the generated API key (a long string)

Step 2: Save to GitHub Secrets

Run Locally (Recommended)

cd your-repo
gh secret set DEV_TO_API_KEY
# Paste the API key (Press Enter twice)

Configuring via the Web UI

  1. GitHub repository → SettingsSecrets and variablesActions
  2. New repository secret
  3. Name: DEV_TO_API_KEY, Value: Copied API key
  4. Add secret

Step 3: Create GitHub Actions Workflow

Directory Structure

your-repo/
  .github/
    workflows/
      publish-to-devto.yml  ← Create this file
  devto/
    article1.md  ← Article for DEV.to
    article2.md

.github/workflows/publish-to-devto.yml

name: Publish to DEV.to

on:
  push:
    branches: [main]
    paths:
      - 'devto/**/*.md'
      - '.github/workflows/publish-to-devto.yml'

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Publish articles to DEV.to
        env:
          DEVTO_API_KEY: ${{ secrets.DEV_TO_API_KEY }}
        run: |
          for file in devto/*.md; do
            echo "Processing $file"

            # Extract frontmatter and content
            title=$(sed -n 's/^title: //p' "$file")
            description=$(sed -n 's/^description: //p' "$file")
            tags=$(sed -n 's/^tags: //p' "$file")
            canonical_url=$(sed -n 's/^canonical_url: //p' "$file")

            # Remove frontmatter to get body
            body=$(sed '1,/^---$/d; /^---$/,$d' "$file")

            # Create JSON payload
            json_payload=$(jq -n \
              --arg title "$title" \
              --arg body "$body" \
              --arg tags "$tags" \
              --arg canonical "$canonical_url" \
              '{article: {title: $title, body_markdown: $body, tags: ($tags | split(", ")), canonical_url: $canonical, published: true}}')

            # Post to DEV.to API
            response=$(curl -X POST https://dev.to/api/articles \
              -H "Content-Type: application/json" \
              -H "api-key: $DEVTO_API_KEY" \
              -d "$json_payload")

            echo "Response: $response"

            # Wait 10 seconds between posts to avoid rate limit
            sleep 10
          done

Step 4: Create the Article File

devto/my-first-article.md

---
title: My First Article
published: true
description: This is my first article on DEV.to
tags: javascript, webdev, tutorial
canonical_url: https://yourblog.com/my-first-article
cover_image: https://example.com/image.png
---

## Introduction

This is my first article!

## Code Example

\`\`\`javascript
console.log("Hello, DEV.to!");
\`\`\`

## Conclusion

Thank you for reading!

Explanation of Frontmatter

Field Description
title Article title
published true for immediate publication, false for draft
description Article description (displayed when shared on social media)
tags Comma-separated tags (maximum of 4)
canonical_url URL of the original article (for SEO)
cover_image Cover image URL (optional)

Step 5: Push to GitHub

git add .
git commit -m "Add DEV.to article"
git push

GitHub Actions will be executed automatically, and the article will be posted to DEV.to.


Verification

Check GitHub Actions logs

gh run list --limit 1
gh run view <run-id> --log

Check on DEV.to

https://dev.to/dashboard

Verify that the article has been published.


Rate Limit Measures

The DEV.to API has rate limits when posting continuously in a short period (429 error).

Measure: Add sleep 10 to the workflow (a 10-second delay between posts).

# Wait 10 seconds between posts to avoid rate limit
sleep 10

Advanced: Multi-site Automatic Distribution

You can distribute the same Markdown file to multiple platforms:

Push to GitHub

├→ Quarto Blog (GitHub Pages)
├→ DEV.to (GitHub Actions)
└→ Hashnode (GitHub Actions)

Benefits:

  • Distribute to multiple sites with a single write
  • Centralized management with a single Markdown file
  • No SEO issues with canonical_url settings

Troubleshooting

Article not being posted

  • Verify if the API key is correct (gh secret list)
  • Check if the article is in a draft state on the DEV.to Dashboard
  • Check the GitHub Actions logs for errors

Rate limit error (429)

  • Add a sleep 10 delay
  • Alternatively, wait for 5 minutes and then manually re-run

Tags not being reflected

  • Tags must be comma-separated (tags: javascript, webdev)
  • Maximum of 4 tags
  • Be careful about the presence of spaces


Conclusion

We have successfully implemented automatic posting to DEV.to using GitHub Actions.

By simply pushing a Markdown file, you now have a system in place to reach readers in the English-speaking world.

This same approach can be applied to other platforms like Hashnode, so please give it a try!

GitHubで編集を提案

Discussion