|
| 1 | +#!/bin/bash -e |
| 2 | + |
| 3 | +# Copyright 2018- The Pixie Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +# SPDX-License-Identifier: Apache-2.0 |
| 18 | + |
| 19 | +# This script outputs a github actions matrix with each of the configs we want to build and test |
| 20 | +# It takes a file with the github context as its first argument: https://docs.github.com/en/actions/learn-github-actions/contexts#github-context |
| 21 | +github_context="$1" |
| 22 | + |
| 23 | +event_name="$(jq -r '.event_name' < "${github_context}")" |
| 24 | +pr_body="$(jq -r '.event.pull_request.body' < "${github_context}")" |
| 25 | +commit_message="$(git log "$(git merge-base origin/main HEAD)"..HEAD --format="%B")" |
| 26 | +combined_message="${pr_body} \n${commit_message}" |
| 27 | + |
| 28 | +nightly_regression_test_iterations="${NIGHTLY_REGRESSION_TEST_ITERATIONS:-5}" |
| 29 | + |
| 30 | +all_kernel_versions=( |
| 31 | + "4.14.254" |
| 32 | + "4.19.254" |
| 33 | + "5.10.173" |
| 34 | + "5.15.101" |
| 35 | + "5.4.235" |
| 36 | + "6.1.18" |
| 37 | +) |
| 38 | +default_kernel_versions=( |
| 39 | + "4.14.254" |
| 40 | + "6.1.18" |
| 41 | +) |
| 42 | +kernel_versions=( "${default_kernel_versions[@]}" ) |
| 43 | + |
| 44 | +check_tag() { |
| 45 | + tag="$1" |
| 46 | + echo "${combined_message}" | grep -P "${tag}([^\-a-zA-Z]|$)" > /dev/null |
| 47 | +} |
| 48 | + |
| 49 | +build_deps_flags=() |
| 50 | +extra_bazel_args=() |
| 51 | +# Build and test everything and BPF for main runs. |
| 52 | +if [[ "${event_name}" == "push" ]]; then |
| 53 | + echo "Main run. Building all targets and bpf" >&2 |
| 54 | + build_deps_flags+=("-a" "-b") |
| 55 | +elif [[ "${event_name}" == "schedule" ]]; then |
| 56 | + echo "Nightly run. Building all targets and all kernel versions of bpf" >&2 |
| 57 | + build_deps_flags+=("-a" "-b") |
| 58 | + extra_bazel_args+=("--runs_per_test=${nightly_regression_test_iterations}") |
| 59 | + kernel_versions=( "${all_kernel_versions[@]}" ) |
| 60 | +elif [[ "${event_name}" == "pull_request" ]]; then |
| 61 | + # Ignore bazel dependency tracking and run all targets if #ci:ignore-deps is in the pr body/commit message. |
| 62 | + if check_tag '#ci:ignore-deps'; then |
| 63 | + echo "Found #ci:ignore-deps tag. Building all targets" >&2 |
| 64 | + build_deps_flags+=("-a") |
| 65 | + fi |
| 66 | + # Build/Test bpf targets if #ci:bpf-build{-all-kernels} is in the pr body/commit message. |
| 67 | + if check_tag '#ci:bpf-build-all-kernels'; then |
| 68 | + echo "Found #ci:bpf-build tag. Building bpf targets" >&2 |
| 69 | + build_deps_flags+=("-b") |
| 70 | + kernel_versions=( "${all_kernel_versions[@]}" ) |
| 71 | + elif check_tag '#ci:bpf-build'; then |
| 72 | + echo "Found #ci:bpf-build-all-kernels. Building bpf targets on all kernel versions" >&2 |
| 73 | + build_deps_flags+=("-b") |
| 74 | + fi |
| 75 | +fi |
| 76 | + |
| 77 | +# Create the target pattern files, passthrough flags to bazel_build_deps. |
| 78 | +./ci/bazel_build_deps.sh "${build_deps_flags[@]}" |
| 79 | +# TODO(james): generate the target pattern files directly from ci/github/matrix.yaml instead of using bazel_build_deps, |
| 80 | +# so that we don't have to edit bazel_build_deps.sh for new configs. |
| 81 | + |
| 82 | +remove_targetless_configs() { |
| 83 | + f="$1" |
| 84 | + # Remove any configs that don't have buildables. |
| 85 | + while read -r build_target_file; |
| 86 | + do |
| 87 | + if [ -s "${build_target_file}" ]; then |
| 88 | + continue |
| 89 | + fi |
| 90 | + yq -i e 'del(.configs[] | select(.buildables=="'"${build_target_file}"'"))' "${f}" |
| 91 | + done < <(yq e '.configs[].buildables' "${f}" | sort | uniq) |
| 92 | + |
| 93 | + # Remove the `tests` key from any configs where the test target file is empty. |
| 94 | + while read -r test_target_file; |
| 95 | + do |
| 96 | + if [ -s "${test_target_file}" ]; then |
| 97 | + continue |
| 98 | + fi |
| 99 | + yq -i e 'del(.configs[].tests | select(.=="'"${test_target_file}"'"))' "${f}" |
| 100 | + done < <(yq e '.configs[].tests' "${f}" | sort | uniq) |
| 101 | +} |
| 102 | + |
| 103 | +remove_disabled_configs() { |
| 104 | + yq -i e 'del(.configs[] | select(.disabled))' "$1" |
| 105 | + yq -i e 'del(.configs[].disabled)' "$1" |
| 106 | +} |
| 107 | + |
| 108 | +merge_yamls() { |
| 109 | + # shellcheck disable=SC2016 |
| 110 | + yq eval-all '. as $item ireduce ({}; . *+ $item)' "$@" |
| 111 | +} |
| 112 | + |
| 113 | +extend_with_kernel_versions() { |
| 114 | + f="$1" |
| 115 | + kernel_matrix_configs="$(mktemp)" |
| 116 | + no_kernel="$(mktemp)" |
| 117 | + yq e 'del(.configs[] | select(.use_kernel_matrix != true))' "$f" > "${kernel_matrix_configs}" |
| 118 | + yq e 'del(.configs[] | select(.use_kernel_matrix))' "$f" > "${no_kernel}" |
| 119 | + |
| 120 | + version_files=() |
| 121 | + for version in "${kernel_versions[@]}" |
| 122 | + do |
| 123 | + version_configs="$(mktemp)" |
| 124 | + yq e '.configs[].args += " --//bazel/test_runners/qemu_with_kernel:kernel_version='"${version}"'"' "$kernel_matrix_configs" > "${version_configs}" |
| 125 | + yq -i e '.configs[].name += " ('"${version}"')"' "${version_configs}" |
| 126 | + version_files+=("${version_configs}") |
| 127 | + done |
| 128 | + |
| 129 | + merge_yamls "${version_files[@]}" "${no_kernel}" > "$f" |
| 130 | + yq -i e 'del(.configs[].use_kernel_matrix)' "$f" |
| 131 | + |
| 132 | + rm "${kernel_matrix_configs}" |
| 133 | + rm "${no_kernel}" |
| 134 | + for f in "${version_files[@]}" |
| 135 | + do |
| 136 | + rm "$f" |
| 137 | + done |
| 138 | +} |
| 139 | + |
| 140 | +add_extra_bazel_args() { |
| 141 | + f="$1" |
| 142 | + if [[ "${#extra_bazel_args[@]}" -gt 0 ]]; then |
| 143 | + yq -i e '.configs[].args += " '"${extra_bazel_args[*]}"'"' "$f" |
| 144 | + fi |
| 145 | +} |
| 146 | + |
| 147 | +process_matrix() { |
| 148 | + in="$1" |
| 149 | + out="$2" |
| 150 | + |
| 151 | + cp "${in}" "${out}" |
| 152 | + remove_disabled_configs "${out}" |
| 153 | + remove_targetless_configs "${out}" |
| 154 | + add_extra_bazel_args "${out}" |
| 155 | + extend_with_kernel_versions "${out}" |
| 156 | +} |
| 157 | + |
| 158 | +matrix="$(mktemp)" |
| 159 | +process_matrix "ci/github/matrix.yaml" "${matrix}" |
| 160 | + |
| 161 | + |
| 162 | +if [[ "$(yq e '.configs|length' "${matrix}")" != 0 ]]; then |
| 163 | + yq e '.include = .configs | del(.configs)' -I=0 -o=json "${matrix}" |
| 164 | +fi |
| 165 | + |
| 166 | +rm "${matrix}" |
0 commit comments