|
1 | | -#!/bin/bash |
| 1 | +#!/usr/bin/env bash |
| 2 | +# avoids ancient bash on macos |
2 | 3 | set -eu |
3 | 4 | # Copy build to cache |
4 | 5 |
|
5 | | - |
6 | 6 | # the copy commands are based on GNU cp tools |
7 | 7 | # On a mac `brew install coreutils` gives `g` prefixed cmd line tools such as gcp |
8 | 8 | # to use these define env variable GNU_CP=gcp before invoking this script. |
9 | 9 | CP="${GNU_CP:-cp}" |
10 | 10 | LS="${GNU_LS:-ls}" |
11 | 11 |
|
12 | | -OUTPUT_DIR="docker" |
13 | | -WORKING_DIR="." |
14 | | -CACHE_BUILD_KEY= |
15 | | -CACHE_LOCATION_USED_PATH= |
16 | | -DEBUG="${DEBUG:-false}" |
| 12 | +join() { |
| 13 | + local IFS="$1" |
| 14 | + shift |
| 15 | + echo "$*" |
| 16 | +} |
| 17 | + |
| 18 | +splitAtColon() { |
| 19 | + # colon is |
| 20 | + echo "$1" | tr ":" "\n" |
| 21 | +} |
| 22 | +# https://stackoverflow.com/a/918931 |
| 23 | + |
| 24 | +outputs_str= |
| 25 | +extra_inputs_str= |
| 26 | +declare -a outputs |
| 27 | +outputs=() |
| 28 | +declare -a inputs |
| 29 | +inputs=() |
| 30 | +working_dir="." |
| 31 | +cache_build="true" |
| 32 | +cache_build_key= |
| 33 | +cache_location_used_path= |
| 34 | +debug="${DEBUG:-false}" |
| 35 | +dry_run=false |
| 36 | + |
17 | 37 |
|
18 | 38 | while [ "$#" -gt 0 ]; do |
19 | 39 | case $1 in |
20 | 40 |
|
21 | | - --working-dir) WORKING_DIR="$2"; shift;; |
22 | | - --working-dir=*) WORKING_DIR="${1#*=}";; |
| 41 | + --working-dir) working_dir="$2"; shift;; |
| 42 | + --working-dir=*) working_dir="${1#*=}";; |
| 43 | + |
| 44 | + --cached-outputs) outputs_str="$2"; shift;; |
| 45 | + --cached-outputs=*) outputs_str="${1#*=}";; |
| 46 | + |
| 47 | + --build-extra-inputs) extra_inputs_str="$2"; shift;; |
| 48 | + --build-extra-inputs=*) extra_inputs_str="${1#*=}";; |
23 | 49 |
|
24 | | - --output-dir) OUTPUT_DIR="$2"; shift;; |
25 | | - --output-dir=*) OUTPUT_DIR="${1#*=}";; |
| 50 | + --cache-build) cache_build="$2"; shift;; |
| 51 | + --cache-build=*) cache_build="${1#*=}";; |
26 | 52 |
|
27 | | - --cache-build-key) CACHE_BUILD_KEY="$2"; shift;; |
28 | | - --cache-build-key=*) CACHE_BUILD_KEY="${1#*=}";; |
| 53 | + --cache-build-key) cache_build_key="$2"; shift;; |
| 54 | + --cache-build-key=*) cache_build_key="${1#*=}";; |
29 | 55 |
|
30 | | - --cache-location-used-path) CACHE_LOCATION_USED_PATH="$2"; shift;; |
31 | | - --cache-location-used-path=*) CACHE_LOCATION_USED_PATH="${1#*=}";; |
| 56 | + --cache-location-used-path) cache_location_used_path="$2"; shift;; |
| 57 | + --cache-location-used-path=*) cache_location_used_path="${1#*=}";; |
32 | 58 |
|
33 | | - --debug) DEBUG="$2"; shift;; |
34 | | - --debug=*) DEBUG="${1#*=}";; |
| 59 | + --debug) debug="$2"; shift;; |
| 60 | + --debug=*) debug="${1#*=}";; |
| 61 | + |
| 62 | + --dry-run) dry_run=true;; |
35 | 63 |
|
36 | 64 | *) echo "Unknown parameter passed: $1"; exit 1;; |
37 | 65 | esac; shift; done |
38 | 66 |
|
39 | | -if [ -z "${CACHE_BUILD_KEY}" ]; then |
| 67 | +if [ -z "${cache_build_key}" ]; then |
40 | 68 | echo "Param --cache-build-key is required."; exit 1; |
41 | | -elif [ -z "${CACHE_LOCATION_USED_PATH}" ]; then |
| 69 | +elif [ -z "${cache_location_used_path}" ]; then |
42 | 70 | echo "Param --cache-location-used-path is required."; exit 1; |
43 | 71 | fi |
44 | 72 |
|
45 | | -CP_VERBOSITY_FLAGS= |
46 | | -if [ "${DEBUG}" == "true" ]; then |
| 73 | +cp_verbosity_flags= |
| 74 | +if [ "${debug}" == "true" ]; then |
47 | 75 | set -x |
48 | | - CP_VERBOSITY_FLAGS="-v" |
| 76 | + cp_verbosity_flags="-v" |
49 | 77 | fi |
50 | 78 |
|
51 | | -ROOT_DIR=$(pwd) |
52 | | - |
53 | | -git_sha_working_dir="" |
54 | | -if [ "${WORKING_DIR}" == "." ]; then |
55 | | - git_sha_working_dir=$(git rev-parse "HEAD:") |
56 | | -else |
57 | | - git_sha_working_dir=$(git rev-parse "HEAD:$WORKING_DIR") |
| 79 | +if [ "$cache_build" != "true" ]; then |
| 80 | + echo "Build skipping is not enabled. Continuing with a regular build (cache_build==$cache_build)" |
| 81 | + exit 0 |
58 | 82 | fi |
59 | | -cache_location_dir="$ROOT_DIR/.ods-cache/build-task/$CACHE_BUILD_KEY/$git_sha_working_dir" |
60 | 83 |
|
61 | | -if [ "${WORKING_DIR}" != "." ]; then |
62 | | - cd "${WORKING_DIR}" |
| 84 | +# note leads to undefined variable if extra_inputs_str is empty on ancient bash |
| 85 | +IFS=":" read -r -a extra_inputs <<< "$extra_inputs_str" |
| 86 | +inputs=("$working_dir") |
| 87 | +for f in "${extra_inputs[@]}"; do |
| 88 | + inputs+=( "$f" ) |
| 89 | +done |
| 90 | + |
| 91 | +IFS=":" read -r -a outputs <<< "$outputs_str" |
| 92 | + |
| 93 | +root_dir=$(pwd) |
| 94 | + |
| 95 | +declare -a git_shas # relative to root |
| 96 | +for f in "${inputs[@]}"; do |
| 97 | + if [ "${f}" == "." ]; then |
| 98 | + git_shas+=( "$(git rev-parse --short "HEAD:")" ) |
| 99 | + else |
| 100 | + git_shas+=( "$(git rev-parse --short "HEAD:$f")") |
| 101 | + fi |
| 102 | +done |
| 103 | +# shellcheck disable=SC2048,SC2086 |
| 104 | +git_sha_combined=$(join "-" ${git_shas[*]}) |
| 105 | +cache_location_dir="$root_dir/.ods-cache/build-task/$cache_build_key/$git_sha_combined" |
| 106 | + |
| 107 | +if [ "${working_dir}" != "." ]; then |
| 108 | + cd "${working_dir}" |
63 | 109 | fi |
64 | 110 |
|
65 | | -rm -rvf "$cache_location_dir" # should be empty as otherwise cache should be used. |
66 | | -mkdir -p "$cache_location_dir" |
| 111 | +if [ "${dry_run}" == "true" ]; then |
| 112 | + echo "(skipping ensuring empty cache location dir at $cache_location_dir)" |
| 113 | +else |
| 114 | + rm -rvf "$cache_location_dir" # should be empty as otherwise cache should be used. |
| 115 | + mkdir -p "$cache_location_dir" |
| 116 | +fi |
67 | 117 |
|
68 | 118 | # Copying ods artifacts which are mostly reports (see artifacts.adoc) |
69 | 119 | # TODO: consistent casing and naming across scripts regarding dir variables |
| 120 | + |
70 | 121 | cache_of_artifacts_dir="$cache_location_dir/artifacts" |
71 | | -tmp_artifacts_dir="${ROOT_DIR}/.ods/tmp-artifacts" |
72 | | -echo "Copying build artifacts to cache: $tmp_artifacts_dir -> $cache_of_artifacts_dir" |
73 | | -mkdir -p "$cache_of_artifacts_dir" |
74 | | -"$CP" -v -r "$tmp_artifacts_dir/." "$cache_of_artifacts_dir" |
| 122 | +tmp_artifacts_dir="${root_dir}/.ods/tmp-artifacts" |
| 123 | +echo "Copying ods build artifacts to cache: $tmp_artifacts_dir -> $cache_of_artifacts_dir" |
| 124 | +if [ "${dry_run}" == "true" ]; then |
| 125 | + echo "(skipping copying ods build artifacts)" |
| 126 | +else |
| 127 | + mkdir -p "$cache_of_artifacts_dir" |
| 128 | + "$CP" -v -r "$tmp_artifacts_dir/." "$cache_of_artifacts_dir" |
| 129 | +fi |
75 | 130 |
|
76 | 131 | # Copying build output |
77 | | -cache_of_output_dir="$cache_location_dir/output" |
78 | | -echo "Copying build output to cache: $OUTPUT_DIR to $cache_of_output_dir" |
79 | | -mkdir -p "$cache_of_output_dir" |
80 | | -start_time=$SECONDS |
81 | | -"$CP" $CP_VERBOSITY_FLAGS -r "$OUTPUT_DIR/." "$cache_of_output_dir" |
82 | | -elapsed=$(( SECONDS - start_time )) |
83 | | -echo "Copying took $elapsed seconds" |
84 | | -if [ "${DEBUG}" == "true" ]; then |
85 | | - echo "-- ls OUTPUT IN CACHE -- " |
86 | | - $LS -Ral "$cache_of_output_dir" |
| 132 | +for i in "${!outputs[@]}"; do |
| 133 | + cache_of_output_dir="$cache_location_dir/output/$i" |
| 134 | + output_dir="${outputs[$i]}" |
| 135 | + echo "Copying build output to cache: $output_dir to $cache_of_output_dir" |
| 136 | + if [ "${dry_run}" == "true" ]; then |
| 137 | + echo "(skipping copying build outputs)" |
| 138 | + else |
| 139 | + mkdir -p "$cache_of_output_dir" |
| 140 | + start_time=$SECONDS |
| 141 | + "$CP" $cp_verbosity_flags -r "$output_dir/." "$cache_of_output_dir" |
| 142 | + elapsed=$(( SECONDS - start_time )) |
| 143 | + echo "Copying took $elapsed seconds" |
| 144 | + if [ "${debug}" == "true" ]; then |
| 145 | + echo "-- ls OUTPUT IN CACHE -- " |
| 146 | + $LS -Ral "$cache_of_output_dir" |
| 147 | + fi |
| 148 | + fi |
| 149 | +done |
| 150 | + |
| 151 | +if [ "${dry_run}" == "true" ]; then |
| 152 | + echo "(skipping saving $cache_location_dir in $cache_location_used_path)" |
| 153 | + echo "(skipping touch of $cache_location_dir/.ods-last-used-stamp" |
| 154 | +else |
| 155 | + echo "$cache_location_dir" > "$cache_location_used_path" |
| 156 | + touch "$cache_location_dir/.ods-last-used-stamp" |
87 | 157 | fi |
88 | | - |
89 | | -echo "$cache_location_dir" > "$CACHE_LOCATION_USED_PATH" |
90 | | -touch "$cache_location_dir/.ods-last-used-stamp" |
|
0 commit comments