dev/install (#58)
All checks were successful
Tests / Run Go Tests (push) Successful in 1m24s

improved ci/cd pipeline + interactive install script

Reviewed-on: #58
Co-authored-by: Tom Tröger <t.troeger.02@gmail.com>
Co-committed-by: Tom Tröger <t.troeger.02@gmail.com>
This commit is contained in:
2025-11-30 21:25:47 +01:00
committed by Tom Tröger
parent 7e5eaebca9
commit 6ab48eb534
9 changed files with 132 additions and 19 deletions

View File

@@ -2,7 +2,8 @@ name: Arbeitszeitmessung Deploy
run-name: ${{ gitea.actor }} is building and deploying arbeitszeitmesssung
on:
push:
tags: "*"
tags:
- "*"
jobs:
testing:

View File

@@ -15,8 +15,8 @@ jobs:
POSTGRES_DB: arbeitszeitmessung
env:
POSTGRES_HOST: postgres
POSTGRES_USER: root
POSTGRES_PASSWORD: password
POSTGRES_API_USER: root
POSTGRES_API_PASS: password
POSTGRES_DB: arbeitszeitmessung
POSTGRES_PORT: 5432
RUNNER_TOOL_CACHE: /toolcache # Runner Tool Cache

View File

@@ -14,7 +14,8 @@ func OpenDatabase() (models.IDatabase, error) {
dbName := helper.GetEnv("POSTGRES_DB", "arbeitszeitmessung")
dbUser := helper.GetEnv("POSTGRES_API_USER", "api_nutzer")
dbPassword := helper.GetEnv("POSTGRES_API_PASS", "password")
dbTz := helper.GetEnv("TZ", "Europe/Berlin")
connStr := fmt.Sprintf("postgres://%s:%s@%s:5432/%s?sslmode=disable&TimeZone=Europe/Berlin", dbUser, dbPassword, dbHost, dbName)
connStr := fmt.Sprintf("postgres://%s:%s@%s:5432/%s?sslmode=disable&TimeZone=%s", dbUser, dbPassword, dbHost, dbName, dbTz)
return sql.Open("postgres", connStr)
}

View File

@@ -31,7 +31,6 @@ func FormatDuration(d time.Duration) string {
// Converts duration to string
func FormatDurationFill(d time.Duration, fill bool) string {
return fmt.Sprintf("%.1f", d.Hours())
hours := int(d.Abs().Hours())
minutes := int(d.Abs().Minutes()) % 60
sign := ""

View File

@@ -1,11 +0,0 @@
POSTGRES_USER=root
POSTGRES_PASSWORD=very_secure
POSTGRES_API_USER=api_nutzer
POSTGRES_API_PASS=password
POSTGRES_PATH=../DB
POSTGRES_DB=arbeitszeitmessung
EXPOSED_PORT=8000
TZ=Europe/Berlin
PGTZ=Europe/Berlin
API_TOKEN=dont_access
EMPTY_DAYS=false

View File

@@ -6,9 +6,7 @@ services:
env_file:
- .env
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
PGTZ: ${TZ}
PGDATA: /var/lib/postgresql/data/pg_data
volumes:
- ${POSTGRES_PATH}:/var/lib/postgresql/data

10
Docker/env.example Normal file
View File

@@ -0,0 +1,10 @@
POSTGRES_USER=root # Postgres ADMIN Nutzername
POSTGRES_PASSWORD=very_secure # Postgres ADMIN Passwort
POSTGRES_API_USER=api_nutzer # Postgres API Nutzername (für Arbeitszeitmessung)
POSTGRES_API_PASS=password # Postgres API Passwort (für Arbeitszeitmessung)
POSTGRES_PATH=../DB # Datebank Pfad (relativ zu Docker Ordner oder absoluter pfad mit /...)
POSTGRES_DB=arbeitszeitmessung # Postgres Datenbank Name
EXPOSED_PORT=8000 # Port auf welchem Arbeitszeitmessung läuft regex:^[0-9]{1,5}$
TZ=Europe/Berlin # Zeitzone
PGTZ=Europe/Berlin # Zeitzone
API_TOKEN=dont_access # API Token für ESP Endpoints

View File

@@ -52,3 +52,11 @@ test:
scan: test
$(MAKE) -C Backend scan
Docker/.env:
cp Docker/env.example Docker/.env
echo "Konfigurations Datei erstellt (./Docker/.env), bitte zuerst ausfüllen und danach erneut 'make install' ausführen"
exit 0
install: Docker/.env
echo "Install"

107
install.sh Executable file
View File

@@ -0,0 +1,107 @@
#!/usr/bin/env bash
set -e
envFile=Docker/.env
envExample=Docker/env.example
echo "Checking Docker installation..."
if ! command -v docker >/dev/null 2>&1; then
echo "Docker not found. Install Docker? [y/N]"
read -r install_docker
if [[ "$install_docker" =~ ^[Yy]$ ]]; then
curl -fsSL https://get.docker.com | sh
else
echo "Docker is required. Exiting."
exit 1
fi
else
echo "Docker is already installed."
fi
echo "Checking Docker Compose..."
if ! docker compose version >/dev/null 2>&1; then
echo "Docker Compose plugin missing. You may need to update Docker."
exit 1
fi
echo "Preparing .env file..."
if [ ! -f $envFile ]; then
if [ -f $envExample ]; then
echo ".env not found. Creating interactively from .env.example."
> $envFile
while IFS= read -r line; do
#ignore empty lines and comments
[[ "$line" =~ ^#.*$ || -z "$line" ]] && continue
key=$(printf "%s" "$line" | cut -d '=' -f 1)
rest=$(printf "%s" "$line" | cut -d '=' -f 2-)
# extract inline comment portion
comment=$(printf "%s" "$rest" | sed -n 's/.*# \(.*\)$/\1/p')
raw_val=$(printf "%s" "$rest" | sed 's/ *#.*//')
default_value=$(printf "%s" "$raw_val" | sed 's/"//g')
regex=""
if [[ "$comment" =~ regex:(.*)$ ]]; then
regex="${BASH_REMATCH[1]}"
fi
comment=$(printf "%s" "$comment" | sed 's/ regex:.*//')
while true; do
if [ -z "$comment" ]; then
printf "Value for $key - $comment (default: $default_value"
else
printf "Value for $key (default: $default_value"
fi
if [ -n "$regex" ]; then
printf ", must match: %s" "$regex"
fi
printf "):\n"
read user_input < /dev/tty
# empty input -> take default
[ -z "$user_input" ] && user_input="$default_value"
printf "\e[A$user_input\n"
# validate
if [ -n "$regex" ]; then
if [[ "$user_input" =~ $regex ]]; then
echo "$key=$user_input" >> $envFile
break
else
printf "Invalid value. Does not match regex: %s\n" "$regex"
continue
fi
else
echo "$key=$user_input" >> $envFile
break
fi
done
done < $envExample
echo ".env created."
else
echo "No .env or .env.example found."
echo "Creating an empty .env file for manual editing."
touch $envFile
fi
else
echo "Using existing .env. (found at $envFile)"
fi
echo "Start containers with docker compose up -d? [y/N]"
read -r start_containers
if [[ "$start_containers" =~ ^[Yy]$ ]]; then
cd Docker
docker compose up -d
echo "Containers started."
else
echo "You can start them manually with: docker compose up -d"
fi