This is an automated email from the ASF dual-hosted git repository.

zregvart pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-website.git


The following commit(s) were added to refs/heads/main by this push:
     new adbd48b  chore(github): refactor GitHub workflow
adbd48b is described below

commit adbd48bdfdddbf0b4626a29734f27e514c456e2b
Author: Zoran Regvart <[email protected]>
AuthorDate: Wed Apr 28 09:57:42 2021 +0200

    chore(github): refactor GitHub workflow
    
    Try not to rely on `pull_request_target` event, instead refactor into
    two workflows triggered by `pull_request` and `workflow_run` events.
---
 .github/workflows/pr.yaml                   | 76 ++++++--------------------
 .github/workflows/{pr.yaml => preview.yaml} | 85 +++++++++++------------------
 2 files changed, 50 insertions(+), 111 deletions(-)

diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml
index b64061d..383fe13 100644
--- a/.github/workflows/pr.yaml
+++ b/.github/workflows/pr.yaml
@@ -15,76 +15,34 @@
 # limitations under the License.
 #
 
-name: Pull request checks and preview
+name: Pull request checks
 
-on: pull_request_target
+on: pull_request
 
 env:
   CAMEL_ENV: production
 
 jobs:
-  check-cache:
+  checks:
     runs-on: ubuntu-latest
     steps:
-      - uses: actions/checkout@v2
-        with:
-          persist-credentials: false
-      - name: Run check
+      - name: Checkout
+        uses: actions/checkout@v2
+      - name: Check cache
         run: yarn workspaces foreach install --check-cache
-  build:
-    needs: check-cache
-    runs-on: ubuntu-latest
-    steps:
-      - uses: actions/checkout@v2
-        with:
-          ref: ${{ github.event.pull_request.head.sha }}
-          persist-credentials: false
       - name: Build
         run: yarn build-all
-      - uses: actions/upload-artifact@v2
-        with:
-          name: website
-          path: public
-  preview:
-    needs: build
-    runs-on: ubuntu-latest
-    steps:
-      - uses: actions/checkout@v2
-        with:
-          persist-credentials: false
-      - uses: actions/download-artifact@v2
-        with:
-          name: website
-          path: public
-      - name: Preview on Netlify
+      - name: Run checks
+        run: yarn checks
+      - name: Store pull request data
         run: |
-          DEPLOY_URL=$(yarn preview:netlify 
--alias="pr-${PULL_REQUEST_NUMBER}" --message="Preview for ${PULL_REQUEST_URL}" 
--json 2> /dev/null |jq -r .deploy_url)
-          echo "DEPLOY_URL=${DEPLOY_URL}" >> $GITHUB_ENV
-        env:
-          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
-          NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
-          PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
-          PULL_REQUEST_URL: ${{ github.event.pull_request.html_url }}
-      - uses: actions/github-script@v3
-        with:
-          github-token: ${{ secrets.GITHUB_TOKEN }}
-          script: |
-            github.issues.createComment({
-              issue_number: context.issue.number,
-              owner: context.repo.owner,
-              repo: context.repo.repo,
-              body: `🚀 Preview for ${context.payload.pull_request.head.sha} is 
available at ${process.env.DEPLOY_URL}`
-            })
-  checks:
-    needs: build
-    runs-on: ubuntu-latest
-    steps:
-      - uses: actions/checkout@v2
-        with:
-          persist-credentials: false
-      - uses: actions/download-artifact@v2
+          mkdir pull_request
+          echo ${{ github.event.pull_request.number }} > ./pull_request/number
+          echo ${{ github.event.pull_request.html_url }} > 
./pull_request/html_url
+      - name: Upload artifacts
+        uses: actions/upload-artifact@v2
         with:
           name: website
