-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
74 lines (64 loc) · 2.47 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
name: 'Composer install action'
description: 'Composer install with cache'
inputs:
composer_install_options:
required: false
default: '--prefer-dist --no-interaction'
description: "Options that will be added to the composer install command"
cache_dir:
required: true
description: Directory that will hold the cached data.
hash_prefix:
required: false
default: 1
description: Hash prefix, useful for testing if you want to bust the cache.
outputs:
cache_hash:
description: 'The hash used for caching.'
value: ${{ steps.restore-cache.outputs.cache_hash }}
cache_dir:
description: 'The directory used for caching.'
value: ${{ steps.restore-cache.outputs.cache_dir }}
runs:
using: "composite"
steps:
- name: Create vendor cache folder
shell: bash
run: mkdir -p "${{ inputs.cache_dir }}"
- name: Restore cache if found
id: restore-cache
shell: bash
run: |
HASH="${{ inputs.hash_prefix }}_${{ hashFiles('composer.json', 'composer.lock') }}"
COMPOSER_CACHE="${{ inputs.cache_dir }}/${HASH}.tgz"
echo "cache_hash=$HASH" >> $GITHUB_OUTPUT
echo "cache_dir=${{ inputs.cache_dir }}" >> $GITHUB_OUTPUT
echo "composer_cache=${COMPOSER_CACHE}" >> $GITHUB_OUTPUT
if [ -e "${COMPOSER_CACHE}" ]; then
echo "Cache HIT, attempting to restore vendor folder from cache..."
rm -rf vendor
# Try extracting; if extraction fails, remove the corrupted cache
if ! tar -xpzf "${COMPOSER_CACHE}"; then
echo "Cache extraction failed, removing corrupted cache..."
rm -f "${COMPOSER_CACHE}"
fi
else
echo "Cache MISS"
fi
- name: Composer install
shell: bash
run: |
composer install ${{ inputs.composer_install_options }}
- name: Save cache (and validate)
shell: bash
run: |
# Only create the cache if it doesn't exist or was removed due to corruption
if [ ! -f "${{ steps.restore-cache.outputs.composer_cache }}" ]; then
echo "Creating new cache..."
tar -cpzf ${{ steps.restore-cache.outputs.composer_cache }} vendor
echo "Validating newly created cache tarball..."
if ! tar -tzf ${{ steps.restore-cache.outputs.composer_cache }} > /dev/null; then
echo "New cache tarball is invalid, removing it..."
rm -f ${{ steps.restore-cache.outputs.composer_cache }}
fi
fi