Repository navigation
Expand file tree
/
Copy pathwarm-local.sh
More file actions
executable file
·124 lines (117 loc) · 4.96 KB
/
Copy pathwarm-local.sh
File metadata and controls
executable file
·124 lines (117 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env bash
# Warm a project's compile-cache seed on this machine.
#
# For projects whose warm-up does not fit a hosted runner ("warm": "local" in
# warm-targets.json). The publish workflow only assembles the seeds this pushes.
#
# Usage: warm-local.sh [options] PROJECT [GROUP...]
#
# --ref REF project commit, tag or branch (default: the remote's HEAD)
# --image IMAGE toolchain image (default: ghcr.io/lastrada-software/cpp-ci:latest)
# --jobs N parallel compile jobs (default: nproc)
# --checkout-dir DIR host directory for the checkout and scratch space
# (default: ${XDG_CACHE_HOME:-~/.cache}/cpp-ci-warm/PROJECT)
# --manifest FILE targets manifest (default: warm-targets.json beside scripts/)
# --no-pull use the local copy of the image
# --push push each seed as :latest and :<project-sha>. Without it
# the seed images are only built locally.
#
# Every group is warmed and its seed image built before anything is pushed, so a
# failing group leaves the registry as it was. Each group starts from the
# previous published seed.
set -euo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
manifest="$HERE/../warm-targets.json"
SEED_REGISTRY="${SEED_REGISTRY:-ghcr.io/lastrada-software/cpp-ci-seed}"
image="${CPP_CI_IMAGE:-ghcr.io/lastrada-software/cpp-ci:latest}"
ref="" jobs="$(nproc)" checkout_dir="" pull=1 push=""
args=()
while [ $# -gt 0 ]; do
case "$1" in
--ref) ref="$2"; shift ;;
--image) image="$2"; shift ;;
--jobs) jobs="$2"; shift ;;
--checkout-dir) checkout_dir="$2"; shift ;;
--manifest) manifest="$2"; shift ;;
--no-pull) pull="" ;;
--push) push=1 ;;
-h|--help) sed -n '2,/^set -/p' "$0" | sed '$d;s/^# \{0,1\}//'; exit 0 ;;
-*) echo "unknown option: $1" >&2; exit 2 ;;
*) args+=("$1") ;;
esac
shift
done
[ "${#args[@]}" -ge 1 ] || { echo "usage: warm-local.sh [options] PROJECT [GROUP...]" >&2; exit 2; }
project="${args[0]}"
groups=("${args[@]:1}")
repo="$(jq -er --arg p "$project" '.projects[] | select(.name==$p) | .repo' "$manifest")" \
|| { echo "unknown project: $project" >&2; exit 2; }
if [ "${#groups[@]}" -eq 0 ]; then
mapfile -t groups < <(jq -r --arg p "$project" '.projects[] | select(.name==$p) | .groups[].name' "$manifest")
fi
for g in "${groups[@]}"; do
jq -e --arg p "$project" --arg g "$g" '.projects[] | select(.name==$p) | .groups[] | select(.name==$g)' \
"$manifest" >/dev/null || { echo "project $project has no group $g" >&2; exit 2; }
done
checkout_dir="${checkout_dir:-${XDG_CACHE_HOME:-$HOME/.cache}/cpp-ci-warm/$project}"
src="$checkout_dir/src"
mkdir -p "$checkout_dir"
checkout_dir="$(cd "$checkout_dir" && pwd)"
src="$checkout_dir/src"
# The project at one commit. A reused checkout keeps its ignored .cache (CPM's
# source cache) and nothing else untracked, so no build tree or stray file of an
# earlier run reaches this one.
url="https://github.com/$repo.git"
if [ ! -d "$src/.git" ]; then
git clone --quiet "$url" "$src"
fi
git -C "$src" fetch --quiet --tags --force origin '+refs/heads/*:refs/remotes/origin/*'
if [ -z "$ref" ]; then
sha="$(git ls-remote "$url" HEAD | awk '{print $1}')"
else
sha="$(git -C "$src" rev-parse --verify --quiet "origin/$ref^{commit}" || git -C "$src" rev-parse --verify "$ref^{commit}")"
fi
test -n "$sha"
git -C "$src" checkout --quiet --force --detach "$sha"
git -C "$src" clean -ffdxq -e .cache
test "$(git -C "$src" rev-parse HEAD)" = "$sha"
echo "$project at $sha ($src)"
if [ -n "$pull" ]; then
docker pull "$image"
fi
if [ -x "$HERE/projects/$project/prepare.sh" ]; then
WORKSPACE="$src" "$HERE/projects/$project/prepare.sh"
fi
built=()
for group in "${groups[@]}"; do
echo "=== warming $project/$group ==="
seed_image="$SEED_REGISTRY-$project-$group"
work="$checkout_dir/work/$group"
rm -rf "$work"
mkdir -p "$work"
seed_args=()
previous=()
if "$HERE/fetch-previous-seed.sh" --strict "$seed_image:latest" "$work/previous.cow"; then
seed_args=(--seed "$work/previous.cow")
previous=(--previous "$work/previous.cow" --previous-image "$seed_image:latest")
else
status=$?
[ "$status" -eq 1 ] || exit "$status"
echo "no previous seed for $project/$group; starting empty"
fi
"$HERE/run-warm-container.sh" --manifest "$manifest" --image "$image" --project "$project" --group "$group" \
--src "$src" --work "$work" --jobs "$jobs" --user "$(id -u):$(id -g)" \
"${seed_args[@]}"
"$HERE/build-seed-image.sh" --tag "$seed_image:$sha" --store "$work/out/cache.cow" \
--base-image "$image" --project-ref "$sha" "${previous[@]}"
built+=("$seed_image")
done
for img in "${built[@]}"; do
if [ -n "$push" ]; then
docker tag "$img:$sha" "$img:latest"
docker push "$img:$sha"
docker push "$img:latest"
else
echo "built $img:$sha; with --push it would also push $img:latest"
fi
done