-          path: public
-      - name: Checks
-        run: yarn checks
+          path: |
+            public
+            pull_request
diff --git a/.github/workflows/pr.yaml b/.github/workflows/preview.yaml
similarity index 50%
copy from .github/workflows/pr.yaml
copy to .github/workflows/preview.yaml
index b64061d..d86ff56 100644
--- a/.github/workflows/pr.yaml
+++ b/.github/workflows/preview.yaml
@@ -15,56 +15,50 @@
 # limitations under the License.
 #
 
-name: Pull request checks and preview
+name: Publish preview
 
-on: pull_request_target
-
-env:
-  CAMEL_ENV: production
+on:
+  workflow_run:
+    workflows: ["Pull request checks"]
+    types:
+      - completed
 
 jobs:
-  check-cache:
-    runs-on: ubuntu-latest
-    steps:
-      - uses: actions/checkout@v2
-        with:
-          persist-credentials: false
-      - name: Run check
-        run: yarn workspaces foreach install --check-cache
-  build:
-    needs: check-cache
-    runs-on: ubuntu-latest
-    steps:
-      - uses: actions/checkout@v2
-        with:
-          ref: ${{ github.event.pull_request.head.sha }}
-          persist-credentials: false
-      - name: Build
-        run: yarn build-all
-      - uses: actions/upload-artifact@v2
-        with:
-          name: website
-          path: public
   preview:
-    needs: build
     runs-on: ubuntu-latest
+    if: github.event.workflow_run.event == 'pull_request' && 
github.event.workflow_run.conclusion == 'success'
     steps:
-      - uses: actions/checkout@v2
-        with:
-          persist-credentials: false
-      - uses: actions/download-artifact@v2
+      - name: Download pull request artifact
+        uses: actions/github-script@v4
         with:
-          name: website
-          path: public
+          script: |
+            const artifacts = await github.actions.listWorkflowRunArtifacts({
+               owner: context.repo.owner,
+               repo: context.repo.repo,
+               run_id: ${{ github.event.workflow_run.id }},
+            });
+            const websiteArtifact = artifacts.data.artifacts.filter((artifact) 
=> {
+              return artifact.name == 'website'
+            })[0];
+            var download = await github.actions.downloadArtifact({
+               owner: context.repo.owner,
+               repo: context.repo.repo,
+               artifact_id: websiteArtifact.id,
+               archive_format: 'zip',
+            });
+            const fs = require('fs');
+            fs.writeFileSync('${{github.workspace}}/website.zip', 
Buffer.from(download.data));
+      - name: Unzip website artifact
+        run: unzip website.zip
+      - name: Checkout
+        uses: actions/checkout@v2
       - name: Preview on Netlify
-        run: |
-          DEPLOY_URL=$(yarn preview:netlify 
--alias="pr-${PULL_REQUEST_NUMBER}" --message="Preview for ${PULL_REQUEST_URL}" 
--json 2> /dev/null |jq -r .deploy_url)
-          echo "DEPLOY_URL=${DEPLOY_URL}" >> $GITHUB_ENV
         env:
           NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
           NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
-          PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
-          PULL_REQUEST_URL: ${{ github.event.pull_request.html_url }}
+        run: |
+          DEPLOY_URL=$(yarn preview:netlify --alias="pr-$(cat 
pull_request/number)" --message="Preview for $(cat pull_request/html_url)" 
--json 2> /dev/null |jq -r .deploy_url)
+          echo "DEPLOY_URL=${DEPLOY_URL}" >> $GITHUB_ENV
       - uses: actions/github-script@v3
         with:
           github-token: ${{ secrets.GITHUB_TOKEN }}
@@ -75,16 +69,3 @@ jobs:
               repo: context.repo.repo,
               body: `🚀 Preview for ${context.payload.pull_request.head.sha} is 
available at ${process.env.DEPLOY_URL}`
             })
-  checks:
-    needs: build
-    runs-on: ubuntu-latest
-    steps:
-      - uses: actions/checkout@v2
-        with:
-          persist-credentials: false
-      - uses: actions/download-artifact@v2
-        with:
-          name: website
-          path: public
-      - name: Checks
-        run: yarn checks

Reply via email to