From 3a9ffc8b24369dee120f8729233e47c8fd08ec63 Mon Sep 17 00:00:00 2001 From: kimzuni Date: Sun, 31 Mar 2024 15:40:42 +0900 Subject: [PATCH] Add TARGET_COMMIT_ID --- .github/workflows/developer.yml | 47 +++++++++++++++++++++++++++++++++ Dockerfile | 14 +++++----- scripts/helper_functions.sh | 9 ++----- scripts/helper_install.sh | 42 ++++++++++++++++++++++------- scripts/init.sh | 14 +++++----- scripts/start.sh | 17 +++++++----- scripts/variables.sh | 9 ++++--- 7 files changed, 112 insertions(+), 40 deletions(-) create mode 100644 .github/workflows/developer.yml diff --git a/.github/workflows/developer.yml b/.github/workflows/developer.yml new file mode 100644 index 0000000..f5cb119 --- /dev/null +++ b/.github/workflows/developer.yml @@ -0,0 +1,47 @@ +name: Developer image +on: # yamllint disable-line rule:truthy + push: + branches: [main] + +jobs: + push: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Docker - Login + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Docker - GHCR Login + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Docker - Metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: | + ${{ github.repository }} + ghcr.io/${{ github.repository }} + tags: type=raw,value=dev + flavor: latest=false + + - name: Docker - Build / Push + uses: docker/build-push-action@v5 + with: + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 89be895..4f237ca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -33,7 +33,7 @@ RUN apt-get update && \ ca-certificates \ lib32gcc1-amd64-cross \ procps=2:4.0.2-3 \ - xdg-user-dirs \ + xdg-user-dirs=0.18-1 \ jo=1.9-1 \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* @@ -112,18 +112,18 @@ ENV TZ="UTC" \ ARM_COMPATIBILITY_MODE=false COPY --chmod=755 ./scripts /home/steam/server -RUN mv /home/steam/server/backup.sh /usr/local/bin/backup +RUN for file in backup.sh; do \ + mv /home/steam/server/$file /usr/local/bin/${file%.sh}; \ + done WORKDIR /home/steam/server RUN touch crontab \ - && mkdir -p /home/steam/Steam/package \ - && chown steam:steam /home/steam/Steam/package \ && rm -rf /tmp/dumps \ + && mkdir -p /home/steam/Steam/package \ && chmod o+w crontab /home/steam/Steam/package \ - && chown -R steam:steam /home/steam/server + && chown -R steam:steam /home/steam/{server,Steam} HEALTHCHECK --start-period=3m \ CMD pidof "LongvinterServer-Linux-Shipping" > /dev/null || exit 1 -EXPOSE $PORT $QUERY_PORT -ENTRYPOINT ["/home/steam/server/init.sh"] +ENTRYPOINT ["/home/steam/server/init.sh"] \ No newline at end of file diff --git a/scripts/helper_functions.sh b/scripts/helper_functions.sh index ae036a5..f25399d 100644 --- a/scripts/helper_functions.sh +++ b/scripts/helper_functions.sh @@ -117,12 +117,9 @@ Log() { DiscordMessage() { local title="$1" local message="$2" - local level="${3}" + local level="${3:-info}" local enabled="$4" local webhook_url="$5" - if [ -z "$level" ]; then - level="info" - fi if [ -n "${DISCORD_WEBHOOK_URL}" ]; then /home/steam/server/discord.sh "$title" "$message" "$level" "$enabled" "$webhook_url" & fi @@ -132,8 +129,6 @@ DiscordMessage() { # Returns 0 if it is shutdown # Returns not 0 if it is not able to be shutdown shutdown_server() { - local return_val=0 - kill -15 `pidof LongvinterServer-Linux-Shipping` return $? } @@ -146,7 +141,7 @@ Server_Info() { echo "Server Name: $CFG_SERVER_NAME" if [ "$DISCORD_SERVER_INFO_MESSAGE_WITH_IP" = true ]; then - echo "Server IP: `curl -sfL icanhazip.com`" + echo "Server IP: `curl -sfSL icanhazip.com`" echo "Server Port: $PORT" fi echo "Server Password: $CFG_PASSWORD" diff --git a/scripts/helper_install.sh b/scripts/helper_install.sh index dee8919..27715fa 100644 --- a/scripts/helper_install.sh +++ b/scripts/helper_install.sh @@ -2,6 +2,17 @@ # shellcheck source=scripts/helper_functions.sh source "/home/steam/server/helper_functions.sh" +# Returns 0 if TARGET_COMMIT_ID has not been declare or is valid +# Returns 1 if Invalid TARGET_COMMIT_ID +CheckCommitID() { + local COMMIT_ID="$1" + local URL="$GIT_REPO_API/commits/$COMMIT_ID" + if [ -n "$COMMIT_ID" ] && ! curl -sfSL "$URL" > /dev/null 2>&1; then + return 1 + fi + return 0 +} + # Returns 0 if game is installed # Returns 1 if game is not installed IsInstalled() { @@ -16,17 +27,25 @@ IsInstalled() { UpdateRequired() { LogAction "Checking for new update" - cd $GIT_REPO_PATH - git fetch origin main - local LATEST_VERSION=`git log HEAD..origin/main -1 | tail -1 | awk '{print $2}'` - if [ -z "$LATEST_VERSION" ]; then - return 1 + cd "$GIT_REPO_PATH" + + local CURRENT_COMMIT=$(git log HEAD -1 | head -1 | awk '{print $2}') + local LATEST_COMMIT=$(curl -sfSL "$GIT_REPO_API/commits/main" | grep sha | head -1 | awk -F'"' '{print $4}') + + if [ -n "$TARGET_COMMIT_ID" ]; then + if [[ "$CURRENT_COMMIT" =~ ^"$TARGET_COMMIT_ID" ]]; then + return 1 + fi + else + if [ "$CURRENT_COMMIT" == "$LATEST_COMMIT" ]; then + return 1 + fi fi return 0 } BeforeInstall() { - if [ "$architecture" == "arm64" ] && [ "$pagesize" != "4096" ]; then + if [ "$ARCHITECTURE" == "arm64" ] && [ "$PAGESIZE" != "4096" ]; then LogWarn "WARNING: Only ARM64 hosts with 4k page size is supported when running steamcmd. Expect server installation to fail." fi } @@ -34,14 +53,14 @@ BeforeInstall() { InstallServer() { BeforeInstall - mkdir -p "$GIT_REPO_PATH" && cd "$GIT_REPO_PATH" + cd "$GIT_REPO_PATH" DiscordMessage "Install" "${DISCORD_PRE_INSTALL_MESSAGE}" "in-progress" "${DISCORD_PRE_INSTALL_MESSAGE_ENABLED}" "${DISCORD_PRE_INSTALL_MESSAGE_URL}" git init git remote add origin $GIT_REPO_URL git fetch origin $GIT_REPO_BRANCH - git checkout -b $GIT_REPO_BRANCH --track origin/$GIT_REPO_BRANCH - + git checkout ${TARGET_COMMIT_ID:-$GIT_REPO_BRANCH} + DiscordMessage "Install" "${DISCORD_POST_INSTALL_MESSAGE}" "in-progress" "${DISCORD_POST_INSTALL_MESSAGE_ENABLED}" "${DISCORD_POST_INSTALL_MESSAGE_URL}" } @@ -52,7 +71,10 @@ UpdateServer() { DiscordMessage "Update" "${DISCORD_PRE_UPDATE_BOOT_MESSAGE}" "in-progress" "${DISCORD_PRE_UPDATE_BOOT_MESSAGE_ENABLED}" "${DISCORD_PRE_UPDATE_BOOT_MESSAGE_URL}" git stash - git pull $GIT_REPO_URL $GIT_REPO_BRANCH + if [ -z "$TARGET_COMMIT_ID" ] || ! git log -1 "${TARGET_COMMIT_ID}" > /dev/null 2>&1; then + git fetch origin $GIT_REPO_BRANCH + fi + git checkout "${TARGET_COMMIT_ID:-origin/$GIT_REPO_BRANCH}" git stash clear DiscordMessage "Update" "${DISCORD_POST_UPDATE_BOOT_MESSAGE}" "in-progress" "${DISCORD_POST_UPDATE_BOOT_MESSAGE_ENABLED}" "${DISCORD_POST_UPDATE_BOOT_MESSAGE_URL}" diff --git a/scripts/init.sh b/scripts/init.sh index 17074dd..a3af4e1 100644 --- a/scripts/init.sh +++ b/scripts/init.sh @@ -5,6 +5,14 @@ source "/home/steam/server/variables.sh" # shellcheck source=scripts/helper_functions.sh source "/home/steam/server/helper_functions.sh" + + +if ! dirExists "$DATA_DIR"; then + LogError "$DATA_DIR is not mounted." + exit 1 +fi +mkdir -p "$SERVER_BACKUP_DIR" + if [[ "$(id -u)" -eq 0 ]] && [[ "$(id -g)" -eq 0 ]]; then if [[ "${PUID}" -ne 0 ]] && [[ "${PGID}" -ne 0 ]]; then LogAction "EXECUTING USERMOD" @@ -20,18 +28,12 @@ elif [[ "$(id -u)" -eq 0 ]] || [[ "$(id -g)" -eq 0 ]]; then exit 1 fi -if ! [ -w "$DATA_DIR" ]; then - LogError "$DATA_DIR is not writable." - exit 1 -fi - term_handler() { DiscordMessage "Shutdown" "${DISCORD_PRE_SHUTDOWN_MESSAGE}" "in-progress" "${DISCORD_PRE_SHUTDOWN_MESSAGE_ENABLED}" "${DISCORD_PRE_SHUTDOWN_MESSAGE_URL}" if ! shutdown_server; then -# kill -SIGTERM "$(pidof PalServer-Linux-Test)" LogWarn "Unable to shutdown the server for unknown reasons." DiscordMessage "Shutdown" "Unable to shutdown the server for unknown reasons." "failure" "${DISCORD_PRE_SHUTDOWN_MESSAGE_ENABLED}" "${DISCORD_PRE_SHUTDOWN_MESSAGE_URL}" return 1 diff --git a/scripts/start.sh b/scripts/start.sh index 036f188..0ec3db0 100644 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -10,7 +10,9 @@ dirExists "$DATA_DIR" || exit isWritable "$DATA_DIR" || exit isExecutable "$DATA_DIR" || exit -if [ "$architecture" == "arm64" ] && [ "${ARM_COMPATIBILITY_MODE,,}" = true ]; then +cd "$GIT_REPO_PATH" || exit 1 + +if [ "$ARCHITECTURE" == "arm64" ] && [ "${ARM_COMPATIBILITY_MODE,,}" = true ]; then LogInfo "ARM compatibility mode enabled" export DEBUGGER="/usr/bin/qemu-i386-static" @@ -18,6 +20,12 @@ if [ "$architecture" == "arm64" ] && [ "${ARM_COMPATIBILITY_MODE,,}" = true ]; t export CPU_MHZ=2000 fi +if ! CheckCommitID "$TARGET_COMMIT_ID"; then + LogError "Invalid TARGET_COMMIT_ID($TARGET_COMMIT_ID)" + sleep infinity + exit 1 +fi + IsInstalled ServerInstalled=$? if [ "$ServerInstalled" == 1 ]; then @@ -26,9 +34,6 @@ if [ "$ServerInstalled" == 1 ]; then InstallServer fi -mkdir -p "$SERVER_BACKUP_DIR" -cd "$GIT_REPO_PATH" || exit 1 - # Update Only If Already Installed if [ "$ServerInstalled" == 0 ] && [ "${UPDATE_ON_BOOT,,}" == true ]; then UpdateRequired @@ -40,10 +45,10 @@ if [ "$ServerInstalled" == 0 ] && [ "${UPDATE_ON_BOOT,,}" == true ]; then fi # Check if the architecture is arm64 -if [ "$architecture" == "arm64" ]; then +if [ "$ARCHITECTURE" == "arm64" ]; then box64_binary="box64" - case $pagesize in + case $PAGESIZE in 8192) LogInfo "Using Box64 for 8k pagesize" box64_binary="box64-8k" diff --git a/scripts/variables.sh b/scripts/variables.sh index 757e2a0..e2c865d 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -4,16 +4,17 @@ export HOME="/home/steam" export DATA_DIR="/data" export GIT_REPO_NAME="longvinter-linux-server" export GIT_REPO_BRANCH="main" -export GIT_REPO_URL="https://github.com/Uuvana-Studios/$GIT_REPO_NAME.git" +export GIT_REPO_URL="https://github.com/Uuvana-Studios/$GIT_REPO_NAME" export GIT_REPO_PATH="$DATA_DIR" +export GIT_REPO_API="${GIT_REPO_URL/github\.com/api.github.com\/repos}" export SERVER_CONFIG_PATH="/Longvinter/Saved/Config/LinuxServer/Game.ini" export SERVER_BACKUP_DIR="$GIT_REPO_PATH/Longvinter/Backup" export SERVER_LOG_PATH="$GIT_REPO_PATH/Longvinter/Saved/Logs/Longvinter.log" export CONFIG_FILE_FULL_PATH="$GIT_REPO_PATH/$SERVER_CONFIG_PATH" -architecture=$(dpkg --print-architecture) -pagesize=$(getconf PAGESIZE) -export architecture pagesize +ARCHITECTURE=$(dpkg --print-architecture) +PAGESIZE=$(getconf PAGESIZE) +export ARCHITECTURE PAGESIZE