|
| 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 |
0 commit comments