From d581e614131be8a3087aa5426d4741558a8896c2 Mon Sep 17 00:00:00 2001 From: Robinson Zimmermann <16561945+robinsonzimmermann@users.noreply.github.com> Date: Wed, 1 May 2024 11:00:10 +0200 Subject: [PATCH] Update post date on merge to main (#116) * Update post date on merge to main --- .github/workflows/merge.yml | 48 ++++++++++++++++++++++++++++++++++++ .github/workflows/static.yml | 13 ++++++++++ tools/publish/index.js | 15 +++++++++++ 3 files changed, 76 insertions(+) create mode 100644 .github/workflows/merge.yml diff --git a/.github/workflows/merge.yml b/.github/workflows/merge.yml new file mode 100644 index 00000000..a2e406be --- /dev/null +++ b/.github/workflows/merge.yml @@ -0,0 +1,48 @@ +name: Publish article + +on: + push: + branches: ['main'] + +jobs: + check-unpublished: + runs-on: ubuntu-latest + outputs: + should_publish: ${{ steps.should-publish.outputs.should_publish }} + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Should publish + id: should-publish + run: echo "should_publish=$([[ -d "content/posts/unpublished" ]] && echo true || echo false)" >> "$GITHUB_OUTPUT" + + publish: + runs-on: ubuntu-latest + needs: check-unpublished + if: ${{ needs.check-unpublished.outputs.should_publish == 'true' }} + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + token: ${{ secrets.BOSS_TOKEN }} + - uses: actions/setup-node@v3 + with: + node-version: 20 + - name: Move article to current date directory + run: | + npm ci + npm run build:utils + npm run posts:publish + + - name: Commit changes + run: | + git config --local user.email "oss@backbase.com" + git config --local user.name "backbaseoss" + git add content + git commit -m "[BOT] Post published" + + - name: Push changes + uses: ad-m/github-push-action@master + with: + github_token: ${{ secrets.BOSS_TOKEN }} + branch: ${{ github.ref }} diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index c61c6f5e..c5d47bd2 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -22,8 +22,21 @@ concurrency: cancel-in-progress: false jobs: + check-unpublished: + runs-on: ubuntu-latest + outputs: + should_deploy: ${{ steps.should-publish.outputs.should_deploy }} + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Should publish + id: should-publish + run: echo "should_deploy=$([[ -d "content/posts/unpublished" ]] && echo false || echo true)" >> "$GITHUB_OUTPUT" + # Single deploy job since we're just deploying deploy: + needs: check-unpublished + if: ${{ needs.check-unpublished.outputs.should_deploy == 'true' }} environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} diff --git a/tools/publish/index.js b/tools/publish/index.js index 7a81276f..08bd9c26 100644 --- a/tools/publish/index.js +++ b/tools/publish/index.js @@ -69,6 +69,11 @@ async function moveUnpublishedDirectory(sourcePath, destinationRoot) { // Remove the "unpublished" directory fs.rmdirSync(unpublishedPath); + + if (isDirectoryEmpty(sourcePath)) { + fs.rmdirSync(sourcePath); + } + console.log('Unpublished directory removed.'); } else { console.log('No "unpublished" directory found.'); @@ -90,3 +95,13 @@ main(); function loadEsmModule(modulePath) { return new Function('modulePath', `return import(modulePath);`)(modulePath); } + +function isDirectoryEmpty(path) { + let empty = false; + if (fs.existsSync(path)) { + const files = fs.readdirSync(path); + empty = !files?.length + } + return empty; +} +