|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +manifest_path="${1:-manifest.yaml}" |
| 6 | +actions_root="${ACTIONS_CACHE_ROOT:-/home/runner/_work/_actions}" |
| 7 | + |
| 8 | +if ! command -v yq >/dev/null 2>&1; then |
| 9 | + echo "Error: yq is required but not installed." >&2 |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +if [[ ! -f "${manifest_path}" ]]; then |
| 14 | + echo "Error: manifest file not found at ${manifest_path}" >&2 |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +mkdir -p "${actions_root}" |
| 19 | + |
| 20 | +declare -A seen_actions=() |
| 21 | + |
| 22 | +while IFS= read -r action_ref; do |
| 23 | + [[ -z "${action_ref}" ]] && continue |
| 24 | + |
| 25 | + if [[ "${action_ref}" != *@* ]]; then |
| 26 | + echo "Error: invalid action entry '${action_ref}', expected owner/repo[/path]@ref" >&2 |
| 27 | + exit 1 |
| 28 | + fi |
| 29 | + |
| 30 | + repo_with_optional_path="${action_ref%@*}" |
| 31 | + ref="${action_ref##*@}" |
| 32 | + |
| 33 | + if [[ "${repo_with_optional_path}" != */* ]]; then |
| 34 | + echo "Error: invalid action repo '${repo_with_optional_path}' in '${action_ref}'" >&2 |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | + |
| 38 | + owner="${repo_with_optional_path%%/*}" |
| 39 | + remainder="${repo_with_optional_path#*/}" |
| 40 | + repo="${remainder%%/*}" |
| 41 | + |
| 42 | + if [[ -z "${owner}" || -z "${repo}" || -z "${ref}" ]]; then |
| 43 | + echo "Error: invalid action entry '${action_ref}'" >&2 |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | + |
| 47 | + cache_key="${owner}/${repo}@${ref}" |
| 48 | + if [[ -n "${seen_actions[${cache_key}]:-}" ]]; then |
| 49 | + continue |
| 50 | + fi |
| 51 | + seen_actions["${cache_key}"]=1 |
| 52 | + |
| 53 | + dest_dir="${actions_root}/${owner}/${repo}/${ref}" |
| 54 | + archive_url="https://codeload.github.com/${owner}/${repo}/tar.gz/${ref}" |
| 55 | + |
| 56 | + rm -rf "${dest_dir}" |
| 57 | + mkdir -p "${dest_dir}" |
| 58 | + |
| 59 | + echo "Caching ${cache_key}" |
| 60 | + curl -fsSL "${archive_url}" \ |
| 61 | + | tar -xz --strip-components=1 -C "${dest_dir}" |
| 62 | + |
| 63 | + touch "${dest_dir}/.completed" |
| 64 | +done < <(yq e -r '.cache.actions[]' "${manifest_path}") |
0 commit comments