Skip to content

Commit 70f7667

Browse files
[Tooling] Skip jobs if there are no relevant code changes (#24543)
* Skip build or validation jobs depending on the changes * Refactor `should-skip-job` script Updates: 1) receive a `--job-type` argument 2) use a case instead of if/else 3) use `buildkite-agent step cancel` and exit with 15 when the script isn't called correctly * Update to DRY the message / Buildkite annotation when skipping builds * Add skip check in unit tests jobs * Add skip test to demo build * Fix version pattern * Add metadata pattern * Improve script paths and add constants for job types in should-skip-job.sh * Update job type order in comment Co-authored-by: Olivier Halligon <[email protected]> --------- Co-authored-by: Olivier Halligon <[email protected]>
1 parent 4cbd62c commit 70f7667

10 files changed

+141
-3
lines changed

.buildkite/commands/build-for-testing.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#!/bin/bash -eu
2+
3+
if "$(dirname "${BASH_SOURCE[0]}")/should-skip-job.sh" --job-type build; then
4+
exit 0
5+
fi
6+
27
APP=${1:-}
38

49
# Run this at the start to fail early if value not available

.buildkite/commands/lint-localized-strings-format.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/bash -eu
22

3+
if "$(dirname "${BASH_SOURCE[0]}")/should-skip-job.sh" --job-type localization; then
4+
exit 0
5+
fi
6+
37
echo "--- :writing_hand: Copy Files"
48
mkdir -pv ~/.configure/wordpress-ios/secrets
59
cp -v fastlane/env/project.env-example ~/.configure/wordpress-ios/secrets/project.env

.buildkite/commands/prototype-build-jetpack.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/bash -eu
22

3+
if "$(dirname "${BASH_SOURCE[0]}")/should-skip-job.sh" --job-type build; then
4+
exit 0
5+
fi
6+
37
"$(dirname "${BASH_SOURCE[0]}")/shared-set-up.sh"
48
"$(dirname "${BASH_SOURCE[0]}")/shared-set-up-distribution.sh"
59

.buildkite/commands/prototype-build-wordpress.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/bash -eu
22

3+
if "$(dirname "${BASH_SOURCE[0]}")/should-skip-job.sh" --job-type build; then
4+
exit 0
5+
fi
6+
37
"$(dirname "${BASH_SOURCE[0]}")/shared-set-up.sh"
48
"$(dirname "${BASH_SOURCE[0]}")/shared-set-up-distribution.sh"
59

.buildkite/commands/run-ui-tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/bash -eu
22

3+
if "$(dirname "${BASH_SOURCE[0]}")/should-skip-job.sh" --job-type validation; then
4+
exit 0
5+
fi
6+
37
DEVICE=$1
48

59
echo "Running UI tests on $DEVICE. The iOS version will be the latest available in the CI host."

.buildkite/commands/run-unit-tests-reader.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/bash -eu
22

3+
if "$(dirname "${BASH_SOURCE[0]}")/should-skip-job.sh" --job-type validation; then
4+
exit 0
5+
fi
6+
37
# Run this at the start to fail early if value not available
48
# TODO: We'll need to create a token for Reader
59
# echo '--- :test-analytics: Configuring Test Analytics'

.buildkite/commands/run-unit-tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/bash -eu
22

