Skip to content

Commit 5858ffe

Browse files
committed
add vendor gh action
1 parent b6eaa0c commit 5858ffe

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

.github/workflows/vendor.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Vendor and Push Images to GHCR
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
workflow_dispatch:
7+
8+
env:
9+
CONFIG_FILE: vendor.json
10+
SCRIPT_FILE: vendor.sh
11+
12+
jobs:
13+
vendor-and-push:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
17+
packages: write
18+
19+
steps:
20+
# Checkout repository
21+
- name: Checkout repository
22+
uses: actions/checkout@v4
23+
24+
# Set up Docker Buildx
25+
- name: Set up Docker Buildx
26+
uses: docker/setup-buildx-action@v3
27+
28+
# Login to GitHub Container Registry
29+
- name: Login to GHCR
30+
uses: docker/login-action@v3
31+
with:
32+
registry: ghcr.io
33+
username: ${{ github.actor }}
34+
password: ${{ secrets.GITHUB_TOKEN }}
35+
36+
# Install dependencies
37+
- name: Install dependencies
38+
run: |
39+
sudo apt-get update
40+
sudo apt-get install -y skopeo jq
41+
42+
# Make script executable
43+
- name: Prepare script
44+
run: chmod +x ${{ env.SCRIPT_FILE }}
45+
46+
# Run vendor script
47+
- name: Vendor and push images
48+
run: |
49+
./${{ env.SCRIPT_FILE }} "${{ env.CONFIG_FILE }}" "ghcr.io" "${{ github.repository }}"

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# IDE
2+
/.vscode/**/*
3+
!/.vscode/settings.json
4+
/.idea

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"docker.io/nginx": "stable-alpine3\\.[0-9]+-slim"
3+
}

vendor.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
set -e
3+
4+
CONFIG_FILE="${1:-images.json}"
5+
REGISTRY="${2:-ghcr.io}"
6+
REPO_NAME="${3}"
7+
8+
if [ -z "$REPO_NAME" ]; then
9+
echo "Error: Repository name is required"
10+
exit 1
11+
fi
12+
13+
# Check if config file exists
14+
if [ ! -f "$CONFIG_FILE" ]; then
15+
echo "Error: $CONFIG_FILE not found"
16+
exit 1
17+
fi
18+
19+
# Validate JSON
20+
jq . "$CONFIG_FILE" >/dev/null 2>&1 || {
21+
echo "Error: $CONFIG_FILE is not valid JSON"
22+
exit 1
23+
}
24+
25+
TARGET_PREFIX="${REGISTRY}/${REPO_NAME}"
26+
27+
echo "Processing images from $CONFIG_FILE..."
28+
29+
# Process each image and its tag regex
30+
MAPPINGS=""
31+
while IFS= read -r IMAGE; do
32+
if [ -n "$IMAGE" ]; then
33+
TAG_REGEX=$(jq -r ".\"${IMAGE}\"" "$CONFIG_FILE")
34+
if [ "$TAG_REGEX" = "null" ]; then
35+
echo "Warning: No tag regex for ${IMAGE}, skipping"
36+
continue
37+
fi
38+
39+
echo "Fetching tags for ${IMAGE} with regex ${TAG_REGEX}"
40+
TAGS=$(skopeo list-tags "docker://${IMAGE}" | jq -r '.Tags[]' | grep -E "${TAG_REGEX}" || true)
41+
42+
if [ -z "$TAGS" ]; then
43+
echo "Warning: No tags found for ${IMAGE} matching ${TAG_REGEX}"
44+
continue
45+
fi
46+
47+
# Generate mapping entry
48+
while IFS= read -r TAG; do
49+
if [ -n "$TAG" ]; then
50+
SOURCE="${IMAGE}:${TAG}"
51+
TARGET="${TARGET_PREFIX}/${IMAGE//\//-}:${TAG}"
52+
MAPPINGS="${MAPPINGS}${SOURCE}=${TARGET}"$'\n'
53+
54+
echo "Vendoring ${SOURCE} -> ${TARGET}"
55+
docker pull "$SOURCE"
56+
docker tag "$SOURCE" "$TARGET"
57+
docker push "$TARGET"
58+
echo "Successfully processed ${SOURCE}"
59+
fi
60+
done <<< "$TAGS"
61+
fi
62+
done <<< "$(jq -r 'keys[]' "$CONFIG_FILE")"
63+
64+
if [ -z "$MAPPINGS" ]; then
65+
echo "Error: No valid image mappings found"
66+
exit 1
67+
fi
68+
69+
echo "Successfully vendored images:"
70+
echo "$MAPPINGS"

0 commit comments

Comments
 (0)