Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: add conditional skip for docs changes #560

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/scripts/check_skip_ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

set -euo pipefail

# 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=("_doc/" ".changelog/")

# Loop through the changed files and find directories/files outside the skipped ones
files_to_check_array=($files_to_check)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were not converting to an array in the original script - it happened to still work due to multiline matches, but this allows us to print individual debug logs more accurately

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 ]]; then
file_is_skipped=true
break
fi
done

if [ "$file_is_skipped" != "true" ]; then
echo -e "non-skippable file changed: $file_to_check"
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"
12 changes: 11 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ env:
PKG_NAME: "consul-dataplane"

jobs:
conditional-skip:
uses: ./.github/workflows/reusable-conditional-skip.yml

get-go-version:
# Cascades down to test jobs
needs: [conditional-skip]
if: needs.conditional-skip.outputs.skip-ci != 'true'
uses: ./.github/workflows/reusable-get-go-version.yml

get-product-version:
# Cascades down to test jobs
needs: [conditional-skip]
if: needs.conditional-skip.outputs.skip-ci != 'true'
runs-on: ubuntu-latest
outputs:
product-version: ${{ steps.get-product-version.outputs.product-version }}
Expand Down Expand Up @@ -401,9 +410,10 @@ jobs:

integration-tests-success:
needs:
- conditional-skip
- integration-tests
runs-on: ubuntu-latest
if: always()
if: always() && needs.conditional-skip.outputs.skip-ci != 'true'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the action won't get stuck because this job is required and there are cases where we won't run it, correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. This follows the pattern in consul here

steps:
- name: evaluate upstream job results
run: |
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/consul-dataplane-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ on:
pull_request:

jobs:
conditional-skip:
uses: ./.github/workflows/reusable-conditional-skip.yml

get-go-version:
# Cascades down to test jobs
needs: [conditional-skip]
if: needs.conditional-skip.outputs.skip-ci != 'true'
uses: ./.github/workflows/reusable-get-go-version.yml

unit-tests:
Expand Down
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
6 changes: 6 additions & 0 deletions .github/workflows/security-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ concurrency:
cancel-in-progress: true

jobs:
conditional-skip:
uses: ./.github/workflows/reusable-conditional-skip.yml

get-go-version:
# Cascades down to test jobs
needs: [conditional-skip]
if: needs.conditional-skip.outputs.skip-ci != 'true'
uses: ./.github/workflows/reusable-get-go-version.yml

scan:
Expand Down
Loading