From 1cf5f07c80aa41eff597a1e8f2a61e75800532de Mon Sep 17 00:00:00 2001 From: Jose Henrique Date: Sat, 15 Feb 2025 22:20:19 -0300 Subject: [PATCH] Add CI workflow for Docker build and deployment; update Dockerfile and adjust health reduction logic in PetActionService; modify database connection string in appsettings.json --- .gitea/workflows/main.yml | 63 ++++++++++++++++++++++++++++++++++++ Dockerfile | 3 -- Services/PetActionService.cs | 4 +-- appsettings.json | 2 +- 4 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 .gitea/workflows/main.yml diff --git a/.gitea/workflows/main.yml b/.gitea/workflows/main.yml new file mode 100644 index 0000000..a0a5003 --- /dev/null +++ b/.gitea/workflows/main.yml @@ -0,0 +1,63 @@ +name: Main Build & Deploy + +on: workflow_dispatch + +jobs: + build: + name: Build and Push Docker Image (amd64 and arm64) + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Install docker and docker compose + run: | + apt update + apt install -y docker docker-compose + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v2 + with: + config-inline: | + [registry."git.ivanch.me"] + + - name: Login to Docker Hub + uses: https://github.com/docker/login-action@v3.3.0 + with: + registry: git.ivanch.me + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_ACCESS_TOKEN }} + + - name: Build Docker image and push + id: docker_build + uses: https://github.com/docker/build-push-action@v6.12.0 + with: + context: ./ + file: ./Dockerfile + push: true + tags: git.ivanch.me/ivanch/pet-companion/pet-companion-api:latest + platforms: linux/amd64, linux/arm64 + + - name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }} + + deploy_live: + name: Deploy Live + runs-on: ubuntu-latest + needs: build + + steps: + - name: Recreate container + uses: https://github.com/appleboy/ssh-action@v1.2.0 + with: + host: ${{ secrets.LIVE_HOST }} + username: ${{ secrets.LIVE_USERNAME }} + key: ${{ secrets.LIVE_KEY }} + port: ${{ secrets.LIVE_PORT }} + script: | + cd ${{ secrets.LIVE_PROJECT_DIR }} + docker compose down + docker compose rm + docker compose pull + docker compose up -d diff --git a/Dockerfile b/Dockerfile index 03dbd9c..e95b408 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,3 @@ -# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. - -# This stage is used when running from VS in fast mode (Default for Debug configuration) FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base USER app WORKDIR /app diff --git a/Services/PetActionService.cs b/Services/PetActionService.cs index cf985a1..f30905f 100644 --- a/Services/PetActionService.cs +++ b/Services/PetActionService.cs @@ -108,7 +108,7 @@ namespace PetCompanion.Services pet.Health -= 5; } - if (random.Next(0, 100) < 5) + if (random.Next(0, 100) < 2) { gathered.Add(new ActionGathered { @@ -125,7 +125,7 @@ namespace PetCompanion.Services Amount = (int)(baseRate * (pet.Stats.Strength * 0.25)) }); - if (random.Next(0, 100) < 10) + if (random.Next(0, 100) < 5) { pet.Health -= random.Next(5, 10); } diff --git a/appsettings.json b/appsettings.json index fe2d73e..a6b53a6 100644 --- a/appsettings.json +++ b/appsettings.json @@ -7,6 +7,6 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "DefaultConnection": "Data Source=petcompanion.db" + "DefaultConnection": "Data Source=game-data/petcompanion.db" } }