From 00a38cbf03b04b76dfbcc2de2d2d0b10c2c1205e Mon Sep 17 00:00:00 2001 From: root Date: Sun, 12 Feb 2023 22:36:01 +0000 Subject: [PATCH] Updated to jenkins and gitea (#4) Co-authored-by: Mutzi Reviewed-on: https://gitea.mattv.de/root/dotfiles/pulls/4 --- .gitlab-ci.yml | 19 ------------------- Cargo.lock | 5 +++-- Cargo.toml | 1 + Jenkinsfile | 20 ++++++++++++++++++++ src/main.rs | 2 +- src/repository.rs | 2 +- src/update.rs | 27 +++++++++++---------------- 7 files changed, 37 insertions(+), 39 deletions(-) delete mode 100644 .gitlab-ci.yml create mode 100644 Jenkinsfile diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml deleted file mode 100644 index e875bfd..0000000 --- a/.gitlab-ci.yml +++ /dev/null @@ -1,19 +0,0 @@ -stages: - - build - -variables: - PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/installer" - -build: - stage: build - image: rust:alpine - rules: - - if: $CI_COMMIT_BRANCH == "installer" - script: - - apk add pkgconf musl-dev curl - - RUSTFLAGS='-C target-feature=+crt-static -C link-self-contained=yes -C link-arg=-s' cargo build --release --target x86_64-unknown-linux-musl - - cp target/x86_64-unknown-linux-musl/release/dotfiles_installer ./installer-amd64 - - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file installer-amd64 "${PACKAGE_REGISTRY_URL}/dev-$CI_COMMIT_SHA/installer-amd64"' - artifacts: - paths: - - installer-amd64 diff --git a/Cargo.lock b/Cargo.lock index 9b41dc0..2dddb2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -87,6 +87,7 @@ dependencies = [ "dialoguer", "indicatif", "serde", + "serde_json", "toml", ] @@ -354,9 +355,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.91" +version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883" +checksum = "cad406b69c91885b5107daf2c29572f6c8cdb3c66826821e286c533490c0bc76" dependencies = [ "itoa", "ryu", diff --git a/Cargo.toml b/Cargo.toml index 7559aee..17f67b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" dialoguer = "0.10.3" indicatif = "0.17.3" toml = "0.6.0" +serde_json = "1.0.93" [dependencies.serde] version = "1.0.152" diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..3c49e99 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,20 @@ +pipeline { + agent none + stages { + stage('Build') { + agent { + docker { image 'rust:alpine' } + } + environment { + RUSTFLAGS = '-C target-feature=+crt-static -C link-self-contained=yes -C link-arg=-s' + TOKEN = credentials('abd7020c-43d6-485b-ae09-2f9b484d9c15') + } + steps { + sh 'apk add pkgconf musl-dev curl' + sh 'cargo build --release --target x86_64-unknown-linux-musl' + sh 'cp target/x86_64-unknown-linux-musl/release/dotfiles_installer ./installer-amd64' + sh 'curl -H "Authorization: token ${TOKEN_PSW}" --upload-file ./installer-amd64 https://gitea.mattv.de/api/packages/root/generic/installer/${GIT_COMMIT}/installer-amd64' + } + } + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f9421b9..7592212 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ impl ToString for MainMenu { } fn main() { - if let Some(version) = option_env!("CI_COMMIT_SHA") { + if let Some(version) = option_env!("GIT_COMMIT") { println!("Starting installer version {}", version); update::check_for_updates(version); } else { diff --git a/src/repository.rs b/src/repository.rs index 4ccfaaf..a88c68f 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -48,7 +48,7 @@ fn check_config() -> bool { fn clone_repo() -> bool { println!("Cloning repo..."); if !Path::new("repo").exists() { std::fs::create_dir("repo").unwrap(); } - git!("clone", "git@ssh.gitlab.mattv.de:root/dotfiles.git", ".").success() + git!("clone", "gitea@gitea.mattv.de:root/dotfiles.git", ".").success() && check_config() } diff --git a/src/update.rs b/src/update.rs index a20e201..73417b5 100644 --- a/src/update.rs +++ b/src/update.rs @@ -3,30 +3,25 @@ use std::fs; use std::fs::File; use std::os::unix::process::CommandExt; use std::path::Path; -use serde::Deserialize; -#[derive(Debug, Deserialize)] -struct Pipeline { - sha: String -} - -#[derive(Debug, Deserialize)] -struct PackageEntry { - version: String, - pipeline: Pipeline -} +const GITEA_KEY: &str = "786666bd8bce93c562c4fc4c83933faa6cbdc802"; pub fn check_for_updates(version: &str) { print!("Checking for updates... "); - let resp: Vec = attohttpc::get("https://gitlab.mattv.de/api/v4/projects/43/packages?name=installer&sort=desc").send().unwrap().json().unwrap(); - let newest = resp.first().unwrap(); - if newest.pipeline.sha != version { - println!("New version exists"); + let resp: serde_json::Value = attohttpc::get("https://gitea.mattv.de/api/v1/repos/root/dotfiles/branches/installer") + .header("accept", "application/json") + .header("Authorization", format!("token {GITEA_KEY}")) + .send().unwrap().json().unwrap(); + let newest = resp["commit"]["id"].as_str().unwrap(); + if newest != version { + println!("New version {newest}"); let exe = current_exe().unwrap(); let temp = Path::new("temp"); print!("Downloading... "); - attohttpc::get(format!("https://gitlab.mattv.de/api/v4/projects/43/packages/generic/installer/{}/installer-amd64", newest.version)).send().unwrap().write_to(File::create(temp).unwrap()).unwrap(); + attohttpc::get(format!("https://gitea.mattv.de/api/packages/root/generic/installer/{newest}/installer-amd64")) + .header("Authorization", format!("token {GITEA_KEY}")) + .send().unwrap().write_to(File::create(temp).unwrap()).unwrap(); println!("Done"); fs::set_permissions(&temp, exe.metadata().unwrap().permissions()).unwrap();