From 5e90d67b0e84ae414c8edaf28aaeb28b489e8f9e Mon Sep 17 00:00:00 2001 From: Igor Gov Date: Sun, 6 Mar 2022 15:03:43 +0200 Subject: [PATCH] Run PR validation check only when needed & use docker cache during build (#876) * Improve PR validation checks --- .github/workflows/build.yml | 35 ++++++++++++++++++++++---- .github/workflows/test.yml | 23 ++++++++++++++--- devops/check_modified_files.sh | 45 ++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 8 deletions(-) create mode 100755 devops/check_modified_files.sh diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8330524d1..3e2479ba7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -15,15 +15,23 @@ jobs: name: CLI executable build runs-on: ubuntu-latest steps: + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + with: + fetch-depth: 2 + + - name: Check modified files + id: modified_files + run: devops/check_modified_files.sh cli/ + - name: Set up Go 1.17 + if: steps.modified_files.outputs.matched == 'true' uses: actions/setup-go@v2 with: go-version: '1.17' - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - - name: Build CLI + if: steps.modified_files.outputs.matched == 'true' run: make cli build-agent: @@ -32,6 +40,23 @@ jobs: steps: - name: Check out code into the Go module directory uses: actions/checkout@v2 + with: + fetch-depth: 2 + + - name: Check modified files + id: modified_files + run: devops/check_modified_files.sh agent/ shared/ tap/ ui/ Dockerfile - - name: Build Agent - run: make agent-docker + - name: Set up Docker Buildx + if: steps.modified_files.outputs.matched == 'true' + uses: docker/setup-buildx-action@v1 + + - name: Build + uses: docker/build-push-action@v2 + if: steps.modified_files.outputs.matched == 'true' + with: + context: . + push: false + tags: up9inc/mizu:devlatest + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3b7fa5b81..978b3585b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,14 +19,16 @@ jobs: name: Unit Tests runs-on: ubuntu-latest steps: + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + with: + fetch-depth: 2 + - name: Set up Go 1.17 uses: actions/setup-go@v2 with: go-version: '^1.17' - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - - name: Install libpcap shell: bash run: | @@ -40,16 +42,31 @@ jobs: - name: 'Set up Cloud SDK' uses: 'google-github-actions/setup-gcloud@v0' + - name: Check CLI modified files + id: cli_modified_files + run: devops/check_modified_files.sh cli/ + - name: CLI Test + if: github.event_name == 'push' || steps.cli_modified_files.outputs.matched == 'true' run: make test-cli + - name: Check Agent modified files + id: agent_modified_files + run: devops/check_modified_files.sh agent/ + - name: Agent Test + if: github.event_name == 'push' || steps.agent_modified_files.outputs.matched == 'true' run: make test-agent - name: Shared Test run: make test-shared + + - name: Check extensions modified files + id: ext_modified_files + run: devops/check_modified_files.sh tap/extensions/ - name: Extensions Test + if: github.event_name == 'push' || steps.ext_modified_files.outputs.matched == 'true' run: make test-extensions - name: Upload coverage to Codecov diff --git a/devops/check_modified_files.sh b/devops/check_modified_files.sh new file mode 100755 index 000000000..e147b1ed3 --- /dev/null +++ b/devops/check_modified_files.sh @@ -0,0 +1,45 @@ +#!/bin/bash +paths_arr=( "$@" ) + +printf "\n========== List modified files ==========\n" +echo "$(git diff --name-only HEAD^ HEAD)" + +printf "\n========== List paths to match and check existence ==========\n" +for path in ${paths_arr[*]} +do + if [ -f "$path" ] || [ -d "$path" ]; then + echo "$path - found" + else + echo "$path - does not found - exiting with failure" + exit 1 + fi +done + +printf "\n========== Check paths of modified files ==========\n" +git diff --name-only HEAD^ HEAD > files.txt +matched=false +while IFS= read -r file +do + for path in ${paths_arr[*]} + do + if [[ $file == $path* ]]; then + echo "$file - match path: $path" + matched=true + break + fi + done + if [[ $matched == true ]]; then + break + else + echo "$file - does not match any given path" + fi +done < files.txt + +printf "\n========== Result ==========\n" +if [[ $matched = true ]]; then + echo "match found" + echo "::set-output name=matched::true" +else + echo "no match found" + echo "::set-output name=matched::false" +fi \ No newline at end of file