From 78007f3d3f0d79884c86ada5ff132881184f7887 Mon Sep 17 00:00:00 2001 From: Dirk Kok Date: Tue, 5 Mar 2024 20:30:04 +0100 Subject: [PATCH] copy some tech improvements from fridgebot (simplified docker-compose, version command --- .github/workflows/main.yml | 8 +++++++ HighlightBot/Dockerfile | 6 +++++ HighlightBot/HighlightCommandModule.cs | 11 +++++++++ HighlightBot/SlashCommandModule.cs | 11 +++++++++ docker-compose.dev.yml | 27 ---------------------- docker-compose.prod.yml | 25 --------------------- docker-compose.yml | 31 ++++++++++++++++++++++++++ postgres.env.example | 3 --- 8 files changed, 67 insertions(+), 55 deletions(-) delete mode 100644 docker-compose.dev.yml delete mode 100644 docker-compose.prod.yml create mode 100644 docker-compose.yml delete mode 100644 postgres.env.example diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index adf3282..fb96307 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,6 +10,13 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - id: compute-version-code + shell: bash + run: echo "release-version=$(echo ${GITHUB_SHA:0:8})" >> $GITHUB_OUTPUT + - name: Publish Docker + uses: elgohr/Publish-Docker-Github-Action@3.04 + env: + HIGHLIGHTBOT_VERSION: ${{ steps.compute-version-code.outputs.release-version }} - name: Publish Docker uses: elgohr/Publish-Docker-Github-Action@3.04 with: @@ -20,3 +27,4 @@ jobs: password: ${{ secrets.REPO_PASSWD }} default_branch: master no_push: ${{ github.event_name == 'pull_request' }} + buildargs: HIGHLIGHTBOT_VERSION diff --git a/HighlightBot/Dockerfile b/HighlightBot/Dockerfile index b4b85b4..355359d 100644 --- a/HighlightBot/Dockerfile +++ b/HighlightBot/Dockerfile @@ -7,6 +7,12 @@ RUN mv appsettings.json.example appsettings.json RUN dotnet publish "HighlightBot.csproj" -c Release -o /app/publish FROM mcr.microsoft.com/dotnet/runtime:6.0-alpine AS final + +ARG FRIDGEBOT_VERSION + WORKDIR /app COPY --from=build /app/publish . + +ENV FRIDGEBOT_VERSION=$FRIDGEBOT_VERSION + ENTRYPOINT ["./HighlightBot"] diff --git a/HighlightBot/HighlightCommandModule.cs b/HighlightBot/HighlightCommandModule.cs index 41c4b4b..5526968 100644 --- a/HighlightBot/HighlightCommandModule.cs +++ b/HighlightBot/HighlightCommandModule.cs @@ -17,6 +17,17 @@ public override Task BeforeExecutionAsync(CommandContext ctx) { return Task.CompletedTask; } + [Command("version")] + public Task ShowVersion(CommandContext context) { + string? response = Environment.GetEnvironmentVariable("HIGHLIGHTBOT_VERSION"); + + if (string.IsNullOrWhiteSpace(response)) { + response = "Unset"; + } + + return context.RespondAsync(response); + } + [Command("show")] public Task GetAllHighlights(CommandContext context) { return Session.GetAllHighlights(Hcc); diff --git a/HighlightBot/SlashCommandModule.cs b/HighlightBot/SlashCommandModule.cs index 8b5a254..49f12ed 100644 --- a/HighlightBot/SlashCommandModule.cs +++ b/HighlightBot/SlashCommandModule.cs @@ -17,6 +17,17 @@ public override Task BeforeSlashExecutionAsync(InteractionContext ctx) { return Task.FromResult(true); } + [SlashCommand("version", "Get the bot's version.")] + public Task ShowVersion(InteractionContext context) { + string? response = Environment.GetEnvironmentVariable("HIGHLIGHTBOT_VERSION"); + + if (string.IsNullOrWhiteSpace(response)) { + response = "Unset"; + } + + return context.CreateResponseAsync(response); + } + [SlashCommand("show", "Show all your highlights.")] public Task GetAllHighlights(InteractionContext context) { return Session.GetAllHighlights(Hcc); diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml deleted file mode 100644 index f1df94e..0000000 --- a/docker-compose.dev.yml +++ /dev/null @@ -1,27 +0,0 @@ -version: "3.3" - -services: - database: - image: "postgres:13.3" - env_file: - - postgres.env - ports: - - "5432:5432" - networks: - - highlightbot-network - volumes: - - database-data:/var/lib/postgresql/data/ # persist data even if container shuts down - bot: - build: HighlightBot - env_file: - - highlightbot.env - depends_on: - - database - networks: - - highlightbot-network - -networks: - highlightbot-network: - -volumes: - database-data: # named volumes can be managed easier using docker-compose diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml deleted file mode 100644 index 265841d..0000000 --- a/docker-compose.prod.yml +++ /dev/null @@ -1,25 +0,0 @@ -version: "3.3" - -services: - database: - image: "postgres:13.3" - env_file: - - postgres.env - networks: - - highlightbot-network - volumes: - - database-data:/var/lib/postgresql/data/ # persist data even if container shuts down - bot: - build: HighlightBot - env_file: - - highlightbot.env - depends_on: - - database - networks: - - highlightbot-network - -networks: - highlightbot-network: - -volumes: - database-data: # named volumes can be managed easier using docker-compose diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..3c8ef25 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,31 @@ +version: "3.3" + +services: + database: + image: "postgres:13.3" + environment: + POSTGRES_USER: highlightbot + POSTGRES_PASSWORD: test123 + POSTGRES_DB: highlightbot-data + ports: + - "5432:5432" + networks: + - highlightbot-network + volumes: + - database-data:/var/lib/postgresql/data/ # persist data even if container shuts down + bot: + build: HighlightBot + environment: + HIGHLIGHT_Discord__Token: "try not to commit this line." + HIGHLIGHT_DiscordNotifications__WebhookUrl: "" + HIGHLIGHT_ConnectionStrings__HighlightDbContext: "Host=database; Database=highlightbot-data; Port=5432; Username=highlightbot; Password=test123" + depends_on: + - database + networks: + - highlightbot-network + +networks: + highlightbot-network: + +volumes: + database-data: # named volumes can be managed easier using docker-compose diff --git a/postgres.env.example b/postgres.env.example deleted file mode 100644 index 1375078..0000000 --- a/postgres.env.example +++ /dev/null @@ -1,3 +0,0 @@ -POSTGRES_USER=highlightbot -POSTGRES_PASSWORD=test123 -POSTGRES_DB=highlightbot-data