From da1c07a04f718059570a099863afaf873ba73dfd Mon Sep 17 00:00:00 2001 From: HarshCasper Date: Wed, 28 Feb 2024 10:53:04 +0530 Subject: [PATCH] create a github action workflow for preview deployments --- .github/workflows/preview-build.yml | 50 +++++++++++++++++ .github/workflows/preview-deploy.yml | 80 ++++++++++++++++++++++++++++ .github/workflows/preview-start.yml | 16 ++++++ 3 files changed, 146 insertions(+) create mode 100644 .github/workflows/preview-build.yml create mode 100644 .github/workflows/preview-deploy.yml create mode 100644 .github/workflows/preview-start.yml diff --git a/.github/workflows/preview-build.yml b/.github/workflows/preview-build.yml new file mode 100644 index 0000000..9ae0b06 --- /dev/null +++ b/.github/workflows/preview-build.yml @@ -0,0 +1,50 @@ +name: Preview Build + +on: + pull_request: + types: [opened, synchronize, reopened] + +jobs: + build-preview: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + submodules: recursive + fetch-depth: 0 + + - name: Setup Hugo + uses: peaceiris/actions-hugo@v2 + with: + hugo-version: '0.101.0' + extended: true + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: '16' + + - name: Install and Build + run: | + npm ci + hugo --gc --minify + + - name: Upload dist artifact + uses: actions/upload-artifact@v4 + with: + name: dist + path: ./public + retention-days: 7 + + - name: Save PR number + if: ${{ always() }} + run: echo ${{ github.event.number }} > ./pr-id.txt + + - name: Upload PR number + if: ${{ always() }} + uses: actions/upload-artifact@v4 + with: + name: pr + path: ./pr-id.txt diff --git a/.github/workflows/preview-deploy.yml b/.github/workflows/preview-deploy.yml new file mode 100644 index 0000000..a2b30f1 --- /dev/null +++ b/.github/workflows/preview-deploy.yml @@ -0,0 +1,80 @@ +name: Preview Deploy + +on: + workflow_run: + workflows: ["Preview Build"] + types: + - completed + +jobs: + success: + runs-on: ubuntu-latest + if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' + steps: + - name: download pr artifact + uses: dawidd6/action-download-artifact@v3 + with: + workflow: ${{ github.event.workflow_run.workflow_id }} + name: pr + + - name: Save the PR ID + id: pr + run: echo "::set-output name=id::$( + body-include: '' + number: ${{ steps.pr.outputs.id }} + + - name: Job failure + if: ${{ failure() }} + uses: actions-cool/maintain-one-comment@v3.1.1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + body: | + Deploy PR Preview failed. + + body-include: '' + number: ${{ steps.pr.outputs.id }} + + failed: + runs-on: ubuntu-latest + if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'failure' + steps: + - name: Download PR artifact + uses: dawidd6/action-download-artifact@v3 + with: + workflow: ${{ github.event.workflow_run.workflow_id }} + name: pr + + - name: Save the PR ID + id: pr + run: echo "::set-output name=id::$( + body-include: '' + number: ${{ steps.pr.outputs.id }} diff --git a/.github/workflows/preview-start.yml b/.github/workflows/preview-start.yml new file mode 100644 index 0000000..80a40be --- /dev/null +++ b/.github/workflows/preview-start.yml @@ -0,0 +1,16 @@ +name: Preview Start + +on: pull_request_target + +jobs: + preview: + runs-on: ubuntu-latest + steps: + - name: create + uses: actions-cool/maintain-one-comment@v3.1.1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + body: | + ⚡️ Deploying PR Preview... + + body-include: ''