|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Usage: bin/report <build-dir> <cache-dir> <env-dir> |
| 3 | + |
| 4 | +# Produces a build report containing metadata about the build, that's consumed by the build system. |
| 5 | +# This script is run for both successful and failing builds, so it should not assume the build ran |
| 6 | +# to completion (e.g. OpenJDK may not even have been installed). |
| 7 | +# |
| 8 | +# Metadata must be emitted to stdout as valid YAML key-value pairs. Any fields that should always |
| 9 | +# be typed as a string must be explicitly quoted. |
| 10 | +# |
| 11 | +# Example valid stdout: |
| 12 | +# openjdk_version: 'X.Y.Z' |
| 13 | +# openjdk_install_duration: 1.234 |
| 14 | +# |
| 15 | +# Failures in this script don't cause the overall build to fail (and won't appear in user |
| 16 | +# facing build logs) to avoid breaking builds unnecessarily / causing confusion. To debug |
| 17 | +# issues check the internal build system logs for `buildpack.report.failed` events, or |
| 18 | +# when developing run `make run` in this repo locally, which runs `bin/report` too. |
| 19 | + |
| 20 | +set -euo pipefail |
| 21 | +shopt -s inherit_errexit |
| 22 | + |
| 23 | +CACHE_DIR="${2}" |
| 24 | + |
| 25 | +# The absolute path to the root of the buildpack. |
| 26 | +BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)" |
| 27 | + |
| 28 | +# The build system doesn't source the `export` script before running this script, so we have to do |
| 29 | +# so manually (if it exists) so that buildpack java can be found (if the build succeeded). |
| 30 | +EXPORT_FILE="${BUILDPACK_DIR}/export" |
| 31 | +if [[ -f "${EXPORT_FILE}" ]]; then |
| 32 | + # shellcheck source=/dev/null |
| 33 | + source "${EXPORT_FILE}" |
| 34 | +fi |
| 35 | + |
| 36 | +source "${BUILDPACK_DIR}/lib/metadata.sh" |
| 37 | +meta_init "${CACHE_DIR}" "java" |
| 38 | + |
| 39 | +# Emit the key / value pair unquoted to stdout. Skips if the value is empty. |
| 40 | +# Based on: https://github.com/heroku/heroku-buildpack-nodejs/blob/main/bin/report |
| 41 | +kv_pair() { |
| 42 | + local key="${1}" |
| 43 | + local value="${2}" |
| 44 | + if [[ -n "${value}" ]]; then |
| 45 | + echo "${key}: ${value}" |
| 46 | + fi |
| 47 | +} |
| 48 | + |
| 49 | +# Emit the key / value pair to stdout, safely quoting the string. Skips if the value is empty. |
| 50 | +# Based on: https://github.com/heroku/heroku-buildpack-nodejs/blob/main/bin/report |
| 51 | +# (Though instead uses single quotes instead of double to avoid escaping issues.) |
| 52 | +kv_pair_string() { |
| 53 | + local key="${1}" |
| 54 | + local value="${2}" |
| 55 | + if [[ -n "${value}" ]]; then |
| 56 | + # Escape any existing single quotes, which for YAML means replacing `'` with `''`. |
| 57 | + value="${value//\'/\'\'}" |
| 58 | + echo "${key}: '${value}'" |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +STRING_FIELDS=( |
| 63 | + # Fields coming from jvm-common |
| 64 | + openjdk_version |
| 65 | + openjdk_distribution |
| 66 | + openjdk_version_selector |
| 67 | +) |
| 68 | + |
| 69 | +# We don't want to quote numeric or boolean fields. |
| 70 | +ALL_OTHER_FIELDS=( |
| 71 | + # Fields coming from jvm-common |
| 72 | + openjdk_install_duration |
| 73 | + app_has_jdk_overlay |
| 74 | +) |
| 75 | + |
| 76 | +for field in "${STRING_FIELDS[@]}"; do |
| 77 | + # shellcheck disable=SC2312 # TODO: Invoke this command separately to avoid masking its return value. |
| 78 | + kv_pair_string "${field}" "$(meta_get "${field}")" |
| 79 | +done |
| 80 | + |
| 81 | +for field in "${ALL_OTHER_FIELDS[@]}"; do |
| 82 | + # shellcheck disable=SC2312 # TODO: Invoke this command separately to avoid masking its return value. |
| 83 | + kv_pair "${field}" "$(meta_get "${field}")" |
| 84 | +done |
0 commit comments