Repository navigation
98 lines (94 loc) · 4.65 KB
/
Copy pathmirror.yml
File metadata and controls
98 lines (94 loc) · 4.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
# Copies the images in .github/mirror.txt from Docker Hub to ghcr.io/docspring/visualci/mirror/<name>, keeping their
# digests, so CI pulls them from GitHub's own registry. Runs on main when the list changes, weekly to repair a deleted
# copy, and by hand. Only main runs it; CI pins the copies by digest, so a tag overwritten from a branch changes nothing
# CI runs. The packages are private: jobs log in with GITHUB_TOKEN (packages: read), forks' pull requests included.
name: Mirror base images
on:
push:
branches: [main]
paths: [.github/mirror.txt, .github/workflows/mirror.yml]
schedule:
- cron: "17 3 * * 1"
workflow_dispatch:
permissions:
contents: read
packages: write
concurrency:
group: mirror-base-images
cancel-in-progress: false
jobs:
mirror:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Install crane (go-containerregistry), checksum verified
env:
GH_TOKEN: ${{ github.token }}
VERSION: v0.22.1
SHA256: 0ab7a1d6932a213aed964ce97666c3077fe691c8606413674a8b3e0b9ec4cda0
run: |
cd "$RUNNER_TEMP"
gh release download "$VERSION" -R google/go-containerregistry \
-p go-containerregistry_Linux_x86_64.tar.gz
# Pinned here, not read from the release's own checksums.txt, so a replaced release can't pass.
echo "$SHA256 go-containerregistry_Linux_x86_64.tar.gz" | sha256sum -c -
tar -xzf go-containerregistry_Linux_x86_64.tar.gz crane
echo "$RUNNER_TEMP" >> "$GITHUB_PATH"
- run: printf '%s' "$TOKEN" | crane auth login ghcr.io -u "$GITHUB_ACTOR" --password-stdin
env:
TOKEN: ${{ github.token }}
- name: Copy each image, retrying Docker Hub's hiccups
run: |
status=0
while read -r name source; do
case "$name" in ''|\#*) continue ;; esac
want=${source##*@}
dest=ghcr.io/docspring/visualci/mirror/$name
if [ "$(crane digest "$dest" 2>/dev/null)" = "$want" ]; then echo "up to date: $dest"; continue; fi
for attempt in 1 2 3 4 5; do
crane copy "$source" "$dest" && break
[ "$attempt" = 5 ] || { echo "attempt $attempt failed; retrying in $((attempt * 20)) s"; sleep $((attempt * 20)); }
done
got=$(crane digest "$dest" 2>/dev/null || true)
if [ "$got" = "$want" ]; then echo "copied: $dest@$want"; else echo "::error::$dest is ${got:-missing}, want $want"; status=1; fi
done < .github/mirror.txt
exit $status
# Private packages are billed for storage: a copy that's neither listed nor pinned anywhere in this checkout's
# workflows or scripts goes once it's a week old (pinned ones stay, so a bump never breaks main's CI before it
# switches; the week gives branches time to rebase). Only images still listed are looked at: when a line goes,
# delete its package by hand. Only the workflow's own packages (this repo's mirror) are touched.
- name: Remove copies no longer listed
env:
GH_TOKEN: ${{ github.token }}
run: |
cutoff=$(date -u -d '7 days ago' +%Y-%m-%dT%H:%M:%SZ)
declare -A wanted
while read -r name source; do
case "$name" in ''|\#*) continue ;; esac
wanted[${name%%:*}]+=" ${source##*@}"
done < .github/mirror.txt
while IFS=@ read -r image digest; do
wanted[${image%%:*}]+=" $digest"
done < <(grep -rhoE 'visualci/mirror/[a-z0-9._-]+(:[A-Za-z0-9._-]+)?@sha256:[0-9a-f]{64}' .github scripts docker |
sed 's|^visualci/mirror/||' | sort -u)
for image in "${!wanted[@]}"; do
package=visualci%2Fmirror%2F$image
if ! versions=$(gh api --paginate "orgs/DocSpring/packages/container/$package/versions" \
--jq ".[] | select(.updated_at < \"$cutoff\") | \"\(.id) \(.name)\""); then
echo "::warning::could not list visualci/mirror/$image's versions, so none were removed"
continue
fi
[ -n "$versions" ] || continue
printf '%s\n' "$versions" |
while read -r id digest; do
case " ${wanted[$image]} " in *" $digest "*) continue ;; esac
if gh api -X DELETE "orgs/DocSpring/packages/container/$package/versions/$id" >/dev/null; then
echo "removed visualci/mirror/$image@$digest"
else
echo "::warning::could not remove visualci/mirror/$image@$digest (version $id)"
fi
done
done