Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

feat: create autoblog workflow (#810)

Dillion Verma505457d2cfcd
diff --git a/.github/workflows/publish-article.yml b/.github/workflows/publish-article.yml
new file mode 100644
index 00000000..20ba40bd
--- /dev/null
+++ b/.github/workflows/publish-article.yml
@@ -0,0 +1,57 @@
+name: Publish Articles
+
+on:
+ workflow_dispatch:
+ inputs:
+ payload:
+ description: "JSON payload from webhook"
+ required: true
+
+jobs:
+ publish:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v4
+
+ - name: Save payload
+ run: echo '${{ toJson(github.event.inputs.payload) }}' > payload.json
+
+ - name: Write blog posts
+ run: |
+ node - <<'EOF'
+ const fs = require('fs');
+ const path = require('path');
+
+ const payload = JSON.parse(fs.readFileSync('payload.json', 'utf8'));
+
+ for (const article of payload.data.articles) {
+ // folder and file name
+ const folder = path.join('content', 'blog');
+ fs.mkdirSync(folder, { recursive: true });
+ const filePath = path.join(folder, `${article.slug}.mdx`);
+
+ // build front-matter
+ const fm = [
+ '---',
+ `title: "${article.title.replace(/"/g,'\\"')}"`,
+ `description: "${article.meta_description.replace(/"/g,'\\"')}"`,
+ `image: ${article.image_url}`,
+ `author: Dillion Verma`,
+ `tag: ${article.tags && article.tags.length ? article.tags[0] : ''}`,
+ `publishedOn: ${article.created_at}`,
+ `featured: true`,
+ '---\n'
+ ].join('\n');
+
+ // write file with front-matter + markdown content
+ fs.writeFileSync(filePath, fm + article.content_markdown);
+ }
+ EOF
+
+ - name: Commit & push
+ run: |
+ git config user.name "github-actions[bot]"
+ git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
+ git add content/blog/
+ git commit -m "Add/update blog articles from webhook" || echo "No changes"
+ git push
diff --git a/apps/www/app/api/blog-webhook/route.ts b/apps/www/app/api/blog-webhook/route.ts
new file mode 100644
index 00000000..41574ed0
--- /dev/null
+++ b/apps/www/app/api/blog-webhook/route.ts
@@ -0,0 +1,43 @@
+import { NextRequest, NextResponse } from "next/server";
+
+const REPO_OWNER = "magicuidesign";
+const REPO_NAME = "magicui";
+const WORKFLOW_FILE = "publish-article.yml";
+
+export async function POST(request: NextRequest) {
+ const auth = request.headers.get("authorization");
+ if (!auth || auth !== `Bearer ${process.env.BLOG_WEBHOOK_ACCESS_TOKEN}`) {
+ return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
+ }
+
+ // Get the raw payload from the incoming webhook
+ const body = await request.json();
+ const payload = JSON.stringify(body);
+
+ // Call GitHub Actions workflow_dispatch
+ const ghResp = await fetch(
+ `https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/actions/workflows/${WORKFLOW_FILE}/dispatches`,
+ {
+ method: "POST",
+ headers: {
+ Accept: "application/vnd.github+json",
+ Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ inputs: { payload },
+ }),
+ },
+ );
+
+ const text = await ghResp.text();
+
+ if (!ghResp.ok) {
+ return NextResponse.json(
+ { error: "GitHub error", details: text },
+ { status: 500 },
+ );
+ }
+
+ return NextResponse.json({ message: "Workflow dispatched", details: text });
+}
diff --git a/turbo.json b/turbo.json
index 6ab02ef5..d18063fb 100644
--- a/turbo.json
+++ b/turbo.json
@@ -2,7 +2,12 @@
"$schema": "https://turbo.build/schema.json",
"ui": "tui",
"envMode": "strict",
- "globalEnv": ["NODE_ENV", "NEXT_PUBLIC_POSTHOG_API_KEY"],
+ "globalEnv": [
+ "NODE_ENV",
+ "NEXT_PUBLIC_POSTHOG_API_KEY",
+ "BLOG_WEBHOOK_ACCESS_TOKEN",
+ "GITHUB_TOKEN"
+ ],
"tasks": {
"build": {
"dependsOn": ["^build"],