From bfbfb693da63002f2b2ec13a13ff92bacf7bcb18 Mon Sep 17 00:00:00 2001
From: brown <1173781832@qq.com>
Date: Fri, 26 Aug 2022 13:26:19 +0800
Subject: [PATCH] CI: optimize ut_pr and auto cancel outdate workflows (#4723)

In this pr, I optimized ut_pr and add auto cancel outdate workflows. For the function of automatic cancellation, please refer to this link: https://docs.github.com/en/actions/using-jobs/using-concurrency

Approved by: @sukki37
---
 .github/workflows/ci.yml      | 20 +++++++++++---------
 .github/workflows/labeler.yml |  4 ++++
 .github/workflows/ut_pr.yml   | 35 +++++++++++++++++++----------------
 3 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index dfc679732..1461f383d 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -8,6 +8,10 @@ on:
 
   workflow_dispatch:
 
+concurrency: 
+  group: ${{ github.event.pull_request.head.repo.full_name}}/${{ github.event.pull_request.head.ref }}/${{ github.workflow }}
+  cancel-in-progress: true
+
 jobs:
 
   ut-linux-x86:
@@ -28,7 +32,7 @@ jobs:
           make ut
 
       - uses: actions/upload-artifact@v2
-        if: ${{ always() }}
+        if: ${{ always() && !cancelled() }}
         continue-on-error: true
         with:
           name: ut_linux_x86_reports
@@ -54,7 +58,7 @@ jobs:
           make ut
 
       - uses: actions/upload-artifact@v2
-        if: ${{ always() }}
+        if: ${{ always() && !cancelled() }}
         continue-on-error: true
         with:
           name: ut_darwin_x86_reports
@@ -80,7 +84,7 @@ jobs:
           make ut
 
       - uses: actions/upload-artifact@v2
-        if: ${{ always() }}
+        if: ${{ always() && !cancelled() }}
         continue-on-error: true
         with:
           name: ut_linux_arm_reports
@@ -152,7 +156,7 @@ jobs:
           rm -rf ./mo-tester/lib
 
       - uses: actions/upload-artifact@v2
-        if: ${{ always() }}
+        if: ${{ always() && !cancelled() }}
         continue-on-error: true
         with:
           name: linux_x86_ci_reports
@@ -224,7 +228,7 @@ jobs:
           rm -rf ./mo-tester/lib
 
       - uses: actions/upload-artifact@v2
-        if: ${{ always() }}
+        if: ${{ always() && !cancelled() }}
         continue-on-error: true
         with:
           name: ci_linux_arm_reports
@@ -294,7 +298,7 @@ jobs:
           rm -rf ./mo-tester/lib
 
       - uses: actions/upload-artifact@v2
-        if: ${{ always() }}
+        if: ${{ always() && !cancelled() }}
         continue-on-error: true
         with:
           name: ci_darwin_x86_reports
@@ -329,6 +333,4 @@ jobs:
         run: |
           cd $GITHUB_WORKSPACE
           make static-check
-          
-      
-          
+          
\ No newline at end of file
diff --git a/.github/workflows/labeler.yml b/.github/workflows/labeler.yml
index 8219df5a4..ba976cb4d 100644
--- a/.github/workflows/labeler.yml
+++ b/.github/workflows/labeler.yml
@@ -4,6 +4,10 @@ on:
     types: [ opened, synchronize, reopened ]
     branches: main
 
+concurrency: 
+  group: ${{ github.event.pull_request.head.repo.full_name}}/${{ github.event.pull_request.head.ref }}/${{ github.workflow }}
+  cancel-in-progress: true
+
 jobs:
   pr-size-label:
     runs-on: ubuntu-latest
diff --git a/.github/workflows/ut_pr.yml b/.github/workflows/ut_pr.yml
index 3c72cc1d3..47b71532c 100644
--- a/.github/workflows/ut_pr.yml
+++ b/.github/workflows/ut_pr.yml
@@ -1,44 +1,47 @@
 name: PR Unit Test Coverage
 on:
   pull_request_target:
-    types: [ opened, synchronize,reopened ]
+    types: [ opened, synchronize ]
+
+concurrency: 
+  group: ${{ github.event.pull_request.head.repo.full_name}}/${{ github.event.pull_request.head.ref }}/${{ github.workflow }}
+  cancel-in-progress: true
 
 jobs:
-  test-name:
+  ut_coverage:
     runs-on: ubuntu-latest
     name: PR Unit Test Coverage
 
     steps:
-      - name: Get PR Changed Paths
-        uses: guguducken/change-paths-action@v0.0.5
-        id: ut-pr
-        with:
-          github-token: ${{ secrets.TOKEN_ACTION }}
-          ignore: "/,etc/,optools/,LICENSES/,cgo/,cmd/,docs/,proto/,test/,.github/"
-
       - uses: actions/checkout@v2
-        if: ${{ steps.ut-pr.outputs.paths != '' }}
         with:
           fetch-depth: '3'
           path: ./matrixone
-          repository: ${{ steps.ut-pr.outputs.resource }}
-          ref: ${{ steps.ut-pr.outputs.branch }}
+          repository: ${{ github.event.pull_request.head.repo.full_name }}
+          ref: ${{ github.event.pull_request.head.ref }}
 
       - name: Set Variables
-        if: ${{ steps.ut-pr.outputs.paths != '' }}
         run: |
           echo "raw_ut_coverage='ut_coverage.raw'" >> $GITHUB_ENV
           echo "ut_report='UT-Report.out'" >> $GITHUB_ENV
 
       - name: Set up Go
-        if: ${{ steps.ut-pr.outputs.paths != '' }}
         uses: ./matrixone/.github/actions/setup_env
 
       - name: Build MatrixOne
-        if: ${{ steps.ut-pr.outputs.paths != '' }}
         run: |
           cd $GITHUB_WORKSPACE/matrixone && make clean && make config && make build
-
+          echo "test_scope=$(go list ./... | grep -v 'driver\|engine/aoe\|engine/memEngine\|pkg/catalog' | xargs)" >> $GITHUB_ENV
+          echo ${{ env.test_scope }}
+          
+      - name: Get Changed Paths
+        uses: guguducken/change-paths-action@v0.0.6
+        id: ut-pr 
+        with:
+          github-token: ${{ secrets.TOKEN_ACTION }}
+          ignore: "/,etc/,optools/,LICENSES/,cgo/,cmd/,docs/,proto/,test/,.github/"
+          source-path: ${{ env.test_scope }}
+      
       - name: PR Unit Test
         if: ${{ steps.ut-pr.outputs.paths != '' }}
         timeout-minutes: 60
-- 
GitLab