From 591020f04bb9d0231aa7d6d6c2f314b7f9e0df9f Mon Sep 17 00:00:00 2001 From: shiroyashik Date: Sun, 19 Jan 2025 17:59:44 +0300 Subject: [PATCH] Auto Migrations + Docker --- .github/workflows/docker-push.yml | 42 +++++++++++++++++++ .gitignore | 1 + Cargo.lock | 1 + Cargo.toml | 3 +- Dockerfile | 39 +++++++++++++++++ {database/migration => migration}/Cargo.toml | 0 {database/migration => migration}/README.md | 0 {database/migration => migration}/src/lib.rs | 0 .../src/m20241211_182453_create_tables.rs | 0 {database/migration => migration}/src/main.rs | 0 src/main.rs | 4 ++ 11 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/docker-push.yml create mode 100644 Dockerfile rename {database/migration => migration}/Cargo.toml (100%) rename {database/migration => migration}/README.md (100%) rename {database/migration => migration}/src/lib.rs (100%) rename {database/migration => migration}/src/m20241211_182453_create_tables.rs (100%) rename {database/migration => migration}/src/main.rs (100%) diff --git a/.github/workflows/docker-push.yml b/.github/workflows/docker-push.yml new file mode 100644 index 0000000..a54453d --- /dev/null +++ b/.github/workflows/docker-push.yml @@ -0,0 +1,42 @@ +name: Docker Push + +on: + push: + branches: [ "master" ] + +jobs: + docker: + runs-on: ubuntu-latest + steps: + # - name: Checkout code + # uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + # - name: Login to Docker Hub + # uses: docker/login-action@v3 + # with: + # username: ${{ vars.DOCKERHUB_USERNAME }} + # password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Get short SHA + id: short_sha + run: echo "sha=$(echo ${GITHUB_SHA} | cut -c1-7)" >> $GITHUB_OUTPUT + + - name: Build and push + uses: docker/build-push-action@v6 + with: + push: true + # context: . + tags: ghcr.io/${{ github.repository_owner }}/doggy-watch:${{ steps.short_sha.outputs.sha }},ghcr.io/${{ github.repository_owner }}/doggy-watch:latest + cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/doggy-watch:buildcache + cache-to: type=registry,ref=ghcr.io/${{ github.repository_owner }}/doggy-watch:buildcache,mode=max + diff --git a/.gitignore b/.gitignore index d5f21ec..d573a03 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /act .env note.txt +docker-compose.yaml \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 9ef75a2..a15a5a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -803,6 +803,7 @@ dependencies = [ "dotenvy", "indexmap", "lazy_static", + "migration", "sea-orm", "teloxide", "tokio", diff --git a/Cargo.toml b/Cargo.toml index 37eaaf9..b47d109 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,11 +6,12 @@ edition = "2021" publish = false [workspace] -members = [ "database", "youtube", "database/migration" ] +members = [ "database", "youtube", "migration" ] [dependencies] database = { path = "database" } youtube = { path = "youtube" } +migration = { path = "migration"} anyhow = "1.0" dotenvy = "0.15" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a743db1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +## Chef +# FROM clux/muslrust:stable AS chef +FROM rust:1.84.0-alpine3.20 AS chef +USER root +RUN apk add --no-cache musl-dev libressl-dev +RUN cargo install cargo-chef +WORKDIR /build + +## Planner +FROM chef AS planner +COPY Cargo.toml Cargo.lock ./ +COPY ./migration ./migration +COPY ./database ./database +COPY ./youtube ./youtube +COPY src src +RUN cargo chef prepare --recipe-path recipe.json + +## Builder +FROM chef AS builder +COPY --from=planner /build/recipe.json recipe.json +# Build dependencies - this is the caching Docker layer! +RUN cargo chef cook --release --target x86_64-unknown-linux-musl --recipe-path recipe.json +# Build application +COPY Cargo.toml Cargo.lock ./ +COPY ./migration ./migration +COPY ./database ./database +COPY ./youtube ./youtube +COPY src src +RUN cargo build --release --target x86_64-unknown-linux-musl --bin doggy-watch + +## Runtime +FROM alpine:3.20.0 AS runtime +WORKDIR /app +COPY --from=builder /build/target/x86_64-unknown-linux-musl/release/doggy-watch /app/doggy-watch + +RUN apk add --no-cache tzdata +ENV TZ=Etc/UTC + +ENTRYPOINT [ "./doggy-watch" ] \ No newline at end of file diff --git a/database/migration/Cargo.toml b/migration/Cargo.toml similarity index 100% rename from database/migration/Cargo.toml rename to migration/Cargo.toml diff --git a/database/migration/README.md b/migration/README.md similarity index 100% rename from database/migration/README.md rename to migration/README.md diff --git a/database/migration/src/lib.rs b/migration/src/lib.rs similarity index 100% rename from database/migration/src/lib.rs rename to migration/src/lib.rs diff --git a/database/migration/src/m20241211_182453_create_tables.rs b/migration/src/m20241211_182453_create_tables.rs similarity index 100% rename from database/migration/src/m20241211_182453_create_tables.rs rename to migration/src/m20241211_182453_create_tables.rs diff --git a/database/migration/src/main.rs b/migration/src/main.rs similarity index 100% rename from database/migration/src/main.rs rename to migration/src/main.rs diff --git a/src/main.rs b/src/main.rs index fa38886..a4be837 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use std::{env::var, sync::Arc, time::Duration}; use dashmap::DashMap; use database::moderators; +use migration::{Migrator, MigratorTrait}; use sea_orm::{prelude::*, sea_query::OnConflict, ConnectOptions, Database, Set}; use teloxide::{ dispatching::dialogue::InMemStorage, @@ -66,6 +67,9 @@ async fn main() -> anyhow::Result<()> { opt.sqlx_logging_level(tracing::log::LevelFilter::Trace); let db: DatabaseConnection = Database::connect(opt).await?; + // applying migrations + Migrator::up(&db, None).await?; + // add administrators to db { let admins: Vec = ADMINISTRATORS.iter().map(|&x| {