Skip to content

Commit

Permalink
Fix: auto publish implementation (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
beelchester authored Jul 27, 2024
1 parent 4a2952c commit ede5178
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
15 changes: 5 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ on:
branches: [develop]
types: [opened, reopened, synchronize]

permissions:
contents: write

jobs:
build-and-deploy:
concurrency: ci-${{ github.ref }}
Expand Down Expand Up @@ -38,28 +41,20 @@ jobs:
with:
folder: build

- name: Get changed markdown files
id: changed-markdown-files
uses: tj-actions/changed-files@v44
with:
files: |
blog/**/*.{md,mdx}
- name: Publish to Hashnode and Dev.to 📝
env:
HASHNODE_PAT: ${{ secrets.HASHNODE_PAT }}
HASHNODE_PUBLICATION_ID: ${{ secrets.HASHNODE_PUBLICATION_ID }}
ADDED_FILES: ${{ steps.changed-markdown-files.outputs.added_files }}
DEVTO_API_KEY: ${{ secrets.DEVTO_API_KEY}}
DEVTO_ORG_ID: ${{secrets.DEVTO_ORG_ID}}
DEVTO_ORG_NAME: ${{secrets.DEVTO_ORG_NAME}}
run: |
cd ./publish-externals
npm run generate
if [ "$GITHUB_REF" == "refs/heads/develop" ]; then
npx --yes tsx ./src/index.ts --publish "$ADDED_FILES"
npx --yes tsx ./src/index.ts --publish
else
npx --yes tsx ./src/index.ts "$ADDED_FILES"
npx --yes tsx ./src/index.ts
fi
- name: Commit and push changes (on develop)
Expand Down
30 changes: 14 additions & 16 deletions publish-externals/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ const ExternalPublications: ExternalPublication[] = [
const main = async () => {
const args = process.argv.slice(2)
const hasPublishFlag = args.includes("--publish")
const addedFilesArg = args.filter((arg) => arg !== "--publish")[0]
const addedFiles = addedFilesArg ? addedFilesArg.split(" ") : []
const currentFiles = fs.readdirSync(path.join(__dirname, "../../blog/"))
const blogs: {[key: string]: Blog} = snapshot.blogs || {}
let toPublish = 0

try {
for (let publication of ExternalPublications) {
for (const file of addedFiles) {
for (let file of currentFiles) {
file = `blog/${file}`
const {frontMatter} = extractFrontMatterAndContent(path.join(__dirname, "../../", file))
const slug = frontMatter.slug
if (file.startsWith("blog/")) {
if (!blogs[slug]) {
console.log("Publishing new blog", slug)
toPublish++
hasPublishFlag && (await publish(file, blogs, publication))
Expand All @@ -54,19 +54,17 @@ const main = async () => {

for (const slug in blogs) {
const file = blogs[slug].file
if (!addedFiles.includes(file)) {
const {content} = extractFrontMatterAndContent(path.join(__dirname, "../../", file))
const contentHash = createHash("sha256").update(content).digest("hex")
const {content} = extractFrontMatterAndContent(path.join(__dirname, "../../", file))
const contentHash = createHash("sha256").update(content).digest("hex")

if (!blogs[slug].hash) {
console.log("Publishing new blog from snapshot", slug)
toPublish++
hasPublishFlag && (await publish(file, blogs, publication))
} else if (blogs[slug].hash !== contentHash) {
console.log("Publishing updated blog", slug)
toPublish++
hasPublishFlag && (await publish(file, blogs, publication))
}
if (!blogs[slug].hash) {
console.log("Publishing new blog from snapshot", slug)
toPublish++
hasPublishFlag && (await publish(file, blogs, publication))
} else if (blogs[slug].hash !== contentHash) {
console.log("Publishing updated blog", slug)
toPublish++
hasPublishFlag && (await publish(file, blogs, publication))
}
}
}
Expand Down

0 comments on commit ede5178

Please sign in to comment.