Skip to content

Commit 9d22beb

Browse files
committed
add test.sh + gate ci on it, modernize publish workflow
1 parent 2868f93 commit 9d22beb

4 files changed

Lines changed: 154 additions & 10 deletions

File tree

.github/workflows/docker-publish.yml

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,21 @@ env:
2121
REGISTRY: ghcr.io
2222
# github.repository as <account>/<repo>
2323
IMAGE_NAME: ${{ github.repository }}
24+
IMAGE_PLATFORMS: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
2425

2526

2627
jobs:
28+
# gate: don't build or publish anything unless the tests pass
29+
test:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- name: Checkout repository
33+
uses: actions/checkout@v5
34+
- name: Run tests
35+
run: ./test.sh
36+
2737
build:
38+
needs: test
2839

2940
runs-on: ubuntu-latest
3041
permissions:
@@ -36,20 +47,25 @@ jobs:
3647

3748
steps:
3849
- name: Checkout repository
39-
uses: actions/checkout@v4
50+
uses: actions/checkout@v5
4051

4152
- name: Install Cosign
4253
uses: sigstore/cosign-installer@v3.5.0
4354

55+
- name: Set up QEMU
56+
uses: docker/setup-qemu-action@v4
57+
with:
58+
platforms: ${{ env.IMAGE_PLATFORMS }}
59+
4460
# Workaround: https://github.com/docker/build-push-action/issues/461
4561
- name: Setup Docker buildx
46-
uses: docker/setup-buildx-action@v3
62+
uses: docker/setup-buildx-action@v4
4763

4864
# Login against a Docker registry except on PR
4965
# https://github.com/docker/login-action
5066
- name: Log into registry ${{ env.REGISTRY }}
5167
if: github.event_name != 'pull_request'
52-
uses: docker/login-action@v3
68+
uses: docker/login-action@v4
5369
with:
5470
registry: ${{ env.REGISTRY }}
5571
username: ${{ github.actor }}
@@ -59,12 +75,16 @@ jobs:
5975
# https://github.com/docker/metadata-action
6076
- name: Extract Docker metadata
6177
id: meta
62-
uses: docker/metadata-action@v5
78+
uses: docker/metadata-action@v6
6379
with:
6480
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
6581

6682
- name: Extract Docker Version Tag
67-
id: docker_version_tag
83+
id: docker_version_tag
84+
env:
85+
# rebuild on code changes (push / manual run); only the nightly
86+
# schedule dedups on the version tag to skip identical rebuilds
87+
FORCE_REBUILD: ${{ github.event_name != 'schedule' && '1' || '' }}
6888
run: |
6989
./get-version.sh $(echo "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:" | tr '[:upper:]' '[:lower:]') | sed 's/^/tag=/g' >> $GITHUB_OUTPUT
7090
@@ -77,12 +97,12 @@ jobs:
7797
- name: Build and push Docker image with tag
7898
if: ${{ !endsWith(steps.docker_version_tag.outputs.tag, 'latest') }}
7999
id: build-and-push-tagged
80-
uses: docker/build-push-action@v5
100+
uses: docker/build-push-action@v7
81101
with:
82102
context: .
83103
push: ${{ github.event_name != 'pull_request' }}
84104
tags: ${{ steps.docker_version_tag.outputs.tag }}
85-
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
105+
platforms: ${{ env.IMAGE_PLATFORMS }}
86106
labels: ${{ steps.meta.outputs.labels }}
87107
cache-from: type=gha
88108
cache-to: type=gha,mode=max
@@ -100,7 +120,7 @@ jobs:
100120

101121
- name: Build Docker Latest Tag
102122
if: ${{ !endsWith(steps.docker_version_tag.outputs.tag, 'latest') }}
103-
id: docker_latest_tag
123+
id: docker_latest_tag
104124
run: |
105125
echo "tag=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" | tr '[:upper:]' '[:lower:]' >> $GITHUB_OUTPUT
106126
@@ -109,12 +129,12 @@ jobs:
109129
- name: Build and push Docker image as latest
110130
if: ${{ !endsWith(steps.docker_version_tag.outputs.tag, 'latest') }}
111131
id: build-and-push-latest
112-
uses: docker/build-push-action@v5
132+
uses: docker/build-push-action@v7
113133
with:
114134
context: .
115135
push: ${{ github.event_name != 'pull_request' }}
116136
tags: ${{ steps.docker_latest_tag.outputs.tag }}
117-
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
137+
platforms: ${{ env.IMAGE_PLATFORMS }}
118138
labels: ${{ steps.meta.outputs.labels }}
119139
cache-from: type=gha
120140
cache-to: type=gha,mode=max

.github/workflows/test.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout repository
12+
uses: actions/checkout@v5
13+
14+
- name: Run test suite
15+
run: ./test.sh

