From d0386955c9bd29686abc33c060cb130eec24dfb7 Mon Sep 17 00:00:00 2001
From: "kirill.chalov" <kirill.chalov@espressif.com>
Date: Tue, 10 Dec 2024 16:15:21 +0800
Subject: [PATCH] feat: add separate workflows to build and deploy preview pr

---
 .github/workflows/pr-build-preview.yml  | 41 ++++++++++++++++
 .github/workflows/pr-deploy-preview.yml | 62 ++++++++-----------------
 2 files changed, 60 insertions(+), 43 deletions(-)
 create mode 100644 .github/workflows/pr-build-preview.yml

diff --git a/.github/workflows/pr-build-preview.yml b/.github/workflows/pr-build-preview.yml
new file mode 100644
index 000000000..3869ce5a7
--- /dev/null
+++ b/.github/workflows/pr-build-preview.yml
@@ -0,0 +1,41 @@
+name: Build preview for PR
+
+on:
+  pull_request:
+    types: [opened, synchronize, reopened]
+
+permissions:
+  contents: read
+
+jobs:
+  build-preview:
+    runs-on: ubuntu-latest
+
+    steps:
+      - name: Install Hugo CLI
+        env:
+          HUGO_VERSION: 0.135.0
+        run: |
+          wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
+          && sudo dpkg -i ${{ runner.temp }}/hugo.deb
+
+      - name: Check out repo
+        uses: actions/checkout@v4
+        with:
+          submodules: recursive
+
+      - name: Build website with Hugo
+        env:
+          # For maximum backward compatibility with Hugo modules
+          HUGO_ENVIRONMENT: preview
+          HUGO_ENV: preview
+        run: |
+          hugo \
+            --gc \
+            --minify
+
+      - name: Upload artifacts
+        uses: actions/upload-artifact@v3
+        with:
+          name: public-folder
+          path: ./public
diff --git a/.github/workflows/pr-deploy-preview.yml b/.github/workflows/pr-deploy-preview.yml
index d3f9f550b..59c60a914 100644
--- a/.github/workflows/pr-deploy-preview.yml
+++ b/.github/workflows/pr-deploy-preview.yml
@@ -1,58 +1,35 @@
 name: Deploy preview for PR
 
 on:
-  pull_request:
-    types: [opened, synchronize, reopened]
-  # Allow manual runs from the Actions tab
-  workflow_dispatch:
+  workflow_run:
+    workflows: ["Build preview for PR"]
+    types:
+      - completed
 
-# Set permissions of the GITHUB_TOKEN
 permissions:
   contents: read
   id-token: write
   pull-requests: write
 
-# Allow only one concurrent deployment
 concurrency:
-  group: ${{ github.workflow }}-${{ github.ref }}
+  group: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch }}
   cancel-in-progress: false
 
-# Define environment variables
 env:
-  HUGO_BASEURL: "https://preview-developer.espressif.com/pr${{ github.event.pull_request.number }}/"
-
-# Default to bash
-defaults:
-  run:
-    shell: bash
+  HUGO_BASEURL: "https://preview-developer.espressif.com/pr${{ github.event.workflow_run.pull_requests[0].number }}/"
 
 jobs:
-  build-and-deploy-preview:
+  deploy-preview:
     runs-on: ubuntu-latest
-
+    if: >
+      github.event.workflow_run.event == 'pull_request' &&
+      github.event.workflow_run.conclusion == 'success'
     steps:
-
-      - name: Install Hugo CLI
-        env:
-          HUGO_VERSION: 0.135.0
-        run: |
-          wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
-          && sudo dpkg -i ${{ runner.temp }}/hugo.deb
-
-      - name: Check out repo
-        uses: actions/checkout@v4
+      - name: Download artifacts
+        uses: actions/download-artifact@v3
         with:
-          submodules: recursive
-
-      - name: Build website with Hugo
-        env:
-          # For maximum backward compatibility with Hugo modules
-          HUGO_ENVIRONMENT: preview
-          HUGO_ENV: preview
-        run: |
-          hugo \
-            --gc \
-            --minify
+          name: public-folder
+          path: ./public
 
       - name: Deploy to AWS S3 PR-specific subdirectory
         uses: jakejarvis/s3-sync-action@master
@@ -61,8 +38,7 @@ jobs:
         env:
           AWS_S3_BUCKET: ${{ secrets.PREVIEW_AWS_BUCKET_NAME }}
           SOURCE_DIR: './public'
-          # Subdirectory for the PR
-          DEST_DIR: "pr${{ github.event.pull_request.number }}"
+          DEST_DIR: "pr${{ github.event.workflow_run.pull_requests[0].number }}"
           AWS_REGION: ${{ secrets.AWS_REGION }}
           AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
           AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
@@ -70,19 +46,19 @@ jobs:
       - name: Post Preview Link to PR
         uses: actions/github-script@v7
         with:
-          github-token: ${{secrets.GITHUB_TOKEN}}
+          github-token: ${{ secrets.GITHUB_TOKEN }}
           script: |
             github.rest.issues.createComment({
-              issue_number: context.issue.number,
+              issue_number: context.event.workflow_run.pull_requests[0].number,
               owner: context.repo.owner,
               repo: context.repo.repo,
-              body: `🎉 A preview of this PR is available at: ${{ env.HUGO_BASEURL }} `
+              body: `🎉 A preview of this PR is available at: ${{ env.HUGO_BASEURL }}`
             })
 
       - name: Invalidate CloudFront cache for PR
         uses: chetan/invalidate-cloudfront-action@v2
         env:
-          PATHS: "/pr-${{ github.event.pull_request.number }}/*"
+          PATHS: "/pr-${{ github.event.workflow_run.pull_requests[0].number }}/*"
           DISTRIBUTION: ${{ secrets.PREVIEW_CLOUDFRONT_DISTRIBUTION }}
           AWS_REGION: ${{ secrets.AWS_REGION }}
           AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}