|
| 1 | +# ------------------------------------------------------------------------------------ |
| 2 | +# Setup Mage Composite Action (GoFortress) |
| 3 | +# |
| 4 | +# Purpose: Install and cache the mage binary for use in GitHub Actions workflows. |
| 5 | +# Provides efficient caching by OS and version, with automatic binary installation |
| 6 | +# on cache miss and PATH management for seamless integration. |
| 7 | +# |
| 8 | +# Features: |
| 9 | +# - Smart binary caching by OS and version |
| 10 | +# - Automatic go install only on cache miss |
| 11 | +# - PATH management for immediate availability |
| 12 | +# |
| 13 | +# Usage: |
| 14 | +# - uses: ./.github/actions/setup-mage |
| 15 | +# with: |
| 16 | +# mage-version: ${{ env.MAGE_X_MAGE_VERSION }} |
| 17 | +# runner-os: ${{ runner.os }} |
| 18 | +# |
| 19 | +# Maintainer: @mrz1836 |
| 20 | +# |
| 21 | +# ------------------------------------------------------------------------------------ |
| 22 | + |
| 23 | +name: "Setup Mage" |
| 24 | +description: "Install and cache mage binary for magefile execution" |
| 25 | + |
| 26 | +inputs: |
| 27 | + mage-version: |
| 28 | + description: "Mage version to install (e.g., v1.15.0)" |
| 29 | + required: true |
| 30 | + runner-os: |
| 31 | + description: "Runner OS for cache key (e.g., ubuntu-latest)" |
| 32 | + required: true |
| 33 | + |
| 34 | +outputs: |
| 35 | + cache-hit: |
| 36 | + description: "Whether mage was restored from cache (true/false)" |
| 37 | + value: ${{ steps.mage-cache.outputs.cache-hit }} |
| 38 | + installation-method: |
| 39 | + description: "How mage was obtained: cached or fresh" |
| 40 | + value: ${{ steps.installation-summary.outputs.method }} |
| 41 | + |
| 42 | +runs: |
| 43 | + using: "composite" |
| 44 | + steps: |
| 45 | + # -------------------------------------------------------------------- |
| 46 | + # Restore mage binary cache |
| 47 | + # -------------------------------------------------------------------- |
| 48 | + - name: 💾 Restore mage binary cache |
| 49 | + id: mage-cache |
| 50 | + uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4 |
| 51 | + with: |
| 52 | + path: ~/.cache/mage-bin |
| 53 | + key: ${{ inputs.runner-os }}-mage-${{ inputs.mage-version }} |
| 54 | + |
| 55 | + # -------------------------------------------------------------------- |
| 56 | + # Install cached binary to PATH when cache hits |
| 57 | + # -------------------------------------------------------------------- |
| 58 | + - name: 📦 Install cached mage to PATH |
| 59 | + if: steps.mage-cache.outputs.cache-hit == 'true' |
| 60 | + shell: bash |
| 61 | + run: | |
| 62 | + echo "📦 Installing cached mage binary to PATH..." |
| 63 | +
|
| 64 | + # Copy cached binary to GOPATH and add to PATH |
| 65 | + mkdir -p "$(go env GOPATH)/bin" |
| 66 | + cp ~/.cache/mage-bin/mage "$(go env GOPATH)/bin/mage" |
| 67 | + chmod +x "$(go env GOPATH)/bin/mage" |
| 68 | + echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH" |
| 69 | +
|
| 70 | + echo "✅ Cached mage binary installed to PATH" |
| 71 | +
|
| 72 | + # -------------------------------------------------------------------- |
| 73 | + # Install mage via go install when cache misses |
| 74 | + # -------------------------------------------------------------------- |
| 75 | + - name: ⬇️ Install mage (cache miss) |
| 76 | + if: steps.mage-cache.outputs.cache-hit != 'true' |
| 77 | + shell: bash |
| 78 | + run: | |
| 79 | + echo "⬇️ Cache miss – installing mage via go install..." |
| 80 | + echo "📋 Installing mage version: ${{ inputs.mage-version }}" |
| 81 | +
|
| 82 | + # Install mage |
| 83 | + go install "github.com/magefile/mage@${{ inputs.mage-version }}" |
| 84 | +
|
| 85 | + # Cache the binary for future runs |
| 86 | + mkdir -p ~/.cache/mage-bin |
| 87 | + cp "$(go env GOPATH)/bin/mage" ~/.cache/mage-bin/mage |
| 88 | +
|
| 89 | + # Ensure GOPATH/bin is in PATH |
| 90 | + echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH" |
| 91 | +
|
| 92 | + echo "✅ Mage installed and cached" |
| 93 | +
|
| 94 | + # -------------------------------------------------------------------- |
| 95 | + # Verify mage installation and set outputs |
| 96 | + # -------------------------------------------------------------------- |
| 97 | + - name: 🔍 Verify mage installation |
| 98 | + id: installation-summary |
| 99 | + shell: bash |
| 100 | + run: | |
| 101 | + echo "🔍 Verifying mage installation..." |
| 102 | +
|
| 103 | + # Test that mage is available and working |
| 104 | + if ! command -v mage >/dev/null 2>&1; then |
| 105 | + echo "❌ ERROR: mage is not available in PATH" >&2 |
| 106 | + exit 1 |
| 107 | + fi |
| 108 | +
|
| 109 | + # Show version |
| 110 | + MAGE_VERSION=$(mage -version 2>&1 | head -1 || echo "unknown") |
| 111 | + echo "✅ mage $MAGE_VERSION is available" |
| 112 | +
|
| 113 | + # Determine installation method |
| 114 | + if [[ "${{ steps.mage-cache.outputs.cache-hit }}" == "true" ]]; then |
| 115 | + echo "method=cached" >> $GITHUB_OUTPUT |
| 116 | + echo "📋 Installation method: Cached" |
| 117 | + else |
| 118 | + echo "method=fresh" >> $GITHUB_OUTPUT |
| 119 | + echo "📋 Installation method: Fresh install" |
| 120 | + fi |
0 commit comments