-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
113 lines (93 loc) · 3.58 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
name: 'Pull Docker image'
description: 'Pull Docker image from registry, or load it from cache.'
inputs:
image:
description: 'Image name'
required: true
cache-directory:
description: 'Directory to store cache in'
required: false
default: '/tmp/cache/docker'
outputs:
image:
description: 'Image name'
value: ${{ inputs.image }}
runs:
using: composite
steps:
- name: 'Prepare'
id: prepare
env:
IMAGE: ${{ inputs.image }}
CACHE_DIR: ${{ inputs.cache-directory }}
shell: bash
#language=bash
run: |
hash=$(echo "$IMAGE" | md5sum | cut -d ' ' -f 1)
echo "tag=$(echo "$IMAGE" | cut -d ':' -f 2)" >> $GITHUB_OUTPUT
echo "image-name=$(echo "$IMAGE" | cut -d ':' -f 1)" >> $GITHUB_OUTPUT
echo "filename=image-$hash.tar" >> $GITHUB_OUTPUT
- uses: actions/cache@v4
id: cache
with:
path: ${{ inputs.cache-directory }}
key: ${{ format('{0}{1}-docker-image-{2}-{3}', runner.os, runner.arch, steps.prepare.outputs.image-name, steps.prepare.outputs.tag) }}
restore-keys: |
${{ format('{0}{1}-docker-image-{2}', runner.os, runner.arch, steps.prepare.outputs.image-name) }}
- name: 'Get Docker image'
env:
CACHE_HIT: ${{ steps.cache.outputs.cache-hit }}
CACHE_DIR: ${{ inputs.cache-directory }}
FILENAME: ${{ steps.prepare.outputs.filename }}
IMAGE: ${{ inputs.image }}
DEBUG: ${{ runner.debug == '1' && 'true' || '' }}
shell: bash
#language=bash
run: |
echo "::group::Get Docker image"
saveCache=$([[ "$CACHE_HIT" == 'true' ]] && echo 'false' || echo 'true')
if [ ! -d "$CACHE_DIR" ]; then
echo "Creating cache directory: $CACHE_DIR"
mkdir -p "$CACHE_DIR"
fi
if [[ "$DEBUG" == 'true' ]]; then
echo "::debug::Cache directory contents:"
ls -lah "$CACHE_DIR"
fi
if ! docker image inspect "$IMAGE" > /dev/null 2>&1; then
if [[ "$CACHE_HIT" == 'true' ]]; then
echo "Loading image from cache..."
if ! docker load -i "$CACHE_DIR/$FILENAME"; then
echo "::warning::Failed to load image from cache, falling back to registry pull."
saveCache='true'
fi
fi
if ! docker image inspect "$IMAGE" > /dev/null 2>&1; then
echo "Image not found locally, pulling from registry..."
if ! docker pull "$IMAGE"; then
echo "::warning::Pull failed. Attempting without cache..."
docker pull --no-cache "$IMAGE" || { echo "::error::Failed to pull image. Exiting."; exit 1; }
fi
fi
else
echo "Image $IMAGE is already present locally."
fi
if [[ "$saveCache" == 'false' ]]; then
echo "::endgroup::"
exit 0
fi
echo "Saving image to cache..."
if [ -f "$CACHE_DIR/$FILENAME" ]; then
echo "Removing existing cached file: $CACHE_DIR/$FILENAME"
rm -f "$CACHE_DIR/$FILENAME"
fi
if ! docker save -o "$CACHE_DIR/$FILENAME" "$IMAGE"; then
echo "::warning::Saving image failed. Retrying..."
docker save -o "$CACHE_DIR/$FILENAME" "$IMAGE" || { echo "::error::Failed to save image. Exiting."; exit 1; }
fi
echo "Image $IMAGE saved as $FILENAME to $CACHE_DIR."
if [[ "$DEBUG" == 'true' ]]; then
echo "::debug::New cache directory contents:"
ls -lah "$CACHE_DIR"
fi
echo "::endgroup::"