Skip to content

Commit

Permalink
add conditional skip and skip running tests and build if only non-cod… (
Browse files Browse the repository at this point in the history
#4204)

* add conditional skip and skip running tests and build if only non-code files are changed

* make check_skip_ci file executable
  • Loading branch information
jm96441n authored Jul 23, 2024
1 parent 6a55305 commit 0639c7e
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
64 changes: 64 additions & 0 deletions .github/scripts/check_skip_ci.sh
Original file line number Diff line number Diff line change
@@ -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"
Empty file.
36 changes: 35 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
24 changes: 24 additions & 0 deletions .github/workflows/reusable-conditional-skip.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0639c7e

Please sign in to comment.