3+
if "$(dirname "${BASH_SOURCE[0]}")/should-skip-job.sh" --job-type validation; then
4+
exit 0
5+
fi
6+
37
# Run this at the start to fail early if value not available
48
echo '--- :test-analytics: Configuring Test Analytics'
59
export BUILDKITE_ANALYTICS_TOKEN=$BUILDKITE_ANALYTICS_TOKEN_UNIT_TESTS
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/bin/bash -eu
2+
3+
# Usage: should-skip-job.sh --job-type [validation|localization|build]
4+
# --job-type validation: For jobs like unit/instrumented tests, manifest validation…
5+
# Skip when changes are limited to documentation, tooling, non-code files, and localization files
6+
# --job-type localization: For jobs doing linting, especially ones that might include linting of localization files
7+
# Check if any localization files have changed
8+
# --job-type build: For jobs building an apk binary, e.g. Assemble, Prototype Builds…
9+
# Skip when changes are limited to documentation, tooling, and non-code files
10+
#
11+
# Return codes:
12+
# 0 - Job should be skipped (script also handles displaying the message and annotation)
13+
# 1 - Job should not be skipped
14+
# 15 - Error in script parameters
15+
16+
COMMON_PATTERNS=(
17+
"*.md"
18+
"*.po"
19+
"*.pot"
20+
"*.txt"
21+
".gitignore"
22+
"config/Version.public.xcconfig"
23+
"docs/**"
24+
"fastlane/**"
25+
"Gemfile"
26+
"Gemfile.lock"
27+
)
28+
29+
LOCALIZATION_PATTERNS=(
30+
"**/*.strings"
31+
"**/*.stringsdict"
32+
)
33+
34+
# Define constants for job types
35+
VALIDATION="validation"
36+
BUILD="build"
37+
LOCALIZATION="localization"
38+
39+
# Check if arguments are valid
40+
if [ -z "${1:-}" ] || [ "$1" != "--job-type" ] || [ -z "${2:-}" ]; then
41+
echo "Error: Must specify --job-type [$VALIDATION|$BUILD|$LOCALIZATION]"
42+
buildkite-agent step cancel
43+
exit 15
44+
fi
45+
46+
# Function to display skip message and create annotation
47+
show_skip_message() {
48+
local job_type=$1
49+
local message="Skipped ${BUILDKITE_LABEL:-Job} - no relevant files changed"
50+
local context="skip-$(echo "${BUILDKITE_LABEL:-$job_type}" | sed -E -e 's/[^[:alnum:]]+/-/g' | tr A-Z a-z)"
51+
52+
echo "$message" | buildkite-agent annotate --style "info" --context "$context"
53+
echo "$message"
54+
}
55+
56+
job_type="$2"
57+
case "$job_type" in
58+
$VALIDATION)
59+
# We should skip if changes are limited to documentation, tooling, non-code files, and localization files
60+
PATTERNS=("${COMMON_PATTERNS[@]}" "${LOCALIZATION_PATTERNS[@]}")
61+
if pr_changed_files --all-match "${PATTERNS[@]}"; then
62+
show_skip_message "$job_type"
63+
exit 0
64+
fi
65+
exit 1
66+
;;
67+
$LOCALIZATION)
68+
# Check if any localization files have changed
69+
# Return true (skip) if NO localization files have changed
70+
if ! pr_changed_files --any-match "${LOCALIZATION_PATTERNS[@]}"; then
71+
show_skip_message "$job_type"
72+
exit 0
73+
fi
74+
exit 1
75+
;;
76+
$BUILD)
77+
# We should skip if changes are limited to documentation, tooling, and non-code files
78+
# We'll let the job run (won't skip) if PR includes changes in localization files though
79+
PATTERNS=("${COMMON_PATTERNS[@]}")
80+
if pr_changed_files --all-match "${PATTERNS[@]}"; then
81+
show_skip_message "$job_type"
82+
exit 0
83+
fi
84+
exit 1
85+
;;
86+
*)
87+
echo "Error: Job type must be either '$VALIDATION', '$BUILD', or '$LOCALIZATION'"
88+
buildkite-agent step cancel
89+
exit 15
90+
;;
91+
esac

.buildkite/pipeline.yml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ steps:
1010
steps:
1111
- label: ":eyeglasses: Reader ASC Demo Build"
1212
key: build_asc_reader
13-
command: .buildkite/commands/release-build-reader.sh
13+
command: |
14+
if .buildkite/commands/should-skip-job.sh --job-type build; then
15+
exit 0
16+
fi
17+
18+
.buildkite/commands/release-build-reader.sh
1419
plugins: [$CI_TOOLKIT_PLUGIN]
1520
artifact_paths:
1621
- Artifacts/*.zip
@@ -30,7 +35,12 @@ steps:
3035

3136
- label: ":eyeglasses: Reader one-off TestFlight Upload"
3237
depends_on: [build_asc_reader, testflight_triggered_reader]
33-
command: .buildkite/commands/release-upload-reader.sh
38+
command: |
39+
if .buildkite/commands/should-skip-job.sh --job-type build; then
40+
exit 0
41+
fi
42+
43+
.buildkite/commands/release-upload-reader.sh
3444
plugins: [$CI_TOOLKIT_PLUGIN]
3545
notify:
3646
- github_commit_status:
@@ -102,6 +112,10 @@ steps:
102112
context: "Reader Unit Tests"
103113
- label: "🔬 Keystone Unit Tests"
104114
command: |
115+
if .buildkite/commands/should-skip-job.sh --job-type validation; then
116+
exit 0
117+
fi
118+
105119
.buildkite/commands/shared-set-up.sh
106120
xcodebuild \
107121
-scheme Keystone \
@@ -116,6 +130,10 @@ steps:
116130
context: "Unit Tests Keystone"
117131
- label: "🔬 WordPressData Unit Tests"
118132
command: |
133+
if .buildkite/commands/should-skip-job.sh --job-type validation; then
134+
exit 0
135+
fi
136+
119137
.buildkite/commands/shared-set-up.sh
120138
xcodebuild \
121139
-scheme WordPressData \

.buildkite/shared-pipeline-vars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# The ~> modifier is not currently used, but we check for it just in case
77
XCODE_VERSION=$(sed -E 's/^~> ?//' .xcode-version)
8-
CI_TOOLKIT_PLUGIN_VERSION="3.8.0"
8+
CI_TOOLKIT_PLUGIN_VERSION="5.3.1"
99

1010
export IMAGE_ID="xcode-$XCODE_VERSION"
1111
export CI_TOOLKIT_PLUGIN="automattic/a8c-ci-toolkit#$CI_TOOLKIT_PLUGIN_VERSION"

0 commit comments

Comments
 (0)