Skip to content

Commit

Permalink
Add TARGET_COMMIT_ID
Browse files Browse the repository at this point in the history
  • Loading branch information
kimzuni committed Mar 31, 2024
1 parent 05dc7fd commit 3a9ffc8
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 40 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/developer.yml
Original file line number Diff line number Diff line change
@@ -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 }}
14 changes: 7 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
Expand Down Expand Up @@ -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"]
9 changes: 2 additions & 7 deletions scripts/helper_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 $?
}
Expand All @@ -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"
Expand Down
42 changes: 32 additions & 10 deletions scripts/helper_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -16,32 +27,40 @@ 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
}

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}"
}

Expand All @@ -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}"
Expand Down
14 changes: 8 additions & 6 deletions scripts/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
17 changes: 11 additions & 6 deletions scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ 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"

# Arbitrary number to avoid CPU_MHZ warning due to qemu and steamcmd
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
Expand All @@ -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
Expand All @@ -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"
Expand Down
9 changes: 5 additions & 4 deletions scripts/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down

0 comments on commit 3a9ffc8

Please sign in to comment.