Skip to content

Commit 0206cfe

Browse files
feat: add local cache storage
1 parent daa556e commit 0206cfe

2 files changed

Lines changed: 56 additions & 21 deletions

File tree

action.yml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,18 @@ runs:
7777
elixir-version: ${{ inputs.elixir-version }}
7878
otp-version: ${{ inputs.otp-version }}
7979

80-
- name: Get deps cache
81-
uses: actions/cache@v4
82-
with:
83-
path: deps/
84-
key: deps-${{ inputs.cache-key }}-${{ runner.os }}-${{ hashFiles('**/mix.lock') }}
85-
restore-keys: |
86-
deps-${{ inputs.cache-key }}-${{ runner.os }}-
80+
# Restore cache before building
81+
- name: Restore local cache
82+
shell: bash
83+
run: ${{ github.action_path }}/localcache.sh restore deps-${{ inputs.cache-key }}-${{ runner.os }}-${{ hashFiles('**/mix.lock') }} deps/
8784

8885
- name: Get build cache
89-
uses: actions/cache@v4
90-
id: build-cache
91-
with:
92-
path: _build/${{env.MIX_ENV}}/
93-
key: build-${{ inputs.cache-key }}-${{ runner.os }}-${{ inputs.otp-version }}-${{ inputs.elixir-version }}-${{ env.MIX_ENV }}-${{ hashFiles('**/mix.lock') }}
94-
restore-keys: |
95-
build-${{ inputs.cache-key }}-${{ runner.os }}-${{ inputs.otp-version }}-${{ inputs.elixir-version }}-${{ env.MIX_ENV }}-
86+
shell: bash
87+
run: ${{ github.action_path }}/localcache.sh restore build-${{ inputs.cache-key }}-${{ runner.os }}-${{ inputs.otp-version }}-${{ inputs.elixir-version }}-${{ env.MIX_ENV }}-${{ hashFiles('**/mix.lock') }} _build/${{env.MIX_ENV}}/
9688

9789
- name: Get Hex cache
98-
uses: actions/cache@v4
99-
id: hex-cache
100-
with:
101-
path: ~/.hex
102-
key: build-${{ runner.os }}-${{ inputs.otp-version }}-${{ inputs.elixir-version }}-${{ hashFiles('**/mix.lock') }}
103-
restore-keys: |
104-
build-${{ runner.os }}-${{ inputs.otp-version }}-${{ inputs.elixir-version }}-
90+
shell: bash
91+
run: ${{ github.action_path }}/localcache.sh restore hex-${{ runner.os }}-${{ inputs.otp-version }}-${{ inputs.elixir-version }}-${{ hashFiles('**/mix.lock') }} ~/.hex
10592

10693
# In my experience, I have issues with incremental builds maybe 1 in 100
10794
# times that are fixed by doing a full recompile.
@@ -145,6 +132,19 @@ runs:
145132
shell: sh
146133
if: inputs.build-app == 'true'
147134

135+
# Save cache after the build is done
136+
- name: Save local deps cache
137+
shell: bash
138+
run: ${{ github.action_path }}/localcache.sh save deps-${{ inputs.cache-key }}-${{ runner.os }}-${{ hashFiles('**/mix.lock') }} deps/
139+
140+
- name: Save local build cache
141+
shell: bash
142+
run: ${{ github.action_path }}/localcache.sh save build-${{ inputs.cache-key }}-${{ runner.os }}-${{ inputs.otp-version }}-${{ inputs.elixir-version }}-${{ env.MIX_ENV }}-${{ hashFiles('**/mix.lock') }} _build/${{env.MIX_ENV}}/
143+
144+
- name: Save local Hex cache
145+
shell: bash
146+
run: ${{ github.action_path }}/localcache.sh save hex-${{ runner.os }}-${{ inputs.otp-version }}-${{ inputs.elixir-version }}-${{ hashFiles('**/mix.lock') }} ~/.hex
147+
148148
# Don't cache PLTs based on mix.lock hash, as Dialyzer can incrementally update even old ones
149149
# Cache key based on Elixir & Erlang version (also useful when running in matrix)
150150
- name: Restore PLT cache

localcache.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
MODE=$1 # "save" or "restore"
6+
KEY=$2 # unique key for the cache (e.g. node_modules_hash123)
7+
TARGET=$3 # path to directory to cache (e.g. node_modules/)
8+
CACHE_DIR=/home/runner/cache
9+
10+
if [[ -z "$MODE" || -z "$KEY" || -z "$TARGET" ]]; then
11+
echo "Usage: $0 [save|restore] <key> <target_dir>"
12+
exit 1
13+
fi
14+
15+
CACHE_PATH="$CACHE_DIR/$KEY"
16+
17+
mkdir -p "$CACHE_DIR"
18+
19+
if [ "$MODE" == "restore" ]; then
20+
if [ -d "$CACHE_PATH" ]; then
21+
echo "✅ Restoring cache from $CACHE_PATH$TARGET"
22+
mkdir -p "$TARGET"
23+
cp -r "$CACHE_PATH/"* "$TARGET/" || true
24+
else
25+
echo "⚠️ No cache found for key: $KEY"
26+
fi
27+
28+
elif [ "$MODE" == "save" ]; then
29+
echo "📦 Saving cache from $TARGET$CACHE_PATH"
30+
mkdir -p "$CACHE_PATH"
31+
cp -r "$TARGET/"* "$CACHE_PATH/" || true
32+
else
33+
echo "❌ Invalid mode: $MODE"
34+
exit 1
35+
fi

0 commit comments

Comments
 (0)