get-version.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ export ALPINE_VERSION=$(docker run --rm -t get-version cat /etc/alpine-release |
66
[ -z "$ALPINE_VERSION" ] && exit 1
77

88
export IMGTAG=$(echo "$1""a$ALPINE_VERSION-g$GITHUBBACKUP_VERSION")
9+
# FORCE_REBUILD (set by the workflow for push / manual runs) rebuilds even if
10+
# the versioned image already exists, so code/config changes get republished.
11+
# the nightly schedule leaves it unset and keeps deduping on the version tag.
12+
if [ -n "$FORCE_REBUILD" ]; then
13+
echo "$IMGTAG"
14+
exit 0
15+
fi
16+
917
export IMAGE_EXISTS=$(docker pull "$IMGTAG" 2>/dev/null >/dev/null; echo $?)
1018

1119
# return latest, if container is already available :)

test.sh

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/bin/sh
2+
# automated functional test for the github-backup container
3+
# builds the image and verifies the backup script actually clones a public
4+
# github repo (real git objects land on disk). all test data is created INSIDE
5+
# the container - nothing is bind-mounted from the host.
6+
set -eu
7+
8+
IMAGE=github-backup-test
9+
NAME=github-backup-test-run
10+
11+
# a tiny, well-known public repo that needs no auth
12+
TEST_REPO=octocat/Hello-World
13+
BACKUP_DIR=/data/repos
14+
15+
FAILED=0
16+
fail() {
17+
echo "FAIL: $*" >&2
18+
FAILED=1
19+
}
20+
21+
cleanup() {
22+
echo ">> cleanup: removing container $NAME"
23+
docker rm -f "$NAME" >/dev/null 2>&1 || true
24+
}
25+
trap cleanup EXIT INT TERM
26+
27+
echo ">> building image $IMAGE"
28+
docker build -t "$IMAGE" .
29+
30+
echo ">> (re)starting container $NAME"
31+
docker rm -f "$NAME" >/dev/null 2>&1 || true
32+
# it's a batch tool, not a server - keep the container alive so we can drive
33+
# the backup script via docker exec and inspect its output afterwards
34+
docker run -d --name "$NAME" "$IMAGE" sleep 900
35+
36+
echo ">> assert: container is running"
37+
docker ps --format '{{.Names}}' | grep -q "^${NAME}$" \
38+
&& echo "ok - container running" || fail "container not running"
39+
40+
echo ">> assert: git is installed"
41+
if docker exec "$NAME" sh -c 'command -v git' >/dev/null 2>&1; then
42+
echo "ok - git present"
43+
else
44+
fail "git not found in container"
45+
fi
46+
47+
echo ">> assert: backup script is on PATH and executable"
48+
if docker exec "$NAME" sh -c 'command -v github-backup.sh' >/dev/null 2>&1; then
49+
echo "ok - github-backup.sh on PATH"
50+
else
51+
fail "github-backup.sh not found on PATH"
52+
fi
53+
54+
echo ">> assert: script prints usage and exits non-zero without args"
55+
if docker exec "$NAME" github-backup.sh >/dev/null 2>&1; then
56+
fail "script exited 0 with no args (expected usage error)"
57+
else
58+
echo "ok - script rejects missing args"
59+
fi
60+
61+
echo ">> assert: real backup - clone public repo $TEST_REPO"
62+
docker exec "$NAME" mkdir -p "$BACKUP_DIR"
63+
if docker exec "$NAME" github-backup.sh "$TEST_REPO" "$BACKUP_DIR"; then
64+
echo "ok - backup script ran"
65+
else
66+
fail "backup script returned non-zero"
67+
fi
68+
69+
# the single-repo path clones https://github.com/<repo> into <repo>.git
70+
CLONE_DIR="$BACKUP_DIR/$TEST_REPO.git"
71+
72+
echo ">> assert: backup produced a git repo with objects"
73+
if docker exec "$NAME" test -d "$CLONE_DIR/.git"; then
74+
echo "ok - clone dir exists: $CLONE_DIR"
75+
else
76+
fail "expected clone dir not found: $CLONE_DIR"
77+
fi
78+
79+
echo ">> assert: cloned repo has a valid HEAD commit"
80+
HEAD_SHA=$(docker exec "$NAME" git -C "$CLONE_DIR" rev-parse HEAD 2>/dev/null || true)
81+
if echo "$HEAD_SHA" | grep -qE '^[0-9a-f]{40}$'; then
82+
echo "ok - HEAD commit: $HEAD_SHA"
83+
else
84+
fail "could not resolve HEAD in backup (got: '$HEAD_SHA')"
85+
fi
86+
87+
echo ">> assert: cloned repo has real git objects on disk"
88+
if docker exec "$NAME" sh -c "git -C '$CLONE_DIR' log --oneline | head -n1" | grep -q .; then
89+
echo "ok - git history present in backup"
90+
else
91+
fail "no git history found in backup"
92+
fi
93+
94+
echo
95+
if [ "$FAILED" -eq 0 ]; then
96+
echo "ALL TESTS PASSED"
97+
exit 0
98+
else
99+
echo "SOME TESTS FAILED"
100+
exit 1
101+
fi

0 commit comments

Comments
 (0)