|
| 1 | +#!/bin/bash -ex |
| 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 | +# The actions create the proper directory structure with .complete markers. |
| 11 | +# |
| 12 | +# Tool cache format: /opt/hostedtoolcache/{tool}/{version}/{arch}/.complete |
| 13 | + |
| 14 | +TOOL_CACHE="/opt/hostedtoolcache" |
| 15 | +TEMP_RUNNER="/tmp/runner-toolcache-setup" |
| 16 | + |
| 17 | +# Create a temporary runner environment to run setup actions |
| 18 | +mkdir -p "$TEMP_RUNNER" |
| 19 | +cd "$TEMP_RUNNER" |
| 20 | + |
| 21 | +# Download a minimal GitHub Actions runner (just need the node runtime) |
| 22 | +# The setup actions are Node.js scripts that populate the tool cache |
| 23 | +GH_RUNNER_VERSION="2.329.0" |
| 24 | +ARCH="x64" # Could be arm64 for ARM builds |
| 25 | + |
| 26 | +curl -sL "https://github.com/actions/runner/releases/download/v${GH_RUNNER_VERSION}/actions-runner-linux-${ARCH}-${GH_RUNNER_VERSION}.tar.gz" | tar xz |
| 27 | + |
| 28 | +# Set environment for tool installation |
| 29 | +export RUNNER_TOOL_CACHE="$TOOL_CACHE" |
| 30 | +export AGENT_TOOLSDIRECTORY="$TOOL_CACHE" |
| 31 | + |
| 32 | +echo "Pre-caching tools to: $TOOL_CACHE" |
| 33 | + |
| 34 | +# Download and run setup actions to populate tool cache |
| 35 | +# These actions will install tools into RUNNER_TOOL_CACHE |
| 36 | + |
| 37 | +# Java (using actions/setup-java) |
| 38 | +echo "Caching Java 8.0.442 and 17.0.13..." |
| 39 | +git clone --depth=1 --branch v4 https://github.com/actions/setup-java.git setup-java |
| 40 | +cd setup-java && npm install --production && cd .. |
| 41 | + |
| 42 | +# Run setup-java for each version |
| 43 | +export INPUT_DISTRIBUTION="temurin" |
| 44 | +export INPUT_JAVA_VERSION="8.0.442" |
| 45 | +node setup-java/dist/setup/index.js |
| 46 | + |
| 47 | +export INPUT_JAVA_VERSION="17.0.13" |
| 48 | +node setup-java/dist/setup/index.js |
| 49 | + |
| 50 | +# Python (using actions/setup-python) |
| 51 | +echo "Caching Python 3.9, 3.10, 3.11..." |
| 52 | +git clone --depth=1 --branch v5 https://github.com/actions/setup-python.git setup-python |
| 53 | +cd setup-python && npm install --production && cd .. |
| 54 | + |
| 55 | +for version in "3.9" "3.10" "3.11"; do |
| 56 | + export INPUT_PYTHON_VERSION="$version" |
| 57 | + node setup-python/dist/setup/index.js || echo "Warning: Python $version cache may be incomplete" |
| 58 | +done |
| 59 | + |
| 60 | +# Node.js (using actions/setup-node) |
| 61 | +echo "Caching Node.js LTS (20.x)..." |
| 62 | +git clone --depth=1 --branch v4 https://github.com/actions/setup-node.git setup-node |
| 63 | +cd setup-node && npm install --production && cd .. |
| 64 | + |
| 65 | +export INPUT_NODE_VERSION="20" |
| 66 | +node setup-node/dist/setup/index.js |
| 67 | + |
| 68 | +# Clean up |
| 69 | +cd / |
| 70 | +rm -rf "$TEMP_RUNNER" |
| 71 | + |
| 72 | +# Set ownership |
| 73 | +chown -R runner:runner "$TOOL_CACHE" |
| 74 | + |
| 75 | +# Create .complete markers if missing (indicates tool is ready) |
| 76 | +find "$TOOL_CACHE" -type d -mindepth 3 -maxdepth 3 | while read dir; do |
| 77 | + [[ ! -f "$dir/.complete" ]] && touch "$dir/.complete" |
| 78 | +done |
| 79 | + |
| 80 | +echo "" |
| 81 | +echo "✓ Tool cache complete" |
| 82 | +du -sh "$TOOL_CACHE" |
| 83 | +echo "Cached tools:" |
| 84 | +find "$TOOL_CACHE" -name ".complete" | sed 's|/opt/hostedtoolcache/||; s|/.complete||' |
0 commit comments