-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.yml
125 lines (114 loc) · 4.33 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
114
115
116
117
118
119
120
121
122
123
124
125
name: 'Cache Container Images Action'
description: 'Store pulled container images into the local GitHub Actions cache to reduce runtime for subsequent runs.'
author: 'James Mortensen'
branding:
icon: 'briefcase'
color: 'blue'
inputs:
runtime:
description: 'Container runtime engine'
required: false
default: 'podman'
images:
description: 'Container images to pull and cache'
required: true
prefix-key:
description: 'The cache namespace. change the value to force a container image cache miss.'
required: false
default: 'podman-cache'
outputs:
cache-hit:
description: "True if container images were found and pulled from the cache"
value: ${{ steps.container-images-cache.outputs.cache-hit }}
runs:
using: 'composite'
steps:
- name: Validate runner
if: runner.os != 'Linux' && runner.os != 'macOS'
run: |
echo "${RUNNER_OS} is not supported. Only Linux is supported at this time."
exit 1
shell: bash
- name: Validate inputs
if: inputs.runtime != 'podman'
run: |
echo "${{ inputs.runtime }} is not supported. Only podman is supported at this time. If you"
echo "build your containers with Docker and push them to Docker Hub, you can still use podman"
echo "in GitHub Actions, since container images are a standard across different runtime engines."
exit 1
shell: bash
- name: Dump container images to file
shell: bash
env:
CONTAINER_IMAGES: ${{ inputs.images }}
run: |
export IMAGES_URIS=`echo $CONTAINER_IMAGES | sed 's/:/\/tags\//g'`
echo "Images URIS="$IMAGES_URIS
echo $IMAGES_URIS | tr " " "\n" >> container-images-uris.txt
echo $CONTAINER_IMAGES | tr " " "\n" >> container-images.txt
cat container-images-uris.txt
- name: Install required packages
run: pip install requests
shell: bash
- name: Get the last updated times for the container images to build key
env:
CONTAINER_IMAGES_PREFIX_KEY: ${{ inputs.prefix-key }}
run: |
import json, os, requests
def get_last_updated(tag_url):
response = requests.get(tag_url)
if response.status_code == 200:
data = json.loads(response.content)
return data['last_updated']
return ""
with open("container-images-uris.txt") as fp:
lines = fp.readlines()
lines = [os.environ.get('CONTAINER_IMAGES_PREFIX_KEY') + '-' + get_last_updated(f"https://hub.docker.com/v2/repositories/{ln.strip()}/") for ln in lines]
with open("container-images-key.txt", "w") as fp:
fp.write(os.linesep.join(lines))
shell: python
- name: Show update date-times
run: cat container-images-key.txt
shell: bash
- name: Check Container Images Cache
id: get-last-updated
uses: actions/cache@v4
with:
path: cached-container-images.tar
key: ${{ runner.os }}-${{ hashFiles('**/container-images-key.txt') }}
- name: Pull Container Image if cache miss
shell: bash
if: steps.get-last-updated.outputs.cache-hit != 'true'
run: |
echo "Key was = "
cat container-images-key.txt
echo "Since there was a cache miss, we will pull..."
echo "Start pulling images at " `date`
cat container-images.txt | while read line ; do podman pull $line ; done
echo "Done pulling images at " `date`
- name: Archive Container Images if cache miss
shell: bash
if: steps.get-last-updated.outputs.cache-hit != 'true'
run: |
echo "Start archiving images at " `date`
export OLD_PWD=$PWD
cd $HOME
sudo tar cf $OLD_PWD/cached-container-images.tar .local/share/containers/storage/
sudo chown runner:docker $OLD_PWD/cached-container-images.tar
cd $OLD_PWD
echo "Done compression at " `date`
- name: Extract tarball if found in cache
shell: bash
if: steps.get-last-updated.outputs.cache-hit == 'true'
run: |
echo "Start extracting images at " `date`
mkdir -p ~/.local/share/containers/
sudo tar xf cached-container-images.tar -C $HOME
echo "Done extracting images at " `date`
- name: Set cache-hit output
id: container-images-cache
shell: bash
env:
CONTAINER_IMAGES_CACHE_HIT: ${{ steps.get-last-updated.outputs.cache-hit }}
run: |
echo "cache-hit=$CONTAINER_IMAGES_CACHE_HIT" >> $GITHUB_OUTPUT