|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +WORKSPACE=${WORKSPACE:-"$(pwd)"} |
| 5 | +BIN="${WORKSPACE}/bin" |
| 6 | +platform_type="$(uname)" |
| 7 | +platform_type_lowercase=$(echo "$platform_type" | tr '[:upper:]' '[:lower:]') |
| 8 | +arch_type="$(uname -m)" |
| 9 | +GITHUB_PR_TRIGGER_COMMENT=${GITHUB_PR_TRIGGER_COMMENT:-""} |
| 10 | +ONLY_DOCS=${ONLY_DOCS:-"true"} |
| 11 | +UI_MACOS_TESTS="$(buildkite-agent meta-data get UI_MACOS_TESTS --default ${UI_MACOS_TESTS:-"false"})" |
| 12 | +runAllStages="$(buildkite-agent meta-data get runAllStages --default ${runAllStages:-"false"})" |
| 13 | +metricbeat_changeset=( |
| 14 | + "^metricbeat/.*" |
| 15 | + "^go.mod" |
| 16 | + "^pytest.ini" |
| 17 | + "^dev-tools/.*" |
| 18 | + "^libbeat/.*" |
| 19 | + "^testing/.*" |
| 20 | + ) |
| 21 | +oss_changeset=( |
| 22 | + "^go.mod" |
| 23 | + "^pytest.ini" |
| 24 | + "^dev-tools/.*" |
| 25 | + "^libbeat/.*" |
| 26 | + "^testing/.*" |
| 27 | +) |
| 28 | +ci_changeset=( |
| 29 | + "^.buildkite/.*" |
| 30 | +) |
| 31 | +go_mod_changeset=( |
| 32 | + "^go.mod" |
| 33 | + ) |
| 34 | +docs_changeset=( |
| 35 | + ".*\\.(asciidoc|md)" |
| 36 | + "deploy/kubernetes/.*-kubernetes\\.yaml" |
| 37 | + ) |
| 38 | +packaging_changeset=( |
| 39 | + "^dev-tools/packaging/.*" |
| 40 | + ".go-version" |
| 41 | + ) |
| 42 | + |
| 43 | +with_docker_compose() { |
| 44 | + local version=$1 |
| 45 | + echo "Setting up the Docker-compose environment..." |
| 46 | + create_workspace |
| 47 | + retry 3 curl -sSL -o ${BIN}/docker-compose "https://github.com/docker/compose/releases/download/${version}/docker-compose-${platform_type_lowercase}-${arch_type}" |
| 48 | + chmod +x ${BIN}/docker-compose |
| 49 | + export PATH="${BIN}:${PATH}" |
| 50 | + docker-compose version |
| 51 | +} |
| 52 | + |
| 53 | +create_workspace() { |
| 54 | + if [[ ! -d "${BIN}" ]]; then |
| 55 | + mkdir -p "${BIN}" |
| 56 | + fi |
| 57 | +} |
| 58 | + |
| 59 | +add_bin_path() { |
| 60 | + echo "Adding PATH to the environment variables..." |
| 61 | + create_workspace |
| 62 | + export PATH="${BIN}:${PATH}" |
| 63 | +} |
| 64 | + |
| 65 | +check_platform_architeture() { |
| 66 | + case "${arch_type}" in |
| 67 | + "x86_64") |
| 68 | + go_arch_type="amd64" |
| 69 | + ;; |
| 70 | + "aarch64") |
| 71 | + go_arch_type="arm64" |
| 72 | + ;; |
| 73 | + "arm64") |
| 74 | + go_arch_type="arm64" |
| 75 | + ;; |
| 76 | + *) |
| 77 | + echo "The current platform/OS type is unsupported yet" |
| 78 | + ;; |
| 79 | + esac |
| 80 | +} |
| 81 | + |
| 82 | +with_mage() { |
| 83 | + local install_packages=( |
| 84 | + "github.com/magefile/mage" |
| 85 | + "github.com/elastic/go-licenser" |
| 86 | + "golang.org/x/tools/cmd/goimports" |
| 87 | + "github.com/jstemmer/go-junit-report" |
| 88 | + "gotest.tools/gotestsum" |
| 89 | + ) |
| 90 | + create_workspace |
| 91 | + for pkg in "${install_packages[@]}"; do |
| 92 | + go install "${pkg}@latest" |
| 93 | + done |
| 94 | +} |
| 95 | + |
| 96 | +with_go() { |
| 97 | + echo "Setting up the Go environment..." |
| 98 | + create_workspace |
| 99 | + check_platform_architeture |
| 100 | + retry 5 curl -sL -o "${BIN}/gvm" "https://github.com/andrewkroh/gvm/releases/download/${SETUP_GVM_VERSION}/gvm-${platform_type_lowercase}-${go_arch_type}" |
| 101 | + chmod +x "${BIN}/gvm" |
| 102 | + eval "$(gvm $GO_VERSION)" |
| 103 | + go version |
| 104 | + which go |
| 105 | + local go_path="$(go env GOPATH):$(go env GOPATH)/bin" |
| 106 | + export PATH="${go_path}:${PATH}" |
| 107 | +} |
| 108 | + |
| 109 | +with_python() { |
| 110 | + if [ "${platform_type}" == "Linux" ]; then |
| 111 | + #sudo command doesn't work at the "pre-command" hook because of another user environment (root with strange permissions) |
| 112 | + sudo apt-get update |
| 113 | + sudo apt-get install -y python3-pip python3-venv |
| 114 | + elif [ "${platform_type}" == "Darwin" ]; then |
| 115 | + brew update |
| 116 | + pip3 install virtualenv |
| 117 | + ulimit -Sn 10000 |
| 118 | + fi |
| 119 | +} |
| 120 | + |
| 121 | +with_dependencies() { |
| 122 | + if [ "${platform_type}" == "Linux" ]; then |
| 123 | + #sudo command doesn't work at the "pre-command" hook because of another user environment (root with strange permissions) |
| 124 | + sudo apt-get update |
| 125 | + sudo apt-get install -y libsystemd-dev libpcap-dev |
| 126 | + elif [ "${platform_type}" == "Darwin" ]; then |
| 127 | + pip3 install libpcap |
| 128 | + fi |
| 129 | +} |
| 130 | + |
| 131 | +retry() { |
| 132 | + local retries=$1 |
| 133 | + shift |
| 134 | + local count=0 |
| 135 | + until "$@"; do |
| 136 | + exit=$? |
| 137 | + wait=$((2 ** count)) |
| 138 | + count=$((count + 1)) |
| 139 | + if [ $count -lt "$retries" ]; then |
| 140 | + >&2 echo "Retry $count/$retries exited $exit, retrying in $wait seconds..." |
| 141 | + sleep $wait |
| 142 | + else |
| 143 | + >&2 echo "Retry $count/$retries exited $exit, no more retries left." |
| 144 | + return $exit |
| 145 | + fi |
| 146 | + done |
| 147 | + return 0 |
| 148 | +} |
| 149 | + |
| 150 | +are_paths_changed() { |
| 151 | + local patterns=("${@}") |
| 152 | + local changelist=() |
| 153 | + |
| 154 | + for pattern in "${patterns[@]}"; do |
| 155 | + changed_files=($(git diff --name-only HEAD@{1} HEAD | grep -E "$pattern")) |
| 156 | + if [ "${#changed_files[@]}" -gt 0 ]; then |
| 157 | + changelist+=("${changed_files[@]}") |
| 158 | + fi |
| 159 | + done |
| 160 | + |
| 161 | + if [ "${#changelist[@]}" -gt 0 ]; then |
| 162 | + echo "Files changed:" |
| 163 | + echo "${changelist[*]}" |
| 164 | + return 0 |
| 165 | + else |
| 166 | + echo "No files changed within specified changeset:" |
| 167 | + echo "${patterns[*]}" |
| 168 | + return 1 |
| 169 | + fi |
| 170 | +} |
| 171 | + |
| 172 | +are_changed_only_paths() { |
| 173 | + local patterns=("${@}") |
| 174 | + local changelist=() |
| 175 | + local changed_files=$(git diff --name-only HEAD@{1} HEAD) |
| 176 | + if [ -z "$changed_files" ] || grep -qE "$(IFS=\|; echo "${patterns[*]}")" <<< "$changed_files"; then |
| 177 | + return 0 |
| 178 | + else |
| 179 | + return 1 |
| 180 | + fi |
| 181 | +} |
| 182 | + |
| 183 | +are_conditions_met_mandatory_tests() { |
| 184 | + if [[ "${BUILDKITE_PULL_REQUEST}" == "" ]] || [[ "${runAllStages}" == "true" ]] || [[ "${ONLY_DOCS}" == "false" && "${BUILDKITE_PULL_REQUEST}" != "" ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/Jenkinsfile#L107-L137 |
| 185 | + if are_paths_changed "${metricbeat_changeset[@]}" || are_paths_changed "${oss_changeset[@]}" || are_paths_changed "${ci_changeset[@]}" || [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "/test metricbeat" ]] || [[ "${GITHUB_PR_LABELS}" =~ Metricbeat ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/metricbeat/Jenkinsfile.yml#L3-L12 |
| 186 | + return 0 |
| 187 | + else |
| 188 | + return 1 |
| 189 | + fi |
| 190 | + else |
| 191 | + return 1 |
| 192 | + fi |
| 193 | +} |
| 194 | + |
| 195 | +are_conditions_met_extended_tests() { |
| 196 | + if are_conditions_met_mandatory_tests; then #from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/Jenkinsfile#L145-L171 |
| 197 | + return 0 |
| 198 | + else |
| 199 | + return 1 |
| 200 | + fi |
| 201 | +} |
| 202 | + |
| 203 | +are_conditions_met_macos_tests() { |
| 204 | + if are_conditions_met_mandatory_tests; then #from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/Jenkinsfile#L145-L171 |
| 205 | + if [[ "${UI_MACOS_TESTS}" == true ]] || [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "/test metricbeat for macos" ]] || [[ "${GITHUB_PR_LABELS}" =~ macOS ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/metricbeat/Jenkinsfile.yml#L3-L12 |
| 206 | + return 0 |
| 207 | + else |
| 208 | + return 1 |
| 209 | + fi |
| 210 | + else |
| 211 | + return 1 |
| 212 | + fi |
| 213 | +} |
| 214 | + |
| 215 | +are_conditions_met_extended_windows_tests() { |
| 216 | + if [[ "${ONLY_DOCS}" == "false" && "${BUILDKITE_PULL_REQUEST}" != "" ]] || [[ "${runAllStages}" == "true" ]]; then #from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/Jenkinsfile#L145-L171 |
| 217 | + if are_paths_changed "${metricbeat_changeset[@]}" || are_paths_changed "${oss_changeset[@]}" || are_paths_changed "${ci_changeset[@]}" || [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "/test metricbeat" ]] || [[ "${GITHUB_PR_LABELS}" =~ Metricbeat ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/metricbeat/Jenkinsfile.yml#L3-L12 |
| 218 | + return 0 |
| 219 | + else |
| 220 | + return 1 |
| 221 | + fi |
| 222 | + else |
| 223 | + return 1 |
| 224 | + fi |
| 225 | +} |
| 226 | + |
| 227 | +are_conditions_met_packaging() { |
| 228 | + if are_conditions_met_extended_windows_tests; then #from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/Jenkinsfile#L145-L171 |
| 229 | + if are_paths_changed "${metricbeat_changeset[@]}" || are_paths_changed "${oss_changeset[@]}" || [[ "${BUILDKITE_TAG}" == "" ]] || [[ "${BUILDKITE_PULL_REQUEST}" != "" ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/metricbeat/Jenkinsfile.yml#L101-L103 |
| 230 | + return 0 |
| 231 | + else |
| 232 | + return 1 |
| 233 | + fi |
| 234 | + else |
| 235 | + return 1 |
| 236 | + fi |
| 237 | +} |
| 238 | + |
| 239 | +if ! are_changed_only_paths "${docs_changeset[@]}" ; then |
| 240 | + ONLY_DOCS="false" |
| 241 | + echo "Changes include files outside the docs_changeset vairiabe. ONLY_DOCS=$ONLY_DOCS." |
| 242 | +else |
| 243 | + echo "All changes are related to DOCS. ONLY_DOCS=$ONLY_DOCS." |
| 244 | +fi |
| 245 | + |
| 246 | +if are_paths_changed "${go_mod_changeset[@]}" ; then |
| 247 | + GO_MOD_CHANGES="true" |
| 248 | +fi |
| 249 | + |
| 250 | +if are_paths_changed "${packaging_changeset[@]}" ; then |
| 251 | + PACKAGING_CHANGES="true" |
| 252 | +fi |
0 commit comments