From 33847a20080e321d3d677f9f66906e7f234f09b1 Mon Sep 17 00:00:00 2001 From: Anson Biggs Date: Mon, 10 Jul 2023 02:45:59 +0000 Subject: [PATCH] Add CI/CD to project --- .gitlab-ci.yml | 30 ++++++++++++++++++++++++++++++ Cargo.lock | 26 +++++++++++++------------- Cargo.toml | 6 +++--- Dockerfile | 14 ++++++++++++++ docker-compose.yml | 6 ++++++ src/main.rs | 16 ++++++---------- 6 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 .gitlab-ci.yml create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..60e4d78 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,30 @@ +build:main: + stage: build + image: + name: gcr.io/kaniko-project/executor:v1.9.0-debug + entrypoint: [""] + script: + - /kaniko/executor + --context "${CI_PROJECT_DIR}" + --dockerfile "${CI_PROJECT_DIR}/Dockerfile" + --destination "${CI_REGISTRY_IMAGE}:latest" + --destination "${CI_REGISTRY_IMAGE}:stable" + --destination "${CI_REGISTRY_IMAGE}:dev" + rules: + - if: '$CI_COMMIT_BRANCH == "main"' + + +build:branch: + stage: build + image: + name: gcr.io/kaniko-project/executor:v1.9.0-debug + entrypoint: [""] + script: + - /kaniko/executor + --context "${CI_PROJECT_DIR}" + --dockerfile "${CI_PROJECT_DIR}/Dockerfile" + --destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHORT_SHA}" + --destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_BRANCH}" + --destination "${CI_REGISTRY_IMAGE}:dev" + rules: + - if: '$CI_COMMIT_BRANCH != "main"' diff --git a/Cargo.lock b/Cargo.lock index 246da64..60982ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -993,19 +993,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "rust" -version = "0.1.0" -dependencies = [ - "log", - "pretty_env_logger", - "serde", - "serde_json", - "teloxide", - "tokio", - "ureq", -] - [[package]] name = "rustc-demangle" version = "0.1.23" @@ -1693,6 +1680,19 @@ dependencies = [ "rustls-webpki 0.100.1", ] +[[package]] +name = "wiki_location_bot" +version = "0.1.0" +dependencies = [ + "log", + "pretty_env_logger", + "serde", + "serde_json", + "teloxide", + "tokio", + "ureq", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 7665fe9..05504b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,12 @@ [package] -name = "rust" +name = "wiki_location_bot" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + [dependencies] -teloxide = { version = "0.12", features = ["macros"] } +teloxide = { version = "0.12.2", features = ["macros"] } log = "0.4" pretty_env_logger = "0.5.0" tokio = { version = "1.8", features = ["rt-multi-thread", "macros"] } diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ae31faa --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM rust:1.70 as builder + +WORKDIR /usr/src/app + +COPY . . +RUN cargo build --release + +FROM debian:bullseye-slim + +COPY --from=builder /usr/src/app/target/release/wiki_location_bot . +RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/* + + +CMD ["./wiki_location_bot"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9aae6bd --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,6 @@ +version: "3" +services: + app: + build: . + +# docker-compose up --build app diff --git a/src/main.rs b/src/main.rs index 3907f05..4c98236 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,9 +21,9 @@ pub struct GeoSearch { pageid: usize, ns: usize, title: String, - lat: Option, - lon: Option, - dist: Option, + lat: f64, + lon: f64, + dist: f32, primary: Option, } @@ -52,28 +52,24 @@ async fn main() { match msg.location() { Some(user_location) => { log::info!("Location received."); - bot.send_message(msg.chat.id, "Searching for nearby locations..."); + bot.send_message(msg.chat.id, "Searching for nearby locations...").await?; let nearby_locations = ureq::get(&format!("https://en.wikipedia.org/w/api.php?action=query&format=json&list=geosearch&formatversion=2&gscoord={}|{}&gsradius=10000&gslimit=5",user_location.latitude,user_location.longitude)) .call() .unwrap() .into_json::().unwrap().query.geosearch.unwrap(); - - + // Not working, and also the wikipedia API is so fast its not necessary. // bot.send_chat_action(msg.chat.id,teloxide::types::ChatAction::FindLocation).await?; for location in nearby_locations { - bot.send_location(msg.chat.id,location.lat.unwrap(),location.lon.unwrap()).await?; + bot.send_location(msg.chat.id,location.lat,location.lon).await?; let url = teloxide::utils::markdown::link(&format!("http://en.wikipedia.org/?curid={}",location.pageid),&location.title); let bold_url = teloxide::utils::markdown::bold(&url); bot.send_message(msg.chat.id, bold_url).parse_mode(teloxide::types::ParseMode::MarkdownV2).await?; - - - } } None => {