-
Notifications
You must be signed in to change notification settings - Fork 0
154 lines (125 loc) · 4.76 KB
/
mirror_images.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
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
name: "Mirror images"
on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
env:
IMAGES: |
node
alpine
BATCH_SIZE: 500
concurrency:
group: "mirror-images-${{ github.workflow }}"
jobs:
simulate-activity:
name: "Simulate repo activity"
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Simulate repository activity to keep schedule active
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git pull origin simulate-activity || true
git checkout simulate-activity
git commit --amend --no-edit
git push origin simulate-activity --force || true
prepare:
name: "Prepare matrix"
runs-on: ubuntu-latest
outputs:
matrix_json: ${{ steps.prepare-matrix.outputs.matrix_json }}
steps:
- name: Prepare matrix for mirror job
id: prepare-matrix
shell: bash
run: |
set -euo pipefail
python3 -u - <<EOF
import json
import subprocess
from pathlib import Path
batch_size = int("${{ env.BATCH_SIZE }}")
images = """${{ env.IMAGES }}""".splitlines()
outdir = Path("batches")
outdir.mkdir(exist_ok=True)
matrix = []
for image in images:
src_image_name = image
dst_image_name = src_image_name.replace("/", "-").replace(".", "") + "-mirror"
if "/" not in src_image_name:
src_image_name = f"library/{src_image_name}"
src_image = f"docker.io/{src_image_name}"
dst_image = f"ghcr.io/${{ github.repository_owner }}/{dst_image_name}"
info_json = subprocess.check_output(["skopeo", "list-tags", f"docker://{src_image}"], text=True)
info = json.loads(info_json)
batch_num = 1
for i in range(0, len(info["Tags"]), batch_size):
batch = info["Tags"][i:i + batch_size]
payload = {
"src_image": src_image,
"dst_image": dst_image,
"tags": batch,
}
with open(outdir / f"{image}-{batch_num}.json", "w") as f:
json.dump(payload, f, indent=2)
matrix.append({
"image": image,
"batch_num": batch_num,
})
batch_num += 1
print("::group::Matrix")
print(json.dumps(matrix, indent=2))
print("::endgroup:")
with open("$GITHUB_OUTPUT", "a") as f:
f.write(f"matrix_json={json.dumps(matrix)}\n")
EOF
- name: Upload batches artifacts
uses: actions/upload-artifact@v4
with:
name: batches
path: batches
retention-days: 1
mirror:
name: "Mirror ${{ matrix.batch.image }} (batch ${{ matrix.batch.batch_num }})"
runs-on: ubuntu-latest
needs: prepare
permissions:
packages: write
strategy:
matrix:
batch: ${{ fromJSON(needs.prepare.outputs.matrix_json) }}
fail-fast: false
steps:
- name: Download batches artifact
uses: actions/download-artifact@v4
with:
name: batches
- name: Log into GHCR with Skopeo
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | \
skopeo login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Mirror image
run: |
set -euo pipefail
batch_file="${{ matrix.batch.image }}-${{ matrix.batch.batch_num }}.json"
echo "::group::Batch file: $batch_file"
cat "$batch_file"
echo
echo "::endgroup::"
src_image=$(jq -r '.src_image' "$batch_file")
dst_image=$(jq -r '.dst_image' "$batch_file")
mapfile -t tags < <(jq -r '.tags[]' "$batch_file")
echo "Will copy ${{ matrix.batch.image }} ($src_image → $dst_image), batch ${{ matrix.batch.batch_num }}"
for tag in "${tags[@]}"; do
echo "::group::Copying $src_image:$tag → $dst_image:$tag"
status=0
skopeo copy --all -f oci -f v2s2 "docker://$src_image:$tag" "docker://$dst_image:$tag" || status=$?
echo "::endgroup::"
if [[ $status -ne 0 ]]; then
echo "::error::Failed to copy $src_image:$tag → $dst_image:$tag (status: $status)"
fi
done