@@ -31,6 +31,10 @@ inputs:
3131 runner-os :
3232 description : " Runner OS for cache key (e.g., ubuntu-latest, windows-latest)"
3333 required : true
34+ use-local :
35+ description : " Build from local source instead of downloading release"
36+ required : false
37+ default : ' false'
3438
3539outputs :
3640 cache-hit :
@@ -48,37 +52,65 @@ runs:
4852 # --------------------------------------------------------------------
4953 - name : 💾 Restore magex binary cache
5054 id : magex-cache
55+ if : inputs.use-local != 'true'
5156 uses : actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
5257 with :
5358 path : |
5459 ~/.cache/magex-bin
5560 key : ${{ inputs.runner-os }}-magex-${{ inputs.magex-version }}
5661
5762 # --------------------------------------------------------------------
58- # Make cached magex usable by copying to GOPATH/bin and adding to PATH
63+ # Install cached binary to PATH when remote cache hits
5964 # --------------------------------------------------------------------
60- - name : 🛠️ Make cached magex usable
65+ - name : 📦 Install cached MAGE-X to PATH (remote mode)
66+ if : inputs.use-local != 'true' && steps.magex-cache.outputs.cache-hit == 'true'
6167 shell : bash
6268 run : |
63- set -euo pipefail
64- BIN_DIR="$HOME/.cache/magex-bin"
65- MAGEX_BIN="$BIN_DIR/magex"
66-
67- # If we restored a cache, copy/link it into GOPATH/bin.
68- if [[ -f "$MAGEX_BIN" ]]; then
69- echo "✅ Using cached magex binary"
70- mkdir -p "$(go env GOPATH)/bin"
71- cp "$MAGEX_BIN" "$(go env GOPATH)/bin/"
72- fi
69+ echo "📦 Installing cached MAGE-X binary to PATH..."
7370
74- # Make sure the binary location is on PATH for *all* subsequent steps.
71+ # Copy cached binary to GOPATH and add to PATH
72+ mkdir -p "$(go env GOPATH)/bin"
73+ cp ~/.cache/magex-bin/magex "$(go env GOPATH)/bin/magex"
74+ chmod +x "$(go env GOPATH)/bin/magex"
7575 echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH"
7676
77+ echo "✅ Cached MAGE-X binary installed to PATH"
78+
7779 # --------------------------------------------------------------------
78- # Detect platform and download MAGE-X binary *only* when the cache was empty.
80+ # Restore local build cache (commit-specific for local builds)
7981 # --------------------------------------------------------------------
80- - name : ✅ Download MAGE-X binary (cache miss)
81- if : steps.magex-cache.outputs.cache-hit != 'true'
82+ - name : 💾 Restore magex binary cache (local)
83+ id : magex-local-cache
84+ if : inputs.use-local == 'true'
85+ uses : actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
86+ with :
87+ path : |
88+ ~/.cache/magex-local
89+ key : ${{ inputs.runner-os }}-local-magex-${{ github.sha }}
90+ # No restore-keys: local builds are commit-specific only to prevent stale cache issues
91+
92+ # --------------------------------------------------------------------
93+ # Install cached binary to PATH when local cache hits
94+ # --------------------------------------------------------------------
95+ - name : 📦 Install cached MAGE-X to PATH (local mode)
96+ if : inputs.use-local == 'true' && steps.magex-local-cache.outputs.cache-hit == 'true'
97+ shell : bash
98+ run : |
99+ echo "📦 Installing cached MAGE-X binary to PATH..."
100+
101+ # Copy cached binary to GOPATH and add to PATH
102+ mkdir -p "$(go env GOPATH)/bin"
103+ cp ~/.cache/magex-local/magex "$(go env GOPATH)/bin/magex"
104+ chmod +x "$(go env GOPATH)/bin/magex"
105+ echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH"
106+
107+ echo "✅ Cached MAGE-X binary installed to PATH"
108+
109+ # --------------------------------------------------------------------
110+ # Download MAGE-X binary for remote mode when cache misses
111+ # --------------------------------------------------------------------
112+ - name : ✅ Download MAGE-X binary (remote mode only)
113+ if : inputs.use-local != 'true' && steps.magex-cache.outputs.cache-hit != 'true'
82114 shell : bash
83115 run : |
84116 echo "⬇️ Cache miss – downloading MAGE-X binary..."
@@ -142,21 +174,60 @@ runs:
142174
143175 echo "✅ Found magex binary at: $MAGEX_BINARY"
144176
145- # Make it executable and copy to GOPATH/bin
177+ # Make it executable and copy to cache directory
146178 chmod +x "$MAGEX_BINARY"
147- mkdir -p "$(go env GOPATH)/bin"
148- cp "$MAGEX_BINARY" "$(go env GOPATH)/bin/magex"
149-
150- # Copy to cache directory for future runs
151179 mkdir -p ~/.cache/magex-bin
152180 cp "$MAGEX_BINARY" ~/.cache/magex-bin/magex
153181
182+ # Copy to GOPATH and add to PATH for subsequent steps
183+ mkdir -p "$(go env GOPATH)/bin"
184+ cp ~/.cache/magex-bin/magex "$(go env GOPATH)/bin/magex"
185+ echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH"
186+
154187 # Cleanup
155188 cd /
156189 rm -rf "$TEMP_DIR"
157190
158191 echo "✅ MAGE-X binary downloaded and cached"
159192
193+ # --------------------------------------------------------------------
194+ # Build MAGE-X from local source (when use-local is true)
195+ # --------------------------------------------------------------------
196+ - name : 🔨 Build MAGE-X from local source
197+ id : build-local
198+ if : inputs.use-local == 'true' && steps.magex-local-cache.outputs.cache-hit != 'true'
199+ shell : bash
200+ run : |
201+ echo "📦 Building local development version of MAGE-X"
202+ cd "$GITHUB_WORKSPACE"
203+
204+ # Check if source directory exists (we're in mage-x repo with full checkout)
205+ if [ ! -d "./cmd/magex" ]; then
206+ echo "❌ ERROR: ./cmd/magex directory not found"
207+ echo "❌ use-local=true requires mage-x repository with full checkout"
208+ echo "❌ Either set use-local=false or ensure full repository checkout"
209+ exit 1
210+ fi
211+
212+ # Build from local source
213+ echo "🔨 Building magex from ./cmd/magex..."
214+ go build -v -o /tmp/magex ./cmd/magex
215+ chmod +x /tmp/magex
216+
217+ # Show version for debugging
218+ /tmp/magex --version || echo "⚠️ Version check skipped"
219+
220+ # Copy to local cache for future runs
221+ mkdir -p ~/.cache/magex-local
222+ cp /tmp/magex ~/.cache/magex-local/magex
223+
224+ # Add magex to PATH for subsequent steps
225+ mkdir -p "$(go env GOPATH)/bin"
226+ cp ~/.cache/magex-local/magex "$(go env GOPATH)/bin/magex"
227+ echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH"
228+
229+ echo "✅ MAGE-X built from local source and cached"
230+
160231 # --------------------------------------------------------------------
161232 # Verify MAGE-X installation and set outputs
162233 # --------------------------------------------------------------------
@@ -167,19 +238,29 @@ runs:
167238 echo "🔍 Verifying MAGE-X installation..."
168239
169240 # Test that magex is available and working
170- if command -v magex >/dev/null 2>&1; then
171- MAGEX_VERSION=$(magex --version 2>/dev/null | grep -E '^\s+Version:' | awk '{print $2}' || echo "unknown")
172- echo "✅ MAGE-X is available: $MAGEX_VERSION"
241+ if ! command -v magex >/dev/null 2>&1; then
242+ echo "❌ ERROR: MAGE-X is not available in PATH" >&2
243+ exit 1
244+ fi
173245
174- # Determine installation method
175- if [[ "${{ steps.magex-cache.outputs.cache-hit }}" == "true" ]]; then
246+ MAGEX_VERSION=$(magex --version 2>/dev/null | grep -E '^\s+Version:' | awk '{print $2}' || echo "unknown")
247+ echo "✅ MAGE-X $MAGEX_VERSION is available"
248+
249+ # Determine installation method based on mode and cache status
250+ if [[ "${{ inputs.use-local }}" == "true" ]]; then
251+ if [[ "${{ steps.magex-local-cache.outputs.cache-hit }}" == "true" ]]; then
176252 echo "method=cached" >> $GITHUB_OUTPUT
177- echo "📋 Installation method: Restored from cache "
253+ echo "📋 Installation method: Cached (local build) "
178254 else
179- echo "method=fresh" >> $GITHUB_OUTPUT
180- echo "📋 Installation method: Fresh binary download "
255+ echo "method=fresh-build " >> $GITHUB_OUTPUT
256+ echo "📋 Installation method: Fresh build from source "
181257 fi
182258 else
183- echo "❌ ERROR: MAGE-X is not available in PATH" >&2
184- exit 1
259+ if [[ "${{ steps.magex-cache.outputs.cache-hit }}" == "true" ]]; then
260+ echo "method=cached" >> $GITHUB_OUTPUT
261+ echo "📋 Installation method: Cached (remote download)"
262+ else
263+ echo "method=fresh-download" >> $GITHUB_OUTPUT
264+ echo "📋 Installation method: Fresh download from releases"
265+ fi
185266 fi
0 commit comments