]> sigrok.org Git - sigrok-cli.git/commitdiff
build.yml: Add PR status check handling
authorSoeren Apel <redacted>
Thu, 22 Aug 2024 21:16:00 +0000 (23:16 +0200)
committerSoeren Apel <redacted>
Thu, 22 Aug 2024 21:16:00 +0000 (23:16 +0200)
.github/workflows/build.yml

index dab50ad843810478b651ecacb98ad5abdad3b430..7882e18ffade2994568677e09f6d137820a1572e 100644 (file)
@@ -1,9 +1,127 @@
 name: sigrok-cli Artifact Builder Workflow
 
 on:
-  push:
-  workflow_dispatch:
+  push:                # When the repo itself has a new commit on the master branch
+    branches:
+      - master
+    tags:              # When a new tag was created
+  workflow_dispatch:   # When the workflow was started manually from github
+  pull_request:
+    types:
+      - labeled        # When a pull request received a new label (e.g. "provide_binaries")
+      - synchronize    # When a pull request's branch was updated with a new commit
 
 jobs:
+  create-pending-status:
+    name: Create pending status
+    # Run this only for pull requests
+    if: (github.event == pull_request)
+
+    steps:
+    - name: Get PR hash
+      id: sha
+      uses: actions/github-script
+      with:
+        result-encoding: string
+        script: |
+          const { owner, repo, number } = context.issue;
+          const pr = await github.rest.pulls.get({
+            owner,
+            repo,
+            pull_number: number,
+          });
+          return pr.data.head.sha
+
+    - name: Create pending status check
+      uses: actions/github-script
+      with:
+        script: |
+          github.rest.repos.createCommitStatus({
+            owner: context.repo.owner,
+            repo: context.repo.repo,
+            sha: "${{ steps.sha.outputs.result }}",
+            state: "pending",
+            target_url: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
+            description: "Running",
+            context: "pr build"
+          });
+
+  update-pending-status-success:
+    name: Update pending status as 'success'
+    needs: call-build-workflow
+    if: |
+      always() &&    # Workaround, see https://github.com/actions/runner/issues/491#issuecomment-850884422
+      (github.event == pull_request) &&
+      !contains(needs.*.result, 'cancelled') &&
+      !contains(needs.*.result, 'failure')
+
+    steps:
+      - name: Get PR hash
+        id: sha
+        uses: actions/github-script
+        with:
+          result-encoding: string
+          script: |
+            const { owner, repo, number } = context.issue;
+            const pr = await github.rest.pulls.get({
+              owner,
+              repo,
+              pull_number: number,
+            });
+            return pr.data.head.sha
+
+      - name: Update status
+        uses: actions/github-script
+        with:
+          script: |
+            github.rest.repos.createCommitStatus({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              sha: "${{ steps.sha.outputs.result }}",
+              state: "success",
+              target_url: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
+              description: "Success",
+              context: "pr build"
+            });
+
+  update-pending-status-failure:
+    name: Update pending status as 'failure'
+    needs: call-build-workflow
+    if: |
+      always() &&    # Workaround, see https://github.com/actions/runner/issues/491#issuecomment-850884422
+      (github.event == pull_request) &&
+      (contains(needs.*.result, 'cancelled') || contains(needs.*.result, 'failure'))
+
+    steps:
+      - name: Get PR hash
+        id: sha
+        uses: actions/github-script
+        with:
+          result-encoding: string
+          script: |
+            const { owner, repo, number } = context.issue;
+            const pr = await github.rest.pulls.get({
+              owner,
+              repo,
+              pull_number: number,
+            });
+            return pr.data.head.sha
+
+      - name: Update status
+        uses: actions/github-script
+        with:
+          script: |
+            github.rest.repos.createCommitStatus({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              sha: "${{ steps.sha.outputs.result }}",
+              state: "failure",
+              target_url: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
+              description: "Failure",
+              context: "pr build"
+            });
+
   call-build-workflow:
+    name: Sigrok artifact builder
+    id: build
     uses: sigrokproject/sigrok-build/.github/workflows/build.yml@master