-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(github-actions): publish posts update on *.mdx changes to GCP Pu…
…b/Sub
- Loading branch information
1 parent
0fd1964
commit e85d05c
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
name: Publish Post Updates | ||
|
||
on: | ||
push: | ||
paths: | ||
- '**/*.mdx' | ||
|
||
jobs: | ||
publish: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Node.js | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18 | ||
|
||
- name: Install dependencies | ||
run: npm install gray-matter @google-cloud/pubsub | ||
|
||
- name: Authenticate to Google Cloud | ||
id: auth | ||
uses: google-github-actions/auth@v2 | ||
with: | ||
project_id: ${{ secrets.GCP_PROJECT_ID }} | ||
workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }} | ||
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT_EMAIL }} | ||
|
||
- name: Set up environment | ||
run: | | ||
echo "CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD)" >> $GITHUB_ENV | ||
env: | ||
CHANGED_FILES: "" | ||
|
||
- name: Publish updates to GCP | ||
env: | ||
GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} | ||
GCP_TOPIC_NAME: ${{ env.GCP_TOPIC_NAME }} | ||
CHANGED_FILES: ${{ env.CHANGED_FILES }} | ||
run: node github-actions-scripts/publish-post-update.js |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { PubSub } = require('@google-cloud/pubsub'); | ||
const matter = require('gray-matter'); | ||
const { execSync } = require('child_process'); | ||
|
||
// Configure Pub/Sub | ||
const topicName = process.env.GCP_TOPIC_NAME; | ||
|
||
// Initialize PubSub client using default credentials | ||
const pubSubClient = new PubSub(); | ||
|
||
// Extract frontmatter from an MDX file | ||
function extractFrontmatter(filePath) { | ||
const content = fs.readFileSync(filePath, 'utf-8'); | ||
const { data: frontmatter } = matter(content); | ||
return frontmatter; | ||
} | ||
|
||
// Determine event type based on git diff | ||
function determineEventType(filePath) { | ||
const slug = path.basename(filePath, '.mdx'); | ||
try { | ||
const creationOutput = execSync(`git log --diff-filter=A -- ${filePath}`).toString().trim(); | ||
if (creationOutput) { | ||
return 'POST_CREATED'; | ||
} | ||
|
||
if (!fs.existsSync(filePath)) { | ||
return 'POST_DELETED'; | ||
} | ||
|
||
const diffOutput = execSync(`git diff HEAD~1 HEAD -- ${filePath}`).toString().trim(); | ||
if (diffOutput.includes('title') || diffOutput.includes('tags') || diffOutput.includes('description')) { | ||
return 'META_UPDATED'; | ||
} | ||
|
||
return 'CONTENT_UPDATED'; | ||
} catch (error) { | ||
console.error(`Error determining event type for ${slug}:`, error); | ||
throw error; | ||
} | ||
} | ||
|
||
// Publish event to Pub/Sub | ||
async function publishEvent(slug, frontmatter, eventType) { | ||
const post = { | ||
title: frontmatter.title, | ||
tags: frontmatter.tags, | ||
created_at: frontmatter.date, | ||
description: frontmatter.description, | ||
slug, | ||
}; | ||
|
||
const message = { | ||
data: post, | ||
message_id: `${slug}-${Date.now()}`, | ||
publish_time: new Date().toISOString(), | ||
attributes: { | ||
eventType, | ||
slug, | ||
}, | ||
}; | ||
|
||
const dataBuffer = Buffer.from(JSON.stringify({ message, subscription: "your-subscription-name" })); | ||
|
||
try { | ||
await pubSubClient.topic(topicName).publishMessage({ data: dataBuffer }); | ||
console.log(`Message published for slug: ${slug}`); | ||
} catch (error) { | ||
console.error(`Failed to publish message for slug: ${slug}`, error); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
// Main script | ||
(async () => { | ||
const changedFiles = process.env.CHANGED_FILES.split(',').filter((file) => file.endsWith('.mdx')); | ||
|
||
for (const file of changedFiles) { | ||
const slug = path.basename(file, '.mdx'); | ||
const frontmatter = extractFrontmatter(file); | ||
const eventType = determineEventType(file); | ||
|
||
console.log(`Detected change in ${file}: Event Type - ${eventType}`); | ||
await publishEvent(slug, frontmatter, eventType); | ||
} | ||
})(); |