From 0639c7e7eca4fdffb183f89b7f2e51e65f76645c Mon Sep 17 00:00:00 2001 From: John Maguire Date: Tue, 23 Jul 2024 15:29:34 -0400 Subject: [PATCH] =?UTF-8?q?add=20conditional=20skip=20and=20skip=20running?= =?UTF-8?q?=20tests=20and=20build=20if=20only=20non-cod=E2=80=A6=20(#4204)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add conditional skip and skip running tests and build if only non-code files are changed * make check_skip_ci file executable --- .github/scripts/check_skip_ci.sh | 64 +++++++++++++++++++ .github/scripts/skip_check_ci.sh | 0 .github/workflows/pr.yml | 36 ++++++++++- .../workflows/reusable-conditional-skip.yml | 24 +++++++ 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100755 .github/scripts/check_skip_ci.sh create mode 100644 .github/scripts/skip_check_ci.sh create mode 100644 .github/workflows/reusable-conditional-skip.yml diff --git a/.github/scripts/check_skip_ci.sh b/.github/scripts/check_skip_ci.sh new file mode 100755 index 0000000000..b8785f5541 --- /dev/null +++ b/.github/scripts/check_skip_ci.sh @@ -0,0 +1,64 @@ +#!/bin/bash +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +set -euo pipefail + +# first argument is the list, second is the item to check +function contains() { + list=($1) + for item in "${list[@]}"; do + if [ "$item" == "$2" ]; then + return 0 + fi + done + return 1 +} + +# Get the list of changed files +# Using `git merge-base` ensures that we're always comparing against the correct branch point. +#For example, given the commits: +# +# A---B---C---D---W---X---Y---Z # origin/main +# \---E---F # feature/branch +# +# ... `git merge-base origin/$SKIP_CHECK_BRANCH HEAD` would return commit `D` +# `...HEAD` specifies from the common ancestor to the latest commit on the current branch (HEAD).. +files_to_check=$(git diff --name-only "$(git merge-base origin/$SKIP_CHECK_BRANCH HEAD~)"...HEAD) + +# Define the directories to check +skipped_directories=("assets" ".changelog/", "version") + +files_to_skip=("LICENSE" ".copywrite.hcl" ".gitignore") + +# Loop through the changed files and find directories/files outside the skipped ones +files_to_check_array=($files_to_check) +for file_to_check in "${files_to_check_array[@]}"; do + file_is_skipped=false + echo "checking file: $file_to_check" + + # Allow changes to: + # - This script + # - Files in the skipped directories + # - Markdown files + for dir in "${skipped_directories[@]}"; do + if [[ "$file_to_check" == */check_skip_ci.sh ]] || + [[ "$file_to_check" == "$dir"* ]] || + [[ "$file_to_check" == *.md ]] || + contains "${files_to_skip[*]}" "$file_to_check"; then + file_is_skipped=true + break + fi + done + + if [ "$file_is_skipped" != "true" ]; then + echo -e "non-skippable file changed: $file_to_check" + SKIP_CI=false + echo "Changes detected in non-documentation files - will not skip tests and build" + echo "skip-ci=false" >>"$GITHUB_OUTPUT" + exit 0 ## if file is outside of the skipped_directory exit script + fi +done + +echo "Changes detected in only documentation files - skipping tests and build" +echo "skip-ci=true" >>"$GITHUB_OUTPUT" diff --git a/.github/scripts/skip_check_ci.sh b/.github/scripts/skip_check_ci.sh new file mode 100644 index 0000000000..e69de29bb2 diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 87507c2b7c..d0d72cb6b3 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -10,8 +10,13 @@ env: SHA: ${{ github.event.pull_request.head.sha || github.sha }} jobs: + conditional-skip: + uses: ./.github/workflows/reusable-conditional-skip.yml + test: name: test + needs: [ conditional-skip ] + if: needs.conditional-skip.outputs.skip-ci != 'true' runs-on: ubuntu-latest steps: - uses: benc-uk/workflow-dispatch@25b02cc069be46d637e8fe2f1e8484008e9e9609 # v1.2.3 @@ -21,4 +26,33 @@ jobs: repo: hashicorp/consul-k8s-workflows ref: main token: ${{ secrets.ELEVATED_GITHUB_TOKEN }} - inputs: '{ "test-ce": false, "context":"${{ env.CONTEXT }}", "actor":"${{ github.actor }}", "repository":"${{ github.repository }}", "branch":"${{ env.BRANCH }}", "sha":"${{ env.SHA }}", "token":"${{ secrets.ELEVATED_GITHUB_TOKEN }}" }' + inputs: '{ "context":"${{ env.CONTEXT }}", "actor":"${{ github.actor }}", "repository":"${{ github.repository }}", "branch":"${{ env.BRANCH }}", "sha":"${{ env.SHA }}", "token":"${{ secrets.ELEVATED_GITHUB_TOKEN }}" }' + + pass-required-checks-on-skip: + needs: [ conditional-skip ] + if: needs.conditional-skip.outputs.skip-ci == 'true' + runs-on: ubuntu-latest + strategy: + matrix: + include: + # The required checks that should be "passed" when the CI is skipped + - check-name: acceptance + - check-name: acceptance-cni + - check-name: acceptance-tproxy + - check-name: Unit test helm templates + - check-name: Unit test enterprise control plane + - check-name: Unit test control plane + - check-name: Unit test cli + - check-name: Unit test acceptance + steps: + - name: Update final status + uses: docker://ghcr.io/curtbushko/commit-status-action:e1d661c757934ab35c74210b4b70c44099ec747a + env: + INPUT_TOKEN: ${{ secrets.ELEVATED_GITHUB_TOKEN }} + INPUT_REPOSITORY: ${{ github.repository }} + INPUT_CONTEXT: ${{ matrix.check-name }} + INPUT_STATE: success + INPUT_DESCRIPTION: "Skipped due to conditional-skip check" + INPUT_SHA: ${{ env.SHA }} + INPUT_DETAILS_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} + INPUT_OWNER: "hashicorp" diff --git a/.github/workflows/reusable-conditional-skip.yml b/.github/workflows/reusable-conditional-skip.yml new file mode 100644 index 0000000000..ecba895152 --- /dev/null +++ b/.github/workflows/reusable-conditional-skip.yml @@ -0,0 +1,24 @@ +name: conditional-skip + +on: + workflow_call: + outputs: + skip-ci: + description: "Whether we should skip build and test jobs" + value: ${{ jobs.check-skip.outputs.skip-ci }} + +jobs: + check-skip: + runs-on: ubuntu-latest + name: Check whether to skip build and tests + outputs: + skip-ci: ${{ steps.check-changed-files.outputs.skip-ci }} + env: + SKIP_CHECK_BRANCH: ${{ github.head_ref || github.ref_name }} + steps: + - uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4 + with: + fetch-depth: 0 + - name: Check changed files + id: check-changed-files + run: ./.github/scripts/check_skip_ci.sh