|
| 1 | +#!/bin/bash -e |
| 2 | +# Pre-cache runtime tools (Java, Python, Node) in hostedtoolcache |
| 3 | +# |
| 4 | +# When setup-* actions run (setup-java, setup-python, setup-node), they: |
| 5 | +# 1. Check RUNNER_TOOL_CACHE (or AGENT_TOOLSDIRECTORY) for pre-installed tools |
| 6 | +# 2. If found: use immediately (instant, no download) |
| 7 | +# 3. If not found: download and install (~200-500 MB, 2-5 minutes) |
| 8 | +# |
| 9 | +# This script pre-installs tools by running setup-* actions during image build. |
| 10 | +# Uses the runner's own Node.js runtime (from /actions-runner/externals/). |
| 11 | + |
| 12 | +TOOL_CACHE="/opt/hostedtoolcache" |
| 13 | +export RUNNER_TOOL_CACHE="$TOOL_CACHE" |
| 14 | +export AGENT_TOOLSDIRECTORY="$TOOL_CACHE" |
| 15 | + |
| 16 | +# Find the Node.js runtime installed by the GitHub Actions runner |
| 17 | +NODE_DIR=$(find /actions-runner/externals -name "node*" -type d | head -1) |
| 18 | +if [ -z "$NODE_DIR" ]; then |
| 19 | + echo "⚠ Warning: Node.js not found in /actions-runner/externals/, skipping tool cache" |
| 20 | + exit 0 |
| 21 | +fi |
| 22 | + |
| 23 | +NODE_BIN="$NODE_DIR/bin/node" |
| 24 | +NPM_BIN="$NODE_DIR/bin/npm" |
| 25 | + |
| 26 | +if [ ! -f "$NODE_BIN" ]; then |
| 27 | + echo "⚠ Warning: Node binary not found at $NODE_BIN, skipping tool cache" |
| 28 | + exit 0 |
| 29 | +fi |
| 30 | + |
| 31 | +echo "Using Node.js from: $NODE_BIN" |
| 32 | +echo "Node version: $($NODE_BIN --version)" |
| 33 | + |
| 34 | +# Helper function to cache a setup action's tool |
| 35 | +# Usage: cache_tool action_owner action_repo action_ref |
| 36 | +cache_tool() { |
| 37 | + local owner=$1 |
| 38 | + local repo=$2 |
| 39 | + local ref=$3 |
| 40 | + |
| 41 | + echo "Setting up ${owner}/${repo}@${ref}..." |
| 42 | + |
| 43 | + local temp_dir=$(mktemp -d) |
| 44 | + cd "$temp_dir" |
| 45 | + |
| 46 | + # Clone the setup action |
| 47 | + if ! git clone --depth=1 --branch "$ref" "https://github.com/${owner}/${repo}.git" action 2>/dev/null; then |
| 48 | + echo " ⚠ Warning: Failed to clone ${owner}/${repo}@${ref}, skipping" |
| 49 | + cd / && rm -rf "$temp_dir" |
| 50 | + return 0 |
| 51 | + fi |
| 52 | + |
| 53 | + cd action |
| 54 | + |
| 55 | + # Install action dependencies using runner's npm |
| 56 | + echo " Installing action dependencies..." |
| 57 | + if ! "$NPM_BIN" install --production --silent 2>&1 | grep -v "^npm notice"; then |
| 58 | + echo " ⚠ Warning: npm install failed for ${owner}/${repo}, skipping" |
| 59 | + cd / && rm -rf "$temp_dir" |
| 60 | + return 0 |
| 61 | + fi |
| 62 | + |
| 63 | + # Clean up |
| 64 | + cd / |
| 65 | + rm -rf "$temp_dir" |
| 66 | + |
| 67 | + echo " ✓ Prepared ${owner}/${repo}@${ref}" |
| 68 | +} |
| 69 | + |
| 70 | +# Note: Pre-caching tools requires running the setup-* actions which download large files |
| 71 | +# This significantly increases image build time (~10-20 minutes) and size (~1-2 GB) |
| 72 | +# For now, we'll skip actual tool installation to keep builds fast |
| 73 | +# Tools will be downloaded on first use and cached in persistent runner-data/toolcache |
| 74 | + |
| 75 | +echo "Tool cache directory: $TOOL_CACHE" |
| 76 | +echo "Note: Tools will be cached on first use in persistent runner-data/toolcache/" |
| 77 | +echo "Skipping pre-installation to keep image size reasonable." |
| 78 | + |
| 79 | +# If you want to pre-install tools (increases build time significantly): |
| 80 | +# cache_tool actions setup-java v4 |
| 81 | +# export INPUT_DISTRIBUTION="temurin" |
| 82 | +# export INPUT_JAVA_VERSION="8.0.442" |
| 83 | +# "$NODE_BIN" action/dist/setup/index.js |
| 84 | +# (repeat for other versions and tools) |
| 85 | + |
| 86 | +# Set ownership |
| 87 | +chown -R runner:runner "$TOOL_CACHE" |
| 88 | + |
| 89 | +echo "✓ Tool cache setup complete" |
0 commit comments