Skip to content

Commit 4910245

Browse files
authored
ci: add conditional skip for docs changes (#4127)
1 parent 5e9594d commit 4910245

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

.github/scripts/check_skip_ci.sh

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
# Copyright (c) HashiCorp, Inc.
3+
# SPDX-License-Identifier: MPL-2.0
4+
5+
set -euo pipefail
6+
7+
# Get the list of changed files
8+
# Using `git merge-base` ensures that we're always comparing against the correct branch point.
9+
#For example, given the commits:
10+
#
11+
# A---B---C---D---W---X---Y---Z # origin/main
12+
# \---E---F # feature/branch
13+
#
14+
# ... `git merge-base origin/$SKIP_CHECK_BRANCH HEAD` would return commit `D`
15+
# `...HEAD` specifies from the common ancestor to the latest commit on the current branch (HEAD)..
16+
files_to_check=$(git diff --name-only "$(git merge-base origin/$SKIP_CHECK_BRANCH HEAD~)"...HEAD)
17+
18+
# Define the directories to check
19+
skipped_directories=("assets" ".changelog/")
20+
21+
# Loop through the changed files and find directories/files outside the skipped ones
22+
files_to_check_array=($files_to_check)
23+
for file_to_check in "${files_to_check_array[@]}"; do
24+
file_is_skipped=false
25+
echo "checking file: $file_to_check"
26+
27+
# Allow changes to:
28+
# - This script
29+
# - Files in the skipped directories
30+
# - Markdown files
31+
for dir in "${skipped_directories[@]}"; do
32+
if [[ "$file_to_check" == */check_skip_ci.sh ]] ||
33+
[[ "$file_to_check" == "$dir"* ]] ||
34+
[[ "$file_to_check" == *.md ]]; then
35+
file_is_skipped=true
36+
break
37+
fi
38+
done
39+
40+
if [ "$file_is_skipped" != "true" ]; then
41+
echo -e "non-skippable file changed: $file_to_check"
42+
SKIP_CI=false
43+
echo "Changes detected in non-documentation files - will not skip tests and build"
44+
echo "skip-ci=false" >> "$GITHUB_OUTPUT"
45+
exit 0 ## if file is outside of the skipped_directory exit script
46+
fi
47+
done
48+
49+
echo "Changes detected in only documentation files - skipping tests and build"
50+
echo "skip-ci=true" >> "$GITHUB_OUTPUT"

.github/workflows/build.yml

+9
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,19 @@ env:
1717
PKG_NAME: "consul-k8s"
1818

1919
jobs:
20+
conditional-skip:
21+
uses: ./.github/workflows/reusable-conditional-skip.yml
22+
2023
get-go-version:
24+
# Cascades down to test jobs
25+
needs: [ conditional-skip ]
26+
if: needs.conditional-skip.outputs.skip-ci != 'true'
2127
uses: ./.github/workflows/reusable-get-go-version.yml
2228

2329
get-product-version:
30+
# Cascades down to test jobs
31+
needs: [ conditional-skip ]
32+
if: needs.conditional-skip.outputs.skip-ci != 'true'
2433
runs-on: ubuntu-latest
2534
outputs:
2635
product-version: ${{ steps.get-product-version.outputs.product-version }}

.github/workflows/pr.yml

+5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ env:
1010
SHA: ${{ github.event.pull_request.head.sha || github.sha }}
1111

1212
jobs:
13+
conditional-skip:
14+
uses: ./.github/workflows/reusable-conditional-skip.yml
15+
1316
test:
1417
name: test
18+
needs: [ conditional-skip ]
19+
if: needs.conditional-skip.outputs.skip-ci != 'true'
1520
runs-on: ubuntu-latest
1621
steps:
1722
- uses: benc-uk/workflow-dispatch@25b02cc069be46d637e8fe2f1e8484008e9e9609 # v1.2.3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: conditional-skip
2+
3+
on:
4+
workflow_call:
5+
outputs:
6+
skip-ci:
7+
description: "Whether we should skip build and test jobs"
8+
value: ${{ jobs.check-skip.outputs.skip-ci }}
9+
10+
jobs:
11+
check-skip:
12+
runs-on: ubuntu-latest
13+
name: Check whether to skip build and tests
14+
outputs:
15+
skip-ci: ${{ steps.check-changed-files.outputs.skip-ci }}
16+
env:
17+
SKIP_CHECK_BRANCH: ${{ github.head_ref || github.ref_name }}
18+
steps:
19+
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
20+
with:
21+
fetch-depth: 0
22+
- name: Check changed files
23+
id: check-changed-files
24+
run: ./.github/scripts/check_skip_ci.sh

.github/workflows/security-scan.yml

+6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ concurrency:
1616
cancel-in-progress: true
1717

1818
jobs:
19+
conditional-skip:
20+
uses: ./.github/workflows/reusable-conditional-skip.yml
21+
1922
get-go-version:
23+
# Cascades down to test jobs
24+
needs: [ conditional-skip ]
25+
if: needs.conditional-skip.outputs.skip-ci != 'true'
2026
uses: ./.github/workflows/reusable-get-go-version.yml
2127

2228
scan:

0 commit comments

Comments
 (0)