forked from ramsey/composer-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_key.sh
executable file
·68 lines (55 loc) · 1.75 KB
/
cache_key.sh
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
#!/usr/bin/env bash
function join_by {
local delimiter="${1-}"
local first="${2-}"
if shift 2; then
printf "%s" "${first}" "${@/#/$delimiter}"
fi
}
function make_key {
local -a non_empties
local element
for element; do
if [ -n "${element}" ]; then
non_empties+=("${element}")
fi
done
tr "[:blank:]" "-" <<<"${non_empties[*]}"
}
runner_os="${1}"
php_version="${2}"
dependency_versions="${3:-locked}"
composer_options="${4}"
files_hash="${5}"
custom_cache_key="${6}"
working_directory="${7}"
key=()
restore_key=()
# Ensure "highest", "lowest", and "locked" are the only values
# allowed for dependency_versions.
case "${dependency_versions}" in
highest) dependency_versions="highest" ;;
lowest) dependency_versions="lowest" ;;
*) dependency_versions="locked" ;;
esac
if [ -n "${custom_cache_key}" ]; then
key+=("${custom_cache_key}")
else
key+=("${runner_os}" "php" "${php_version}" "composer" "${composer_options}" "${dependency_versions}" "${working_directory}")
restore_key=("$(make_key "${key[@]}")-")
key+=("${files_hash}")
fi
# Remove duplicates.
# shellcheck disable=SC2207
uniq_restore_key=($(tr ' ' '\n' <<<"${restore_key[@]}" | awk '!u[$0]++' | tr '\n' ' '))
cache_key="$(make_key "${key[@]}")"
echo "::debug::Cache primary key is '${cache_key}'"
echo "::debug::Cache restore keys are '$(join_by ", " "${uniq_restore_key[@]}")'"
echo "key=${cache_key}" >> "${GITHUB_OUTPUT}"
# Use an environment variable to capture the multiline restore key.
# See: https://docs.github.com/en/actions/learn-github-actions/workflow-commands-for-github-actions#multiline-strings
{
echo "CACHE_RESTORE_KEY<<EOF"
printf '%s\n' "${uniq_restore_key[@]}"
echo "EOF"
} >> "${GITHUB_ENV}"