From 8145cdec01fdf9a8b01ca0ec9018651171215184 Mon Sep 17 00:00:00 2001 From: Ronoaldo JLP Date: Fri, 17 Nov 2023 00:04:05 -0300 Subject: [PATCH 01/11] ci: execute tests before pushing image Fixes #14 --- .github/workflows/multiarch.yaml | 6 ++ minetest-wrapper.sh | 12 +++- sample/Dockerfile | 4 +- sample/minetest.conf | 8 +-- sample/quickstart.sh | 14 +++-- test/integration-test.sh | 56 +++++++++++++++++++ test/testdata/.minetest/worlds/world/world.mt | 8 +++ .../worlds/world/worldmods/shutdown/init.lua | 4 ++ 8 files changed, 101 insertions(+), 11 deletions(-) create mode 100755 test/integration-test.sh create mode 100644 test/testdata/.minetest/worlds/world/world.mt create mode 100644 test/testdata/.minetest/worlds/world/worldmods/shutdown/init.lua diff --git a/.github/workflows/multiarch.yaml b/.github/workflows/multiarch.yaml index 7a5f19a..f5a666e 100644 --- a/.github/workflows/multiarch.yaml +++ b/.github/workflows/multiarch.yaml @@ -74,6 +74,12 @@ jobs: run: | echo "${{ steps.meta.outputs.tags }}" + - name: Run a test build + id: run-test-build + shell: bash + run: | + ./test/integration-test.sh + - name: Set up QEMU for multiarch builds uses: docker/setup-qemu-action@v1 diff --git a/minetest-wrapper.sh b/minetest-wrapper.sh index e0cb726..380d118 100755 --- a/minetest-wrapper.sh +++ b/minetest-wrapper.sh @@ -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 +} >"${MINETEST_STDERR_FILE}" 2>"${MINETEST_STDERR_FILE}" \ No newline at end of file diff --git a/sample/Dockerfile b/sample/Dockerfile index 439395e..c398812 100644 --- a/sample/Dockerfile +++ b/sample/Dockerfile @@ -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 diff --git a/sample/minetest.conf b/sample/minetest.conf index 06a4cd0..f3f5201 100644 --- a/sample/minetest.conf +++ b/sample/minetest.conf @@ -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 diff --git a/sample/quickstart.sh b/sample/quickstart.sh index 8ab5a4f..f7bdfcb 100755 --- a/sample/quickstart.sh +++ b/sample/quickstart.sh @@ -2,18 +2,24 @@ set -x set -e +if [ -n "$1" ]; then + export BUILD_ARGS="--build-arg BASE_IMAGE=$1" +fi + # 1. (Re)build the image -docker build -t myserver:latest ./ +# shellcheck disable=2086 +docker build -t myserver:latest ${BUILD_ARGS} ./ # 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 +# 3. Run the server without the restart loop. docker run -it --rm --name minetest_server \ - -v $PWD/data:/var/lib/minetest \ + -v "$PWD/data:/var/lib/minetest" \ -p 30000:30000/udp -p 30000:30000/tcp \ + -e NO_LOOP=true \ myserver:latest diff --git a/test/integration-test.sh b/test/integration-test.sh new file mode 100755 index 0000000..63b8f51 --- /dev/null +++ b/test/integration-test.sh @@ -0,0 +1,56 @@ +#!/bin/bash +set -e + +BASEDIR="$(readlink -f "$(dirname "$0")/..")" +IMG="ghcr.io/ronoaldo/minetestserver:testing" +TMPDIR="$(mktemp -d)" + +log() { + echo "$(date "+%Y-%m-%d %H%M%S") $*" +} + +cleanup() { + log "Removing temporary directory ${TMPDIR}" +} + +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" + + log "Starting server with quickstart.sh script" + ./quickstart.sh "${IMG}" + + popd || exit +else + log "Error entering ${TMPDIR}" + exit 1 +fi \ No newline at end of file diff --git a/test/testdata/.minetest/worlds/world/world.mt b/test/testdata/.minetest/worlds/world/world.mt new file mode 100644 index 0000000..6e9aa9c --- /dev/null +++ b/test/testdata/.minetest/worlds/world/world.mt @@ -0,0 +1,8 @@ +gameid = minetesterror + +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 \ No newline at end of file diff --git a/test/testdata/.minetest/worlds/world/worldmods/shutdown/init.lua b/test/testdata/.minetest/worlds/world/worldmods/shutdown/init.lua new file mode 100644 index 0000000..ab29644 --- /dev/null +++ b/test/testdata/.minetest/worlds/world/worldmods/shutdown/init.lua @@ -0,0 +1,4 @@ +print("Shutting down in 5 seconds") +minetest.after(5, function() + minetest.request_shutdown("", false, 1) +end) \ No newline at end of file From 46ea6fdc571b13c2eeaabb0a9c18f8ed98b72881 Mon Sep 17 00:00:00 2001 From: Ronoaldo JLP Date: Fri, 17 Nov 2023 00:08:03 -0300 Subject: [PATCH 02/11] ci: add concurrency notation. Avoids running the same workflow event for the same branch multiple times at once, and only executes the latest commit of a set of changes. --- .github/workflows/multiarch.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/multiarch.yaml b/.github/workflows/multiarch.yaml index f5a666e..5d1e02b 100644 --- a/.github/workflows/multiarch.yaml +++ b/.github/workflows/multiarch.yaml @@ -1,5 +1,11 @@ name: Multiarch build docker image +# Ref https://github.com/orgs/community/discussions/26681 +# If the same push generates multiple events (pr, push, etc.) only run one build +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: From aac05b12a19501c908fb02713fe83df2c62a926a Mon Sep 17 00:00:00 2001 From: Ronoaldo JLP Date: Fri, 17 Nov 2023 00:15:13 -0300 Subject: [PATCH 03/11] ci: allow testing to run non-interactive. This change makes the quickstart.sh bigger, but should not hurt. --- sample/quickstart.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sample/quickstart.sh b/sample/quickstart.sh index f7bdfcb..a3a5640 100755 --- a/sample/quickstart.sh +++ b/sample/quickstart.sh @@ -1,24 +1,28 @@ #!/bin/bash +# shellcheck disable=2086 set -x set -e +# 0. Setup some environment variables for testing if [ -n "$1" ]; then export BUILD_ARGS="--build-arg BASE_IMAGE=$1" fi +if [ -t 1 ] ; then + export IT=-it +fi # 1. (Re)build the image -# shellcheck disable=2086 docker build -t myserver:latest ${BUILD_ARGS} ./ # 2. Prepare/fix the data dir mkdir -p data -docker run -it --rm \ +docker run ${IT} --rm \ -v "$PWD:/server" \ -u 0:0 \ myserver:latest bash -c 'chown -R minetest /server/data' # 3. Run the server without the restart loop. -docker run -it --rm --name minetest_server \ +docker run ${IT} --rm --name minetest_server \ -v "$PWD/data:/var/lib/minetest" \ -p 30000:30000/udp -p 30000:30000/tcp \ -e NO_LOOP=true \ From 21bc7c77d48d836bd69d36d4a175877ada9165fa Mon Sep 17 00:00:00 2001 From: Ronoaldo JLP Date: Fri, 17 Nov 2023 00:26:31 -0300 Subject: [PATCH 04/11] restored quickstart.sh, reconfigured it-test. Integration test will launch it's own script, it is just too buggy to use quickstart.sh for that. --- sample/quickstart.sh | 16 +++------------- test/integration-test.sh | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/sample/quickstart.sh b/sample/quickstart.sh index a3a5640..f3af8cc 100755 --- a/sample/quickstart.sh +++ b/sample/quickstart.sh @@ -1,29 +1,19 @@ #!/bin/bash -# shellcheck disable=2086 set -x set -e -# 0. Setup some environment variables for testing -if [ -n "$1" ]; then - export BUILD_ARGS="--build-arg BASE_IMAGE=$1" -fi -if [ -t 1 ] ; then - export IT=-it -fi - # 1. (Re)build the image -docker build -t myserver:latest ${BUILD_ARGS} ./ +docker build -t myserver:latest ./ # 2. Prepare/fix the data dir mkdir -p data -docker run ${IT} --rm \ +docker run --it --rm \ -v "$PWD:/server" \ -u 0:0 \ myserver:latest bash -c 'chown -R minetest /server/data' # 3. Run the server without the restart loop. -docker run ${IT} --rm --name minetest_server \ +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 diff --git a/test/integration-test.sh b/test/integration-test.sh index 63b8f51..bb8a0d7 100755 --- a/test/integration-test.sh +++ b/test/integration-test.sh @@ -4,6 +4,9 @@ set -e 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") $*" @@ -46,8 +49,23 @@ if pushd "${TMPDIR}" ; then # shellcheck disable=2102 cp -rv "${BASEDIR}"/test/testdata/.minetest "${TMPDIR}/data" - log "Starting server with quickstart.sh script" - ./quickstart.sh "${IMG}" + log "Building and starting test server ..." + docker build -t myserver:latest --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 \ + myserver:latest 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 \ + myserver:latest popd || exit else From e7f55d6e6e07471c14485cf667c7cd60e7070396 Mon Sep 17 00:00:00 2001 From: Ronoaldo JLP Date: Fri, 17 Nov 2023 00:45:44 -0300 Subject: [PATCH 05/11] attempt to fix failed builds. it is a bit late... --- .github/workflows/multiarch.yaml | 8 +------- test/integration-test.sh | 1 + 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/.github/workflows/multiarch.yaml b/.github/workflows/multiarch.yaml index 5d1e02b..9b09907 100644 --- a/.github/workflows/multiarch.yaml +++ b/.github/workflows/multiarch.yaml @@ -1,11 +1,5 @@ name: Multiarch build docker image -# Ref https://github.com/orgs/community/discussions/26681 -# If the same push generates multiple events (pr, push, etc.) only run one build -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - on: push: branches: @@ -14,7 +8,7 @@ on: - '**issue**' - '**patch**' - 'ronoaldo/**' - pull_request: + workflow_dispatch: env: REGISTRY: ghcr.io diff --git a/test/integration-test.sh b/test/integration-test.sh index bb8a0d7..c256458 100755 --- a/test/integration-test.sh +++ b/test/integration-test.sh @@ -1,5 +1,6 @@ #!/bin/bash set -e +set -x BASEDIR="$(readlink -f "$(dirname "$0")/..")" IMG="ghcr.io/ronoaldo/minetestserver:testing" From 73c6097a1f8ec52e2772d0bcaef7ec30ea09c697 Mon Sep 17 00:00:00 2001 From: Ronoaldo JLP Date: Fri, 17 Nov 2023 01:00:03 -0300 Subject: [PATCH 06/11] there we go ... This should fix the error I introduced myself. --- .github/workflows/multiarch.yaml | 5 +---- minetest-wrapper.sh | 2 +- sample/quickstart.sh | 7 ++++--- test/integration-test.sh | 7 ++++--- test/testdata/.minetest/worlds/world/world.mt | 2 +- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/.github/workflows/multiarch.yaml b/.github/workflows/multiarch.yaml index 9b09907..86645fe 100644 --- a/.github/workflows/multiarch.yaml +++ b/.github/workflows/multiarch.yaml @@ -4,10 +4,7 @@ on: push: branches: - 'main' - - 'stable-5' - - '**issue**' - - '**patch**' - - 'ronoaldo/**' + pull_request: workflow_dispatch: env: diff --git a/minetest-wrapper.sh b/minetest-wrapper.sh index 380d118..3d3de3f 100755 --- a/minetest-wrapper.sh +++ b/minetest-wrapper.sh @@ -17,4 +17,4 @@ while true ; do echo "Restarting in 10s ..." sleep 10 done -} >"${MINETEST_STDERR_FILE}" 2>"${MINETEST_STDERR_FILE}" \ No newline at end of file +} 2>&1 | tee -a "${MINETEST_STDERR_FILE}" \ No newline at end of file diff --git a/sample/quickstart.sh b/sample/quickstart.sh index f3af8cc..c5b51ff 100755 --- a/sample/quickstart.sh +++ b/sample/quickstart.sh @@ -7,13 +7,14 @@ docker build -t myserver:latest ./ # 2. Prepare/fix the data dir mkdir -p data -docker run --it --rm \ +docker run -it --rm \ -v "$PWD:/server" \ -u 0:0 \ myserver:latest bash -c 'chown -R minetest /server/data' -# 3. Run the server without the restart loop. -docker run --it --rm --name=myserver \ +# 3. Run the server +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 diff --git a/test/integration-test.sh b/test/integration-test.sh index c256458..b31d483 100755 --- a/test/integration-test.sh +++ b/test/integration-test.sh @@ -50,8 +50,9 @@ if pushd "${TMPDIR}" ; then # 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 myserver:latest --build-arg BASE_IMAGE=${IMG} ./ + docker build -t ${TEST_IMG} --build-arg BASE_IMAGE=${IMG} ./ # 2. Prepare/fix the data dir mkdir -p data @@ -59,14 +60,14 @@ if pushd "${TMPDIR}" ; then docker run ${IT} --rm \ -v "$PWD:/server" \ -u 0:0 \ - myserver:latest bash -c 'chown -R minetest /server/data' + ${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 \ - myserver:latest + ${TEST_IMG} popd || exit else diff --git a/test/testdata/.minetest/worlds/world/world.mt b/test/testdata/.minetest/worlds/world/world.mt index 6e9aa9c..e026a25 100644 --- a/test/testdata/.minetest/worlds/world/world.mt +++ b/test/testdata/.minetest/worlds/world/world.mt @@ -1,4 +1,4 @@ -gameid = minetesterror +gameid = minetest load_mod_ethereal = true load_mod_3d_armor = true From 757c5ddc4a8074f03a0a9ac29191730a2d5314b0 Mon Sep 17 00:00:00 2001 From: Ronoaldo JLP Date: Fri, 17 Nov 2023 01:06:23 -0300 Subject: [PATCH 07/11] ci: added better step description to it-test. --- .github/workflows/multiarch.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/multiarch.yaml b/.github/workflows/multiarch.yaml index 86645fe..74384c3 100644 --- a/.github/workflows/multiarch.yaml +++ b/.github/workflows/multiarch.yaml @@ -71,7 +71,7 @@ jobs: run: | echo "${{ steps.meta.outputs.tags }}" - - name: Run a test build + - name: Run integration tests on Dockerfile id: run-test-build shell: bash run: | From bd6fe729f2fd0ff3a1d66f15c8aa847bbfe5c70f Mon Sep 17 00:00:00 2001 From: Ronoaldo JLP Date: Fri, 17 Nov 2023 10:50:57 -0300 Subject: [PATCH 08/11] ci: updated github workflow settings. --- .github/workflows/multiarch.yaml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/multiarch.yaml b/.github/workflows/multiarch.yaml index 74384c3..cd22813 100644 --- a/.github/workflows/multiarch.yaml +++ b/.github/workflows/multiarch.yaml @@ -48,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: | @@ -78,20 +78,20 @@ jobs: ./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@v4 - 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 }} From 1dcd861295424a3b38a4054e17a21f89f516ee1f Mon Sep 17 00:00:00 2001 From: Ronoaldo JLP Date: Fri, 17 Nov 2023 10:53:32 -0300 Subject: [PATCH 09/11] ci: fixed docker/setup-buildx-action version. --- .github/workflows/multiarch.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/multiarch.yaml b/.github/workflows/multiarch.yaml index cd22813..0498255 100644 --- a/.github/workflows/multiarch.yaml +++ b/.github/workflows/multiarch.yaml @@ -81,7 +81,7 @@ jobs: uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v4 + uses: docker/setup-buildx-action@v3 - name: Log in to the Container registry uses: docker/login-action@v3 From 97ca7204c423fdccf59ca545daac994830684910 Mon Sep 17 00:00:00 2001 From: Ronoaldo JLP Date: Fri, 17 Nov 2023 11:17:16 -0300 Subject: [PATCH 10/11] ci: actually removing the directory. It was just issuing the log output. --- test/integration-test.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/test/integration-test.sh b/test/integration-test.sh index b31d483..4b74657 100755 --- a/test/integration-test.sh +++ b/test/integration-test.sh @@ -15,6 +15,7 @@ log() { cleanup() { log "Removing temporary directory ${TMPDIR}" + rm -rvf "${TMPDIR}" } version_from_workflow() { From c53eb1e3c93ba6ad9c12e8399191b8411d77877d Mon Sep 17 00:00:00 2001 From: Ronoaldo JLP Date: Fri, 17 Nov 2023 19:28:02 -0300 Subject: [PATCH 11/11] ci: makes cleanup optional --- test/integration-test.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/integration-test.sh b/test/integration-test.sh index 4b74657..cf802ae 100755 --- a/test/integration-test.sh +++ b/test/integration-test.sh @@ -14,8 +14,10 @@ log() { } cleanup() { - log "Removing temporary directory ${TMPDIR}" - rm -rvf "${TMPDIR}" + if [ "${CLEANUP}" = "true" ]; then + log "Removing temporary directory ${TMPDIR}" + rm -rvf "${TMPDIR}" + fi } version_from_workflow() {