Compare commits

...

3 Commits

Author SHA1 Message Date
58bd07d556
chore: remove unnecessary step
All checks were successful
Create and publish a Docker image / build-and-push-image (push) Successful in 1m29s
Default docker image in Gitea runner now ships with Docker
2023-12-19 20:09:05 +01:00
ad014dd1ae
chore: simplify Dockerfile 2023-12-19 20:09:05 +01:00
60a81f66a8
feat: Enable Docker deployment and CD
All checks were successful
Create and publish a Docker image / build-and-push-image (push) Successful in 8m1s
Closes #8, partially addresses #6
2023-11-26 04:22:56 +01:00
8 changed files with 143 additions and 33 deletions

3
.dockerignore Normal file
View File

@ -0,0 +1,3 @@
/assets
/.sqlx
/.env.example

View File

@ -1,2 +1,5 @@
DISCORD_TOKEN=changeme
# Only useful when developing locally. Do not change it when deploying
# with Docker.
DATABASE_URL=sqlite:p4bl0t.db

View File

@ -0,0 +1,40 @@
name: Create and publish a Docker image
on:
push:
branches: ['main', 'feature/docker']
env:
REGISTRY: labs.phundrak.com
IMAGE_NAME: ${{ gitea.repository }}
jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ env.REGISTRY }}
username: ${{ gitea.actor }}
password: ${{ secrets.DOCKER_REGISTRY_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

45
Dockerfile Normal file
View File

@ -0,0 +1,45 @@
ARG RUST_VERSION=1.73.0
FROM rust:${RUST_VERSION}-slim-bullseye AS build
RUN --mount=type=cache,target=/usr/local/cargo/registry \
cargo install sqlx-cli --no-default-features --features rustls,sqlite && \
cp /usr/local/cargo/bin/sqlx /bin/sqlx
ENV DATABASE_URL=sqlite:/var/p4bl0t.db
WORKDIR /app
RUN --mount=type=bind,source=src,target=src \
--mount=type=bind,source=Cargo.toml,target=Cargo.toml \
--mount=type=bind,source=Cargo.lock,target=Cargo.lock \
--mount=type=bind,source=migrations,target=migrations \
--mount=type=cache,target=/app/target/ \
--mount=type=cache,target=/usr/local/cargo/registry \
<<EOF
set -e
sqlx database create
sqlx migrate run
cargo install --locked --path .
EOF
FROM debian:bullseye-slim AS final
RUN apt-get update && apt-get install -qqy ca-certificates
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
USER appuser
ENV DATABASE_URL=sqlite:/var/p4bl0t.db
ENV DISCORD_TOKEN=changeme
COPY --from=build /usr/local/cargo/bin/p4bl0t /bin
COPY --chown=appuser --from=build /var/p4bl0t.db /var/p4bl0t.db
CMD [ "p4bl0t" ]

8
docker-compose.yml Normal file
View File

@ -0,0 +1,8 @@
version: '3'
services:
p4bl0t:
env_file: .env
build:
context: .
target: final

View File

@ -4,7 +4,7 @@ use std::env;
use poise::serenity_prelude::{ChannelId, GuildId};
use sqlx::SqlitePool;
use tracing::error;
use tracing::{error, info};
pub type Result<T> = ::std::result::Result<T, sqlx::Error>;
@ -24,14 +24,12 @@ impl Database {
///
/// This function will return an error if the Sqlite pool fails to
/// create.
// TODO: Create the database if it doesnt exist already and run migrations
pub async fn new() -> Result<Self> {
Ok(Self(
SqlitePool::connect(
&env::var("DATABASE_URL")
.expect("Missing enviroment variable DATABASE_URL"),
)
.await?,
))
let db_url = env::var("DATABASE_URL")
.expect("Missing enviroment variable DATABASE_URL");
info!("Connecting to database located at {db_url}");
Ok(Self(SqlitePool::connect(&db_url).await?))
}
/// Return from database all channels registered as loggers for a

View File

@ -1,9 +1,10 @@
mod commands;
pub mod error;
mod events;
pub mod utils;
pub mod error;
use poise::FrameworkBuilder;
use tracing::info;
use utils::serenity;
use commands::logging;
@ -19,24 +20,30 @@ pub type Result = ::std::result::Result<(), Error>;
///
/// Panics if the environment `DISCORD_TOKEN` is unavailable.
pub fn make_bot() -> FrameworkBuilder<BotData, Error> {
poise::Framework::builder()
.options(poise::FrameworkOptions {
commands: vec![logging()],
event_handler: |ctx, event, framework, data| {
Box::pin(event_handler(ctx, event, framework, data))
},
..Default::default()
})
.token(std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN"))
.intents(serenity::GatewayIntents::non_privileged())
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(
ctx,
&framework.options().commands,
)
.await?;
Ok(BotData::new().await?)
})
})
match std::env::var("DISCORD_TOKEN") {
Ok(token) => {
info!("Launching bot with token {token}");
poise::Framework::builder()
.options(poise::FrameworkOptions {
commands: vec![logging()],
event_handler: |ctx, event, framework, data| {
Box::pin(event_handler(ctx, event, framework, data))
},
..Default::default()
})
.token(token)
.intents(serenity::GatewayIntents::non_privileged())
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(
ctx,
&framework.options().commands,
)
.await?;
Ok(BotData::new().await?)
})
})
}
Err(_) => panic!("DISCORD_TOKEN environment variable is missing."),
}
}

View File

@ -1,17 +1,23 @@
#![warn(clippy::style, clippy::pedantic)]
mod utils;
mod db;
mod discord;
mod utils;
use std::error::Error;
use tracing::info;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
dotenvy::dotenv()?;
color_eyre::install()?;
println!("Setting logging up");
utils::setup_logging();
info!("Setting up color_eyre");
color_eyre::install()?;
info!("Reading from dotenv");
let _ =
dotenvy::dotenv().map_err(|_| info!("No .env file found, skipping"));
info!("Launching bot");
let bot = discord::make_bot();
bot.run().await?;