Skip to content

Commit bc3144e

Browse files
committed
add test.sh + gated ci, modernize workflow
1 parent ecd01f5 commit bc3144e

5 files changed

Lines changed: 116 additions & 11 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
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
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
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 smoke tests
15+
run: ./test.sh

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[ -z "$DOCKER_REGISTRY" ] && echo "error please specify docker-registry DOCKER_REGISTRY" && exit 1
44
IMG="$DOCKER_REGISTRY/$(basename $(cat .git/config | tr ' ' '\n' | grep github.com))"
55

6-
PLATFORM="linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6"
6+
PLATFORM="linux/amd64,linux/arm64,linux/arm/v7"
77

88
TAG=$(./get-version.sh)
99

get-version.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ export APACHE_VERSION=$(docker run --rm -t get-version dpkg --list apache2 | gre
66
[ -z "$DEBIAN_VERSION" ] && exit 1
77

88
export IMGTAG=$(echo "$1""d$DEBIAN_VERSION-a$APACHE_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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/sh
2+
# Automated smoke test for the apache2-ssl-secure container.
3+
# Builds the image, runs it (the entrypoint auto-generates a self-signed cert),
4+
# waits for apache to listen on 80 and asserts that the apache config is valid,
5+
# that HTTP serves a response and that PHP actually executes.
6+
set -e
7+
8+
IMG=apache2-ssl-secure-test
9+
CN=apache2-ssl-secure-test-run
10+
11+
cleanup() { docker rm -f "$CN" >/dev/null 2>&1 || true; }
12+
trap cleanup EXIT
13+
14+
fail() { echo "FAIL: $1"; exit 1; }
15+
16+
# HTTP GET over bash /dev/tcp (the image ships neither wget nor curl, but bash
17+
# is present as the entrypoint interpreter). $1 = path, prints the raw response.
18+
http_get() {
19+
docker exec "$CN" bash -c '
20+
exec 3<>/dev/tcp/127.0.0.1/80 || exit 1
21+
printf "GET %s HTTP/1.0\r\nHost: localhost\r\n\r\n" "'"$1"'" >&3
22+
cat <&3
23+
'
24+
}
25+
26+
echo ">> building image"
27+
docker build -t "$IMG" .
28+
29+
echo ">> starting container"
30+
docker rm -f "$CN" >/dev/null 2>&1 || true
31+
docker run -d --name "$CN" "$IMG" >/dev/null
32+
33+
echo ">> waiting for apache to listen on 80 (up to 120s)"
34+
up=0
35+
for _ in $(seq 1 60); do
36+
if docker exec "$CN" bash -c 'exec 3<>/dev/tcp/127.0.0.1/80' 2>/dev/null; then up=1; break; fi
37+
sleep 2
38+
done
39+
[ "$up" = 1 ] || fail "apache did not start listening on 80 in time"
40+
41+
echo ">> assert: container is running"
42+
[ "$(docker inspect -f '{{.State.Running}}' "$CN")" = true ] || fail "container not running"
43+
echo "ok - container running"
44+
45+
echo ">> assert: apache2ctl -t reports Syntax OK"
46+
syntax=$(docker exec "$CN" apache2ctl -t 2>&1 || true)
47+
echo "$syntax" | grep -q 'Syntax OK' || fail "apache config not valid (got: $syntax)"
48+
echo "ok - apache2ctl -t: Syntax OK"
49+
50+
echo ">> assert: HTTP request on port 80 returns a response"
51+
resp=$(http_get /) || fail "no HTTP response on port 80"
52+
echo "$resp" | grep -q '^HTTP/' || fail "no HTTP status line (got: $(echo "$resp" | head -1))"
53+
echo "ok - HTTP responded: $(echo "$resp" | head -1 | tr -d '\r')"
54+
55+
echo ">> assert: PHP executes"
56+
docker exec "$CN" bash -c "printf '%s' \"<?php echo 'PHPOK:'.PHP_VERSION;\" > /var/www/html/phptest.php"
57+
body=$(http_get /phptest.php)
58+
echo "$body" | grep -q 'PHPOK:8\.' || fail "PHP did not execute as expected (got: $body)"
59+
echo "ok - PHP executed: $(echo "$body" | grep -o 'PHPOK:8\.[0-9.]*')"
60+
61+
echo ""
62+
echo "ALL TESTS PASSED"

0 commit comments

Comments
 (0)