|
| 1 | +#!/bin/bash -eu |
| 2 | + |
| 3 | +# Usage: should-skip-job.sh --job-type [validation|build|localization] |
| 4 | +# --job-type validation: For jobs like unit/UI tests, authenticator tests... |
| 5 | +# Skip when changes are limited to documentation, tooling, non-code files, and localization files |
| 6 | +# --job-type localization: For jobs that check localization files |
| 7 | +# Check if any localization files have changed |
| 8 | +# Return true (skip) if NO localization files have changed |
| 9 | +# --job-type build: For jobs building app binaries, e.g. Build, Prototype Builds... |
| 10 | +# Skip when changes are limited to documentation, tooling, and non-code files |
| 11 | +# Does not skip if localization files changed |
| 12 | +# |
| 13 | +# Return codes: |
| 14 | +# 0 - Job should be skipped (script also handles displaying the message and annotation) |
| 15 | +# 1 - Job should not be skipped |
| 16 | +# 15 - Error in script parameters |
| 17 | + |
| 18 | +COMMON_PATTERNS=( |
| 19 | + "*.md" |
| 20 | + "*.pot" |
| 21 | + "*.txt" |
| 22 | + ".gitignore" |
| 23 | + "config/Version.Public.xcconfig" |
| 24 | + "docs/**" |
| 25 | + "fastlane/**" |
| 26 | + "Gemfile" |
| 27 | + "Gemfile.lock" |
| 28 | +) |
| 29 | + |
| 30 | +LOCALIZATION_PATTERNS=( |
| 31 | + "**/*.strings" |
| 32 | + "**/*.stringsdict" |
| 33 | +) |
| 34 | + |
| 35 | +# Check if arguments are valid |
| 36 | +if [ -z "${1:-}" ] || [ "$1" != "--job-type" ] || [ -z "${2:-}" ]; then |
| 37 | + echo "Error: Must specify --job-type [validation|build|localization]" |
| 38 | + buildkite-agent step cancel |
| 39 | + exit 15 |
| 40 | +fi |
| 41 | + |
| 42 | +# Function to display skip message and create annotation |
| 43 | +show_skip_message() { |
| 44 | + local job_type=$1 |
| 45 | + local message="Skipping ${BUILDKITE_LABEL:-Job} - no relevant files changed" |
| 46 | + local context="skip-$(echo "${BUILDKITE_LABEL:-$job_type}" | sed -E -e 's/[^[:alnum:]]+/-/g' | tr A-Z a-z)" |
| 47 | + |
| 48 | + echo "$message" | buildkite-agent annotate --style "info" --context "$context" |
| 49 | + echo "$message" |
| 50 | +} |
| 51 | + |
| 52 | +job_type="$2" |
| 53 | +case "$job_type" in |
| 54 | + "validation") |
| 55 | + # We should skip if changes are limited to documentation, tooling, non-code files, and localization files |
| 56 | + PATTERNS=("${COMMON_PATTERNS[@]}" "${LOCALIZATION_PATTERNS[@]}") |
| 57 | + if pr_changed_files --all-match "${PATTERNS[@]}"; then |
| 58 | + show_skip_message "$job_type" |
| 59 | + exit 0 |
| 60 | + fi |
| 61 | + exit 1 |
| 62 | + ;; |
| 63 | + "localization") |
| 64 | + # Check if any localization files have changed |
| 65 | + # Return true (skip) if NO localization files have changed |
| 66 | + if ! pr_changed_files --any-match "${LOCALIZATION_PATTERNS[@]}"; then |
| 67 | + show_skip_message "$job_type" |
| 68 | + exit 0 |
| 69 | + fi |
| 70 | + exit 1 |
| 71 | + ;; |
| 72 | + "build") |
| 73 | + # We should skip if changes are limited to documentation, tooling, and non-code files |
| 74 | + # We'll let the job run (won't skip) if PR includes changes in localization files though |
| 75 | + PATTERNS=("${COMMON_PATTERNS[@]}") |
| 76 | + if pr_changed_files --all-match "${PATTERNS[@]}"; then |
| 77 | + show_skip_message "$job_type" |
| 78 | + exit 0 |
| 79 | + fi |
| 80 | + exit 1 |
| 81 | + ;; |
| 82 | + *) |
| 83 | + echo "Error: Job type must be either 'validation', 'build', or 'localization'" |
| 84 | + buildkite-agent step cancel |
| 85 | + exit 15 |
| 86 | + ;; |
| 87 | +esac |
0 commit comments