-
-
Notifications
You must be signed in to change notification settings - Fork 301
187 lines (173 loc) · 6.65 KB
/
Copy pathdocker_apply_cache.yaml
File metadata and controls
187 lines (173 loc) · 6.65 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
name: "03 Maintain: Apply Package Cache"
description: "Build and publish the lesson dependency image after a pull request has been merged or via manual trigger"
on:
workflow_dispatch:
inputs:
name:
description: 'Who triggered this build?'
required: true
default: 'Maintainer (via GitHub)'
force-dependency-image-rebuild:
description: 'Rebuild the dependency image layer even if one already exists?'
required: false
default: false
type: boolean
prune-keep-count:
description: 'How many existing dependency image layers to keep?'
required: false
default: 1
type: number
pull_request:
types:
- closed
branches:
- main
# queue cache runs
concurrency:
group: docker-apply-cache
cancel-in-progress: false
jobs:
preflight:
name: "Preflight: PR or Manual Trigger?"
runs-on: ubuntu-latest
outputs:
do-apply: ${{ steps.check.outputs.merged_or_manual }}
steps:
- name: "Should we run cache application?"
id: check
run: |
if [[ "${{ github.event_name }}" == "workflow_dispatch" ||
("${{ github.ref }}" == "refs/heads/main" && "${{ github.event.action }}" == "closed" && "${{ github.event.pull_request.merged }}" == "true") ]]; then
echo "merged_or_manual=true" >> $GITHUB_OUTPUT
else
echo "This was not a manual trigger and no PR was merged. No action taken."
echo "merged_or_manual=false" >> $GITHUB_OUTPUT
fi
shell: bash
check-renv:
name: "Check If We Need {renv}"
runs-on: ubuntu-latest
needs: preflight
if: needs.preflight.outputs.do-apply == 'true'
outputs:
renv-needed: ${{ steps.check-for-renv.outputs.renv-needed }}
renv-cache-hashsum: ${{ steps.check-for-renv.outputs.renv-cache-hashsum }}
steps:
- name: "Check for renv"
id: check-for-renv
uses: carpentries/actions/renv-checks@v1
with:
WORKBENCH_TAG: ${{ vars.WORKBENCH_TAG || 'latest' }}
skip-cache-check: true
no-renv-cache-used:
name: "No renv package dependency image needed"
runs-on: ubuntu-latest
needs: check-renv
if: needs.check-renv.outputs.renv-needed != 'true'
steps:
- name: "No dependency image needed"
run: echo "No renv dependency image needed for this lesson"
update-renv-cache:
name: "Publish renv package dependency image"
runs-on: ubuntu-latest
needs: check-renv
if: needs.check-renv.outputs.renv-needed == 'true'
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v6
- name: "Get Container Version Used"
id: wb-vers
uses: carpentries/actions/container-version@v1
with:
WORKBENCH_TAG: ${{ vars.WORKBENCH_TAG }}
renv-needed: ${{ needs.check-renv.outputs.renv-needed }}
token: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set dependency image tags
id: image
env:
IMAGE_OWNER: ${{ github.repository_owner }}
IMAGE_NAME: ${{ github.event.repository.name }}
WB_VERSION: ${{ steps.wb-vers.outputs.container-version }}
RENV_HASH: ${{ needs.check-renv.outputs.renv-cache-hashsum }}
run: |
set -euo pipefail
exact_image="ghcr.io/${IMAGE_OWNER}/${IMAGE_NAME}-deps:${WB_VERSION}_renv-${RENV_HASH}"
latest_image="ghcr.io/${IMAGE_OWNER}/${IMAGE_NAME}-deps:latest"
# lowercaseify
echo "exact_image=${exact_image,,}" >> "$GITHUB_OUTPUT"
echo "latest_image=${latest_image,,}" >> "$GITHUB_OUTPUT"
shell: bash
- name: Check for existing dependency image tag
id: image-exists
env:
EXACT_IMAGE: ${{ steps.image.outputs.exact_image }}
run: |
set -euo pipefail
if docker manifest inspect "${EXACT_IMAGE}" >/dev/null 2>&1; then
echo "exists=true" >> "$GITHUB_OUTPUT"
echo "## ⚠️ Dependency image already exists" >> $GITHUB_STEP_SUMMARY
echo "Dependency image already exists for this renv hash: ${EXACT_IMAGE}" >> $GITHUB_STEP_SUMMARY
echo "Dependency image already exists for this renv hash: ${EXACT_IMAGE}"
else
echo "exists=false" >> "$GITHUB_OUTPUT"
echo "No existing dependency image found for this renv hash: ${EXACT_IMAGE}"
fi
shell: bash
- name: Build and push dependency image layer
id: build-push-deps-layer
if: |
steps.image-exists.outputs.exists != 'true' ||
(
github.event_name == 'workflow_dispatch' &&
github.event.inputs.force-dependency-image-rebuild == 'true'
)
uses: carpentries/actions/build-dependency-image@v1
with:
workbench-tag: ${{ vars.WORKBENCH_TAG || 'latest' }}
github-token: ${{ secrets.GITHUB_TOKEN }}
github-repository: ${{ github.repository }}
github-sha: ${{ github.sha }}
exact-image: ${{ steps.image.outputs.exact_image }}
latest-image: ${{ steps.image.outputs.latest_image }}
build-context: ${{ github.workspace }}
prune-dependency-images:
name: "Prune Dependency Images"
runs-on: ubuntu-latest
needs: check-renv
steps:
- name: Prune any old dependency image layers
uses: carpentries/actions/prune-dependency-images@v1
if: needs.check-renv.outputs.renv-needed == 'true'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
owner: ${{ github.repository_owner }}
owner-type: ${{ github.event.repository.owner.type }}
repository: ${{ github.event.repository.name }}
package-name: ${{ github.event.repository.name }}-deps
keep-count: ${{ github.event.inputs.prune-keep-count }}
continue-on-error: true
record-cache-result:
name: "Record Caching Status"
runs-on: ubuntu-latest
needs: [check-renv, update-renv-cache]
if: always()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: "Record cache result"
run: |
echo "${{ needs.check-renv.outputs.renv-needed != 'true' || needs.update-renv-cache.result == 'success' }}" > ${{ github.workspace }}/apply-cache-result
shell: bash
- name: "Upload cache result"
uses: actions/upload-artifact@v7
with:
name: apply-cache-result
path: ${{ github.workspace }}/apply-cache-result