Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: execute tests before pushing image #15

Merged
merged 12 commits into from
Nov 17, 2023
25 changes: 14 additions & 11 deletions .github/workflows/multiarch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ on:
push:
branches:
- 'main'
- 'stable-5'
- '**issue**'
- '**patch**'
- 'ronoaldo/**'
pull_request:
workflow_dispatch:

env:
REGISTRY: ghcr.io
Expand Down Expand Up @@ -51,17 +48,17 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Extract Minetest branch from Dockerfile/args
id: minetest-version
run: |
export MINETEST_VERSION="$(echo "${{ matrix.args }}" | grep MINETEST_VERSION | cut -f 2 -d=)"
echo "::set-output name=version::${MINETEST_VERSION}"
echo "version=${MINETEST_VERSION}" >> $GITHUB_OUTPUT

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v3.5.0
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE }}
tags: |
Expand All @@ -74,21 +71,27 @@ jobs:
run: |
echo "${{ steps.meta.outputs.tags }}"

- name: Run integration tests on Dockerfile
id: run-test-build
shell: bash
run: |
./test/integration-test.sh

- name: Set up QEMU for multiarch builds
uses: docker/setup-qemu-action@v1
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
uses: docker/setup-buildx-action@v3

- name: Log in to the Container registry
uses: docker/login-action@v1.10.0
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v2
uses: docker/build-push-action@v5
with:
context: .
file: ${{ matrix.dockerfile }}
Expand Down
12 changes: 10 additions & 2 deletions minetest-wrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
MINETEST_STDERR_FILE=${MINETEST_STDERR_FILE:-/tmp/minetest.stderr}

# Main loop
{
while true ; do
echo -e "\n\n-- Separator --\n\n" >> ${MINETEST_STDERR_FILE}
minetestserver $@ 2>&1 | tee -a ${MINETEST_STDERR_FILE}
echo -e "\n\n-- Separator --\n\n" >> "${MINETEST_STDERR_FILE}"
minetestserver "$@"
RET="$?"
if [ "${NO_LOOP}" == "true" ]; then
echo "Exiting ..."
exit ${RET}
fi

echo "Minetest server crashed! See error logs at debug.txt and ${MINETEST_STDERR_FILE}"
echo "Restarting in 10s ..."
sleep 10
done
} 2>&1 | tee -a "${MINETEST_STDERR_FILE}"
4 changes: 3 additions & 1 deletion sample/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM ghcr.io/ronoaldo/minetestserver:stable-5
# Use the default stable 5.x version
ARG BASE_IMAGE=ghcr.io/ronoaldo/minetestserver:stable-5
FROM ${BASE_IMAGE}

# Install mods from ContentDB
WORKDIR /usr/share/minetest
Expand Down
8 changes: 4 additions & 4 deletions sample/minetest.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
default_game = minetest
server_name = My Custom Server
motd = Welcome to my custom server
server_name = My Test Server
motd = Welcome to my test server

server_announce = true
server_address = jupiter.ronoaldo.dev.br
server_announce = false
server_address = your.public.dns.or-ip.here
port = 30000
bind_address = 0.0.0.0
7 changes: 4 additions & 3 deletions sample/quickstart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ docker build -t myserver:latest ./
# 2. Prepare/fix the data dir
mkdir -p data
docker run -it --rm \
-v $PWD:/server \
-v "$PWD:/server" \
-u 0:0 \
myserver:latest bash -c 'chown -R minetest /server/data'

# 3. Run the server
docker run -it --rm --name minetest_server \
-v $PWD/data:/var/lib/minetest \
docker run -it --rm --name=myserver \
-v "$PWD/data:/var/lib/minetest" \
-p 30000:30000/udp -p 30000:30000/tcp \
-e NO_LOOP=true \
myserver:latest
79 changes: 79 additions & 0 deletions test/integration-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash
set -e
set -x

BASEDIR="$(readlink -f "$(dirname "$0")/..")"
IMG="ghcr.io/ronoaldo/minetestserver:testing"
TMPDIR="$(mktemp -d)"
if [ -t 1 ] ; then
export IT=-it
fi

log() {
echo "$(date "+%Y-%m-%d %H%M%S") $*"
}

cleanup() {
if [ "${CLEANUP}" = "true" ]; then
log "Removing temporary directory ${TMPDIR}"
rm -rvf "${TMPDIR}"
fi
}

version_from_workflow() {
# TODO(ronoaldo): proper YAML parser to avoid breaking
grep "$1"= "${BASEDIR}/.github/workflows/multiarch.yaml" | grep -v export | tail -n 1 | cut -f 2 -d=
}

log "Starting an integration test using IMG=${IMG}"
trap 'cleanup' EXIT

log "Detecting versions from workflow ..."
MT="$(version_from_workflow MINETEST_VERSION)"
MTG="$(version_from_workflow MINETEST_GAME_VERSION)"
IRR="$(version_from_workflow MINETEST_IRRLICHT_VERSION)"

log "Building/tagging test image using versions: ${MT}, ${MTG}, ${IRR}... "
docker build \
-t "${IMG}" \
--build-arg MINETEST_VERSION="${MT}" \
--build-arg MINETEST_GAME_VERSION="${MTG}" \
--build-arg MINETEST_IRRLICHT_VERSION="${IRR}" \
./

log "Using ${TMPDIR} as a temporary directory"

cp -rv "${BASEDIR}"/sample/* "${TMPDIR}"
if pushd "${TMPDIR}" ; then
log "Cleaning up previous data (if any)"
rm -rvf "${TMPDIR}/data"

log "Installing local world shutdown hook"
mkdir -p "${TMPDIR}/data"
# shellcheck disable=2102
cp -rv "${BASEDIR}"/test/testdata/.minetest "${TMPDIR}/data"

TEST_IMG="gcr.io/ronoaldo/myserver:testsrv"
log "Building and starting test server ..."
docker build -t ${TEST_IMG} --build-arg BASE_IMAGE=${IMG} ./

# 2. Prepare/fix the data dir
mkdir -p data
# shellcheck disable=2086
docker run ${IT} --rm \
-v "$PWD:/server" \
-u 0:0 \
${TEST_IMG} bash -c 'chown -R minetest /server/data'

# 3. Run the server without the restart loop.
# shellcheck disable=2086
docker run ${IT} --rm \
-v "$PWD/data:/var/lib/minetest" \
-e NO_LOOP=true \
${TEST_IMG}

popd || exit
else
log "Error entering ${TMPDIR}"
exit 1
fi
8 changes: 8 additions & 0 deletions test/testdata/.minetest/worlds/world/world.mt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
gameid = minetest

load_mod_ethereal = true
load_mod_3d_armor = true
load_mod_3d_armor_ip = true
load_mod_3d_armor_sfinv = true
load_mod_3d_armor_stand = true
load_mod_3d_armor_ui = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
print("Shutting down in 5 seconds")
minetest.after(5, function()
minetest.request_shutdown("", false, 1)
end)