gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/.buildkite/hooks/post-command (about)

     1  set -x
     2  
     3  # Clear any downloaded credentials.
     4  rm -f repo.key
     5  
     6  # Upload all relevant test failures.
     7  make -s testlogs 2>/dev/null | grep // | sort | uniq | (
     8    declare log_count=0
     9    while read target log; do
    10      if test -z "${target}"; then
    11        continue
    12      fi
    13  
    14      # N.B. If *all* tests fail due to some common cause, then we will
    15      # end up spending way too much time uploading logs. Instead, we just
    16      # upload the first 10 and stop. That is hopefully enough to debug.
    17      #
    18      # We include this test in the metadata, but note that we cannot
    19      # upload the actual test logs. The user should rerun locally.
    20      log_count=$((${log_count}+1))
    21      if test "${log_count}" -ge 10; then
    22        echo " * ${target} (no upload)" | \
    23          buildkite-agent annotate --style error --context failures --append
    24      else
    25        buildkite-agent artifact upload "${log}"
    26        echo " * [${target}](artifact://${log#/}) (${BUILDKITE_LABEL})" | \
    27          buildkite-agent annotate --style error --context failures --append
    28      fi
    29    done
    30  )
    31  
    32  # Upload all profiles, and include in an annotation.
    33  declare profile_output=$(mktemp --tmpdir)
    34  for file in $(find /tmp/profile -name \*.pprof -print 2>/dev/null | sort); do
    35    # Generate a link to the profile parsing function in gvisor.dev, which
    36    # implicitly uses a prefix of https://storage.googleapis.com. Note that
    37    # this relies on the specific BuildKite bucket location, and will break if
    38    # this changes (although the artifacts will still exist and be just fine).
    39    profile_name="${file#/tmp/profile/}"
    40    profile_url="https://gvisor.dev/profile/gvisor-buildkite/${BUILDKITE_BUILD_ID}/${BUILDKITE_JOB_ID}/${file#/}/"
    41    buildkite-agent artifact upload "${file}"
    42    echo "<li><a href='${profile_url}'>${profile_name}</a></li>" >> "${profile_output}"
    43  done
    44  
    45  # Upload if we had outputs.
    46  if test -s "${profile_output}"; then
    47    # Make the list a collapsible section in markdown.
    48    sed -i "1s|^|<details><summary>${BUILDKITE_LABEL}</summary><ul>\n|" "${profile_output}"
    49    echo "</ul></details>" >> "${profile_output}"
    50    cat "${profile_output}" | buildkite-agent annotate --style info --context profiles --append
    51  fi
    52  rm -rf "${profile_output}"
    53  
    54  # Clean the bazel cache, if there's failure.
    55  if test "${BUILDKITE_COMMAND_EXIT_STATUS}" -ne "0"; then
    56    set -x
    57    dmesg -kT | tail -n 50
    58    # LINT.IfChange
    59    runtime="buildkite_runtime"
    60    # LINT.ThenChange(pre-command)
    61    if [ -d "/tmp/${runtime}/" ]; then
    62        tar -czf "/tmp/${BUILDKITE_JOB_ID}.tar.gz" -C /tmp/ "${runtime}"
    63        buildkite-agent artifact upload "/tmp/${BUILDKITE_JOB_ID}.tar.gz"
    64    fi
    65    # Attempt to clear the cache and shut down.
    66    make clean || echo "make clean failed with code $?"
    67    make bazel-shutdown || echo "make bazel-shutdown failed with code $?"
    68    # Attempt to clear any Go cache.
    69    sudo rm -rf "${HOME}/.cache/go-build"
    70    sudo rm -rf "${HOME}/go"
    71  fi
    72  
    73  # Kill any running containers (clear state), except for "bootstrap".
    74  for id_and_name in $(docker ps --format='{{.ID}}/{{.Names}}'); do
    75    if [[ "$(echo "$id_and_name" | cut -d'/' -f2-)" == 'bootstrap' ]]; then
    76      continue
    77    fi
    78    timeout --kill-after=10s --preserve-status 8s \
    79        docker container kill "$(echo "$id_and_name" | cut -d'/' -f1)"
    80  done
    81  
    82  set -euo pipefail
    83  
    84  if [[ "${BUILDKITE_PIPELINE_INSTALL_RUNTIME:-}" == "true" ]]; then
    85    # Remove all Docker runtimes that may be installed.
    86    num_docker_runtimes="$(jq \
    87        '(if has("runtimes") then .runtimes else {} end) | length' \
    88        < /etc/docker/daemon.json)"
    89    if [[ "$num_docker_runtimes" -gt 0 ]]; then
    90      cat /etc/docker/daemon.json | jq '.runtimes = {}' \
    91        | sudo tee /etc/docker/daemon.json.tmp
    92      sudo mv /etc/docker/daemon.json.tmp /etc/docker/daemon.json
    93      echo 'Removed all Docker runtimes; reloading Docker daemon with this new configuration:' >&2
    94      cat /etc/docker/daemon.json >&2
    95      bash -c "$DOCKER_RELOAD_COMMAND"
    96    fi
    97  fi
    98  
    99  # Cleanup temporary directory where STAGED_BINARIES may have been extracted.
   100  if [[ -n "${BUILDKITE_STAGED_BINARIES_DIRECTORY:-}" ]]; then
   101    rm -rf "$BUILDKITE_STAGED_BINARIES_DIRECTORY" || true
   102  fi