|
| 1 | +# ------------------------------------------------------------------------------------ |
| 2 | +# Cache Redis Image Composite Action (GoFortress) |
| 3 | +# |
| 4 | +# Purpose: Cache Redis Docker images to accelerate service container startup. |
| 5 | +# Provides intelligent caching similar to Go module and build caching, pinned to |
| 6 | +# Redis version for proper cache invalidation. |
| 7 | +# |
| 8 | +# Features: |
| 9 | +# - Docker image caching using actions/cache |
| 10 | +# - Version-pinned cache keys tied to REDIS_VERSION |
| 11 | +# - Cross-platform image caching support |
| 12 | +# - Performance tracking and metrics |
| 13 | +# - Automatic fallback to Docker Hub on cache miss |
| 14 | +# - Cache compression for storage efficiency |
| 15 | +# |
| 16 | +# Usage: |
| 17 | +# - uses: ./.github/actions/cache-redis-image |
| 18 | +# with: |
| 19 | +# redis-version: ${{ env.REDIS_VERSION }} |
| 20 | +# runner-os: ${{ runner.os }} |
| 21 | +# cache-mode: "restore-save" # restore, save, restore-save |
| 22 | +# |
| 23 | +# Maintainer: @mrz1836 |
| 24 | +# |
| 25 | +# ------------------------------------------------------------------------------------ |
| 26 | + |
| 27 | +name: "Cache Redis Image" |
| 28 | +description: "Cache Redis Docker images for faster service container startup with version-pinned keys" |
| 29 | + |
| 30 | +inputs: |
| 31 | + redis-version: |
| 32 | + description: "Redis Docker image version (e.g., 7-alpine, 6-alpine)" |
| 33 | + required: true |
| 34 | + runner-os: |
| 35 | + description: "Operating system for cache keys (e.g., Linux, macOS)" |
| 36 | + required: true |
| 37 | + cache-mode: |
| 38 | + description: "Cache operation mode: restore, save, restore-save" |
| 39 | + required: false |
| 40 | + default: "restore-save" |
| 41 | + force-pull: |
| 42 | + description: "Force pull image even if cache exists (for cache warming)" |
| 43 | + required: false |
| 44 | + default: "false" |
| 45 | + |
| 46 | +outputs: |
| 47 | + cache-hit: |
| 48 | + description: "Whether Redis image was restored from cache (true/false)" |
| 49 | + value: ${{ steps.restore-redis-image.outputs.cache-hit }} |
| 50 | + image-size: |
| 51 | + description: "Size of Redis image in MB" |
| 52 | + value: ${{ steps.image-info.outputs.image-size }} |
| 53 | + operation-time: |
| 54 | + description: "Total operation time in seconds" |
| 55 | + value: ${{ steps.operation-summary.outputs.operation-time }} |
| 56 | + cache-key: |
| 57 | + description: "Cache key used for Redis image" |
| 58 | + value: ${{ steps.cache-config.outputs.cache-key }} |
| 59 | + image-available: |
| 60 | + description: "Whether Redis image is available locally (true/false)" |
| 61 | + value: ${{ steps.image-verification.outputs.image-available }} |
| 62 | + |
| 63 | +runs: |
| 64 | + using: "composite" |
| 65 | + steps: |
| 66 | + # ———————————————————————————————————————————————————————————————— |
| 67 | + # Initialize operation tracking |
| 68 | + # ———————————————————————————————————————————————————————————————— |
| 69 | + - name: ⏱️ Initialize operation tracking |
| 70 | + id: operation-start |
| 71 | + shell: bash |
| 72 | + run: | |
| 73 | + echo "🗄️ Starting Redis image caching operation..." |
| 74 | + OPERATION_START=$(date +%s) |
| 75 | + echo "operation-start=$OPERATION_START" >> $GITHUB_OUTPUT |
| 76 | + echo "📋 Configuration:" |
| 77 | + echo " • Redis Version: ${{ inputs.redis-version }}" |
| 78 | + echo " • Runner OS: ${{ inputs.runner-os }}" |
| 79 | + echo " • Cache Mode: ${{ inputs.cache-mode }}" |
| 80 | + echo " • Force Pull: ${{ inputs.force-pull }}" |
| 81 | + echo "" |
| 82 | +
|
| 83 | + # ———————————————————————————————————————————————————————————————— |
| 84 | + # Configure cache settings and keys |
| 85 | + # ———————————————————————————————————————————————————————————————— |
| 86 | + - name: 🔧 Configure cache settings |
| 87 | + id: cache-config |
| 88 | + shell: bash |
| 89 | + run: | |
| 90 | + echo "🔧 Configuring Redis image cache settings..." |
| 91 | +
|
| 92 | + # Generate cache key pinned to Redis version and OS |
| 93 | + REDIS_VERSION="${{ inputs.redis-version }}" |
| 94 | + RUNNER_OS="${{ inputs.runner-os }}" |
| 95 | +
|
| 96 | + # Normalize Redis version for cache key (remove special characters) |
| 97 | + NORMALIZED_VERSION=$(echo "$REDIS_VERSION" | sed 's/[^a-zA-Z0-9.-]/_/g') |
| 98 | +
|
| 99 | + # Create cache key |
| 100 | + CACHE_KEY="redis-image-${RUNNER_OS}-${NORMALIZED_VERSION}" |
| 101 | +
|
| 102 | + # Define cache paths |
| 103 | + CACHE_DIR="$HOME/.cache/redis-images" |
| 104 | + IMAGE_TAR="redis-${NORMALIZED_VERSION}.tar" |
| 105 | + CACHE_PATH="${CACHE_DIR}/${IMAGE_TAR}" |
| 106 | +
|
| 107 | + echo "📋 Cache Configuration:" |
| 108 | + echo " • Cache Key: $CACHE_KEY" |
| 109 | + echo " • Cache Directory: $CACHE_DIR" |
| 110 | + echo " • Image Tar: $IMAGE_TAR" |
| 111 | + echo " • Cache Path: $CACHE_PATH" |
| 112 | +
|
| 113 | + # Create cache directory |
| 114 | + mkdir -p "$CACHE_DIR" |
| 115 | +
|
| 116 | + # Set outputs |
| 117 | + echo "cache-key=$CACHE_KEY" >> $GITHUB_OUTPUT |
| 118 | + echo "cache-dir=$CACHE_DIR" >> $GITHUB_OUTPUT |
| 119 | + echo "image-tar=$IMAGE_TAR" >> $GITHUB_OUTPUT |
| 120 | + echo "cache-path=$CACHE_PATH" >> $GITHUB_OUTPUT |
| 121 | + echo "normalized-version=$NORMALIZED_VERSION" >> $GITHUB_OUTPUT |
| 122 | +
|
| 123 | + # ———————————————————————————————————————————————————————————————— |
| 124 | + # Restore Redis image from cache |
| 125 | + # ———————————————————————————————————————————————————————————————— |
| 126 | + - name: 💾 Restore Redis image from cache |
| 127 | + if: contains(inputs.cache-mode, 'restore') |
| 128 | + id: restore-redis-image |
| 129 | + uses: actions/cache/restore@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4 |
| 130 | + with: |
| 131 | + path: ${{ steps.cache-config.outputs.cache-path }} |
| 132 | + key: ${{ steps.cache-config.outputs.cache-key }} |
| 133 | + |
| 134 | + # ———————————————————————————————————————————————————————————————— |
| 135 | + # Load cached Redis image into Docker |
| 136 | + # ———————————————————————————————————————————————————————————————— |
| 137 | + - name: 📦 Load cached Redis image |
| 138 | + if: contains(inputs.cache-mode, 'restore') && steps.restore-redis-image.outputs.cache-hit == 'true' && inputs.force-pull != 'true' |
| 139 | + id: load-cached-image |
| 140 | + shell: bash |
| 141 | + run: | |
| 142 | + echo "📦 Loading Redis image from cache..." |
| 143 | + CACHE_PATH="${{ steps.cache-config.outputs.cache-path }}" |
| 144 | +
|
| 145 | + if [ -f "$CACHE_PATH" ]; then |
| 146 | + echo "✅ Cache file found: $CACHE_PATH" |
| 147 | +
|
| 148 | + # Load image from tar file |
| 149 | + if docker load < "$CACHE_PATH"; then |
| 150 | + echo "✅ Redis image loaded successfully from cache" |
| 151 | + echo "image-loaded=true" >> $GITHUB_OUTPUT |
| 152 | + else |
| 153 | + echo "❌ Failed to load Redis image from cache" |
| 154 | + echo "image-loaded=false" >> $GITHUB_OUTPUT |
| 155 | + exit 1 |
| 156 | + fi |
| 157 | + else |
| 158 | + echo "❌ Cache file not found: $CACHE_PATH" |
| 159 | + echo "image-loaded=false" >> $GITHUB_OUTPUT |
| 160 | + exit 1 |
| 161 | + fi |
| 162 | +
|
| 163 | + # ———————————————————————————————————————————————————————————————— |
| 164 | + # Pull Redis image if not cached or force-pull enabled |
| 165 | + # ———————————————————————————————————————————————————————————————— |
| 166 | + - name: 📥 Pull Redis image from Docker Hub |
| 167 | + if: (contains(inputs.cache-mode, 'restore') && steps.restore-redis-image.outputs.cache-hit != 'true') || inputs.force-pull == 'true' |
| 168 | + id: pull-redis-image |
| 169 | + shell: bash |
| 170 | + run: | |
| 171 | + echo "📥 Pulling Redis image from Docker Hub..." |
| 172 | + REDIS_IMAGE="redis:${{ inputs.redis-version }}" |
| 173 | +
|
| 174 | + echo "🔍 Pulling image: $REDIS_IMAGE" |
| 175 | +
|
| 176 | + # Pull the image with progress |
| 177 | + if docker pull "$REDIS_IMAGE"; then |
| 178 | + echo "✅ Redis image pulled successfully: $REDIS_IMAGE" |
| 179 | + echo "image-pulled=true" >> $GITHUB_OUTPUT |
| 180 | + else |
| 181 | + echo "❌ Failed to pull Redis image: $REDIS_IMAGE" |
| 182 | + echo "image-pulled=false" >> $GITHUB_OUTPUT |
| 183 | + exit 1 |
| 184 | + fi |
| 185 | +
|
| 186 | + # ———————————————————————————————————————————————————————————————— |
| 187 | + # Save Redis image to cache |
| 188 | + # ———————————————————————————————————————————————————————————————— |
| 189 | + - name: 💾 Save Redis image to cache |
| 190 | + if: contains(inputs.cache-mode, 'save') && (steps.pull-redis-image.outputs.image-pulled == 'true' || inputs.force-pull == 'true') |
| 191 | + id: save-redis-image |
| 192 | + shell: bash |
| 193 | + run: | |
| 194 | + echo "💾 Saving Redis image to cache..." |
| 195 | + REDIS_IMAGE="redis:${{ inputs.redis-version }}" |
| 196 | + CACHE_PATH="${{ steps.cache-config.outputs.cache-path }}" |
| 197 | +
|
| 198 | + echo "🔍 Saving image: $REDIS_IMAGE" |
| 199 | + echo "📁 Cache path: $CACHE_PATH" |
| 200 | +
|
| 201 | + # Save image as tar file |
| 202 | + if docker save "$REDIS_IMAGE" | gzip > "$CACHE_PATH"; then |
| 203 | + echo "✅ Redis image saved to cache successfully" |
| 204 | +
|
| 205 | + # Get file size |
| 206 | + if [ -f "$CACHE_PATH" ]; then |
| 207 | + FILE_SIZE_BYTES=$(stat -c%s "$CACHE_PATH" 2>/dev/null || stat -f%z "$CACHE_PATH" 2>/dev/null || echo "0") |
| 208 | + FILE_SIZE_MB=$((FILE_SIZE_BYTES / 1024 / 1024)) |
| 209 | + echo "📊 Cache file size: ${FILE_SIZE_MB}MB" |
| 210 | + echo "cache-size-mb=$FILE_SIZE_MB" >> $GITHUB_OUTPUT |
| 211 | + fi |
| 212 | +
|
| 213 | + echo "image-saved=true" >> $GITHUB_OUTPUT |
| 214 | + else |
| 215 | + echo "❌ Failed to save Redis image to cache" |
| 216 | + echo "image-saved=false" >> $GITHUB_OUTPUT |
| 217 | + exit 1 |
| 218 | + fi |
| 219 | +
|
| 220 | + # ———————————————————————————————————————————————————————————————— |
| 221 | + # Save cache using actions/cache |
| 222 | + # ———————————————————————————————————————————————————————————————— |
| 223 | + - name: 🗄️ Save Redis image cache |
| 224 | + if: contains(inputs.cache-mode, 'save') && steps.save-redis-image.outputs.image-saved == 'true' |
| 225 | + uses: actions/cache/save@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4 |
| 226 | + with: |
| 227 | + path: ${{ steps.cache-config.outputs.cache-path }} |
| 228 | + key: ${{ steps.cache-config.outputs.cache-key }} |
| 229 | + |
| 230 | + # ———————————————————————————————————————————————————————————————— |
| 231 | + # Verify Redis image availability |
| 232 | + # ———————————————————————————————————————————————————————————————— |
| 233 | + - name: 🔍 Verify Redis image availability |
| 234 | + id: image-verification |
| 235 | + shell: bash |
| 236 | + run: | |
| 237 | + echo "🔍 Verifying Redis image availability..." |
| 238 | + REDIS_IMAGE="redis:${{ inputs.redis-version }}" |
| 239 | +
|
| 240 | + # Check if image exists locally |
| 241 | + if docker image inspect "$REDIS_IMAGE" >/dev/null 2>&1; then |
| 242 | + echo "✅ Redis image available locally: $REDIS_IMAGE" |
| 243 | + echo "image-available=true" >> $GITHUB_OUTPUT |
| 244 | + else |
| 245 | + echo "❌ Redis image not available locally: $REDIS_IMAGE" |
| 246 | + echo "image-available=false" >> $GITHUB_OUTPUT |
| 247 | + exit 1 |
| 248 | + fi |
| 249 | +
|
| 250 | + # ———————————————————————————————————————————————————————————————— |
| 251 | + # Gather image information and metrics |
| 252 | + # ———————————————————————————————————————————————————————————————— |
| 253 | + - name: 📊 Gather image information |
| 254 | + id: image-info |
| 255 | + shell: bash |
| 256 | + run: | |
| 257 | + echo "📊 Gathering Redis image information..." |
| 258 | + REDIS_IMAGE="redis:${{ inputs.redis-version }}" |
| 259 | +
|
| 260 | + # Get image size |
| 261 | + IMAGE_SIZE_BYTES=$(docker image inspect "$REDIS_IMAGE" --format='{{.Size}}' 2>/dev/null || echo "0") |
| 262 | + IMAGE_SIZE_MB=$((IMAGE_SIZE_BYTES / 1024 / 1024)) |
| 263 | +
|
| 264 | + # Get image ID and created date |
| 265 | + IMAGE_ID=$(docker image inspect "$REDIS_IMAGE" --format='{{.Id}}' 2>/dev/null | cut -d: -f2 | head -c 12) |
| 266 | + IMAGE_CREATED=$(docker image inspect "$REDIS_IMAGE" --format='{{.Created}}' 2>/dev/null || echo "unknown") |
| 267 | +
|
| 268 | + echo "📋 Image Information:" |
| 269 | + echo " • Image: $REDIS_IMAGE" |
| 270 | + echo " • Size: ${IMAGE_SIZE_MB}MB" |
| 271 | + echo " • ID: $IMAGE_ID" |
| 272 | + echo " • Created: $IMAGE_CREATED" |
| 273 | +
|
| 274 | + # Set outputs |
| 275 | + echo "image-size=$IMAGE_SIZE_MB" >> $GITHUB_OUTPUT |
| 276 | + echo "image-id=$IMAGE_ID" >> $GITHUB_OUTPUT |
| 277 | + echo "image-created=$IMAGE_CREATED" >> $GITHUB_OUTPUT |
| 278 | +
|
| 279 | + # ———————————————————————————————————————————————————————————————— |
| 280 | + # Operation summary and timing |
| 281 | + # ———————————————————————————————————————————————————————————————— |
| 282 | + - name: ✅ Operation summary |
| 283 | + id: operation-summary |
| 284 | + shell: bash |
| 285 | + run: | |
| 286 | + echo "✅ Redis image caching operation completed" |
| 287 | +
|
| 288 | + # Calculate operation time |
| 289 | + OPERATION_START="${{ steps.operation-start.outputs.operation-start }}" |
| 290 | + OPERATION_END=$(date +%s) |
| 291 | + OPERATION_TIME=$((OPERATION_END - OPERATION_START)) |
| 292 | +
|
| 293 | + # Determine cache status |
| 294 | + CACHE_HIT="${{ steps.restore-redis-image.outputs.cache-hit || 'false' }}" |
| 295 | + IMAGE_AVAILABLE="${{ steps.image-verification.outputs.image-available }}" |
| 296 | +
|
| 297 | + echo "📊 Operation Summary:" |
| 298 | + echo " • Cache Hit: $CACHE_HIT" |
| 299 | + echo " • Image Available: $IMAGE_AVAILABLE" |
| 300 | + echo " • Operation Time: ${OPERATION_TIME}s" |
| 301 | + echo " • Cache Key: ${{ steps.cache-config.outputs.cache-key }}" |
| 302 | +
|
| 303 | + # Set outputs |
| 304 | + echo "operation-time=$OPERATION_TIME" >> $GITHUB_OUTPUT |
| 305 | + echo "cache-hit-final=$CACHE_HIT" >> $GITHUB_OUTPUT |
| 306 | +
|
| 307 | + if [[ "$IMAGE_AVAILABLE" == "true" ]]; then |
| 308 | + if [[ "$CACHE_HIT" == "true" ]]; then |
| 309 | + echo "🚀 Redis image restored from cache in ${OPERATION_TIME}s" |
| 310 | + else |
| 311 | + echo "📥 Redis image pulled and cached in ${OPERATION_TIME}s" |
| 312 | + fi |
| 313 | + else |
| 314 | + echo "❌ Redis image operation failed after ${OPERATION_TIME}s" |
| 315 | + exit 1 |
| 316 | + fi |
0 commit comments