CI/CD Setup

This commit is contained in:
Joe Bellus 2022-02-11 21:23:31 +00:00
parent 2156529b1c
commit 204369ea47
8 changed files with 277 additions and 168 deletions

3
.cargo/config.toml Normal file
View File

@ -0,0 +1,3 @@
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"

153
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,153 @@
stages:
- test
- ui
- build
- deploy
- release
test:
image: "rust:slim-buster"
stage: test
script:
- mkdir dist
- touch dist/index.html
- apt-get update
- apt-get -y install pkg-config
- cargo test
build-ui:
stage: ui
image: node:16.13-slim
script:
- apt-get update
- apt-get -y install libssl-dev
- npm install
- npm run build
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
artifacts:
paths:
- dist/
build-x64-bin:
dependencies:
- build-ui
image: "rust:slim-buster"
stage: build
tags:
- linux
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- apt-get update
- apt-get -y install libssl-dev pkg-config
- cargo build --release
- cd target/release
- tar -czf ../../vade-linux-x64.tar.gz vade
- cd ../../
artifacts:
paths:
- target/release/vade
- vade-linux-x64.tar.gz
build-musl-bin:
dependencies:
- build-ui
stage: build
image: 'rust:latest'
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- rustup target add x86_64-unknown-linux-musl
- apt update && apt install -y musl-tools musl-dev
- update-ca-certificates
- cargo build --target x86_64-unknown-linux-musl --release
- cd target/x86_64-unknown-linux-musl/release/
- tar -czf ../../../vade-linux-musl.tar.gz vade
- cd ../../../
artifacts:
paths:
- vade-linux-musl.tar.gz
- target/x86_64-unknown-linux-musl/release/vade
build-arm-bin:
dependencies:
- build-ui
stage: build
image: 'rust:latest'
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- rustup target add armv7-unknown-linux-gnueabihf
- apt update
- apt-get -y install gcc-arm-linux-gnueabihf
- cargo build --target armv7-unknown-linux-gnueabihf --release
- cd target/armv7-unknown-linux-gnueabihf/release/
- tar -czf ../../../vade-linux-arm.tar.gz vade
- cd ../../../
artifacts:
paths:
- target/armv7-unknown-linux-gnueabihf/release/vade
- vade-linux-arm.tar.gz
build-win-bin:
dependencies:
- build-ui
stage: build
tags:
- windows
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- cargo build --release
artifacts:
paths:
- target/release/vade.exe
deploy-dev-docker:
dependencies:
- build-musl-bin
stage: deploy
image: docker:latest
script:
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
- docker build -t $CI_REGISTRY/vade/vade-mecum .
- docker push $CI_REGISTRY/vade/vade-mecum
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
deploy-binaries:
dependencies:
- build-x64-bin
- build-musl-bin
- build-arm-bin
- build-win-bin
image: curlimages/curl:latest
stage: deploy
rules:
- if: $CI_COMMIT_TAG
script:
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file ./vade-linux-x64.tar.gz "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/bin/$CI_COMMIT_TAG/vade-linux-x64-$CI_COMMIT_TAG.tar.gz"'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file ./vade-linux-arm.tar.gz "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/bin/$CI_COMMIT_TAG/vade-linux-arm-$CI_COMMIT_TAG.tar.gz"'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file ./vade-linux-musl.tar.gz "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/bin/$CI_COMMIT_TAG/vade-linux-musl-$CI_COMMIT_TAG.tar.gz"'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file ./target/release/vade.exe "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/bin/$CI_COMMIT_TAG/vade.exe"'
release:
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
rules:
- if: $CI_COMMIT_TAG
script:
- echo "running release_job"
release:
name: 'Release v$CI_COMMIT_TAG'
description: 'Version $CI_COMMIT_TAG'
tag_name: '$CI_COMMIT_TAG'
ref: '$CI_COMMIT_TAG'
assets:
links:
- name: 'vade-linux-x64-$CI_COMMIT_TAG.tar.gz'
url: '${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/linux-bin/$CI_COMMIT_TAG/vade-linux-x64-$CI_COMMIT_TAG.tar.gz'

233
Cargo.lock generated
View File

@ -852,22 +852,6 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2df960f5d869b2dd8532793fde43eb5427cceb126c929747a26823ab0eeb536" checksum = "a2df960f5d869b2dd8532793fde43eb5427cceb126c929747a26823ab0eeb536"
[[package]]
name = "core-foundation"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6888e10551bb93e424d8df1d07f1a8b4fceb0001a3a4b048bfc47554946f47b3"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
[[package]] [[package]]
name = "cpufeatures" name = "cpufeatures"
version = "0.2.1" version = "0.2.1"
@ -1012,15 +996,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "fastrand"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3fcf0cee53519c866c09b5de1f6c56ff9d647101f81c1964fa632e148896cdf"
dependencies = [
"instant",
]
[[package]] [[package]]
name = "firestorm" name = "firestorm"
version = "0.5.0" version = "0.5.0"
@ -1057,21 +1032,6 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "foreign-types"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
dependencies = [
"foreign-types-shared",
]
[[package]]
name = "foreign-types-shared"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]] [[package]]
name = "form_urlencoded" name = "form_urlencoded"
version = "1.0.1" version = "1.0.1"
@ -1082,6 +1042,12 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "fs_extra"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"
[[package]] [[package]]
name = "fuchsia-zircon" name = "fuchsia-zircon"
version = "0.3.3" version = "0.3.3"
@ -1426,6 +1392,27 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
[[package]]
name = "jemalloc-sys"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d3b9f3f5c9b31aa0f5ed3260385ac205db665baa41d49bb8338008ae94ede45"
dependencies = [
"cc",
"fs_extra",
"libc",
]
[[package]]
name = "jemallocator"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43ae63fcfc45e99ab3d1b29a46782ad679e98436c3169d15a167a1108a724b69"
dependencies = [
"jemalloc-sys",
"libc",
]
[[package]] [[package]]
name = "jobserver" name = "jobserver"
version = "0.1.24" version = "0.1.24"
@ -1690,24 +1677,6 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "native-tls"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48ba9f7719b5a0f42f338907614285fb5fd70e53858141f69898a1fb7203b24d"
dependencies = [
"lazy_static",
"libc",
"log",
"openssl",
"openssl-probe",
"openssl-sys",
"schannel",
"security-framework",
"security-framework-sys",
"tempfile",
]
[[package]] [[package]]
name = "net2" name = "net2"
version = "0.2.37" version = "0.2.37"
@ -1811,39 +1780,6 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "openssl"
version = "0.10.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c7ae222234c30df141154f159066c5093ff73b63204dcda7121eb082fc56a95"
dependencies = [
"bitflags",
"cfg-if 1.0.0",
"foreign-types",
"libc",
"once_cell",
"openssl-sys",
]
[[package]]
name = "openssl-probe"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "openssl-sys"
version = "0.9.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e46109c383602735fa0a2e48dd2b7c892b048e1bf69e5c3b1d804b7d9c203cb"
dependencies = [
"autocfg",
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]] [[package]]
name = "ouroboros" name = "ouroboros"
version = "0.14.0" version = "0.14.0"
@ -2163,15 +2099,6 @@ version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
"winapi 0.3.9",
]
[[package]] [[package]]
name = "resolv-conf" name = "resolv-conf"
version = "0.7.0" version = "0.7.0"
@ -2261,6 +2188,19 @@ dependencies = [
"semver 1.0.4", "semver 1.0.4",
] ]
[[package]]
name = "rustls"
version = "0.19.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35edb675feee39aec9c99fa5ff985081995a06d594114ae14cbe797ad7b7a6d7"
dependencies = [
"base64",
"log",
"ring",
"sct",
"webpki",
]
[[package]] [[package]]
name = "rustversion" name = "rustversion"
version = "1.0.6" version = "1.0.6"
@ -2282,22 +2222,22 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "schannel"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75"
dependencies = [
"lazy_static",
"winapi 0.3.9",
]
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.1.0" version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "sct"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b362b83898e0e69f38515b82ee15aa80636befe47c3b6d3d89a911e78fc228ce"
dependencies = [
"ring",
"untrusted",
]
[[package]] [[package]]
name = "sea-orm" name = "sea-orm"
version = "0.5.0" version = "0.5.0"
@ -2384,29 +2324,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "security-framework"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fed7948b6c68acbb6e20c334f55ad635dc0f75506963de4464289fbd3b051ac"
dependencies = [
"bitflags",
"core-foundation",
"core-foundation-sys",
"libc",
"security-framework-sys",
]
[[package]]
name = "security-framework-sys"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a57321bf8bc2362081b2599912d2961fe899c0efadf1b4b2f8d48b3e253bb96c"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]] [[package]]
name = "semver" name = "semver"
version = "0.9.0" version = "0.9.0"
@ -2659,6 +2576,7 @@ dependencies = [
"parking_lot", "parking_lot",
"percent-encoding", "percent-encoding",
"rust_decimal", "rust_decimal",
"rustls",
"serde", "serde",
"serde_json", "serde_json",
"sha2", "sha2",
@ -2670,6 +2588,8 @@ dependencies = [
"tokio-stream", "tokio-stream",
"url", "url",
"uuid", "uuid",
"webpki",
"webpki-roots",
] ]
[[package]] [[package]]
@ -2699,10 +2619,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8061cbaa91ee75041514f67a09398c65a64efed72c90151ecd47593bad53da99" checksum = "8061cbaa91ee75041514f67a09398c65a64efed72c90151ecd47593bad53da99"
dependencies = [ dependencies = [
"actix-rt 2.6.0", "actix-rt 2.6.0",
"native-tls",
"once_cell", "once_cell",
"tokio 1.16.1", "tokio 1.16.1",
"tokio-native-tls", "tokio-rustls",
] ]
[[package]] [[package]]
@ -2790,20 +2709,6 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "tempfile"
version = "3.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4"
dependencies = [
"cfg-if 1.0.0",
"fastrand",
"libc",
"redox_syscall",
"remove_dir_all",
"winapi 0.3.9",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.30" version = "1.0.30"
@ -2963,13 +2868,14 @@ dependencies = [
] ]
[[package]] [[package]]
name = "tokio-native-tls" name = "tokio-rustls"
version = "0.3.0" version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b" checksum = "bc6844de72e57df1980054b38be3a9f4702aba4858be64dd700181a8a6d0e1b6"
dependencies = [ dependencies = [
"native-tls", "rustls",
"tokio 1.16.1", "tokio 1.16.1",
"webpki",
] ]
[[package]] [[package]]
@ -3256,6 +3162,7 @@ dependencies = [
"base64", "base64",
"bcrypt", "bcrypt",
"chrono", "chrono",
"jemallocator",
"jsonwebtoken", "jsonwebtoken",
"mime_guess", "mime_guess",
"rand 0.8.4", "rand 0.8.4",
@ -3263,6 +3170,7 @@ dependencies = [
"sea-orm", "sea-orm",
"serde", "serde",
"serde_json", "serde_json",
"sqlx",
"tracing", "tracing",
"tracing-actix-web", "tracing-actix-web",
"tracing-subscriber", "tracing-subscriber",
@ -3375,6 +3283,25 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "webpki"
version = "0.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8e38c0608262c46d4a56202ebabdeb094cef7e560ca7a226c6bf055188aa4ea"
dependencies = [
"ring",
"untrusted",
]
[[package]]
name = "webpki-roots"
version = "0.21.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aabe153544e473b775453675851ecc86863d2a81d786d741f6b76778f2a48940"
dependencies = [
"webpki",
]
[[package]] [[package]]
name = "widestring" name = "widestring"
version = "0.4.3" version = "0.4.3"

View File

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
sea-orm = { version = "0.5.0", features = [ "sqlx-sqlite", "runtime-actix-native-tls", "macros", "mock"], default-features = false } sea-orm = { version = "0.5.0", features = [ "sqlx-sqlite", "runtime-actix-rustls", "macros", "mock"], default-features = false }
tracing = "0.1.30" tracing = "0.1.30"
tracing-unwrap = "0.9.2" tracing-unwrap = "0.9.2"
tracing-subscriber = { version = "0.3.8", features = ["fmt"] } tracing-subscriber = { version = "0.3.8", features = ["fmt"] }
@ -15,7 +15,7 @@ actix-cors = "0.5.4"
chrono = { version = "0.4.19", features = ["serde"] } chrono = { version = "0.4.19", features = ["serde"] }
serde = { version = "1.0.136", features= [ "derive" ] } serde = { version = "1.0.136", features= [ "derive" ] }
serde_json = "1.0.78" serde_json = "1.0.78"
actix-web = "4.0.0-rc.1" actix-web = "4.0.0-rc.2"
actix-rt = "2.6.0" actix-rt = "2.6.0"
tracing-test = "0.2.1" tracing-test = "0.2.1"
tracing-actix-web = "0.5.0-rc.1" tracing-actix-web = "0.5.0-rc.1"
@ -26,3 +26,7 @@ actix-web-httpauth = "0.6.0-beta.7"
jsonwebtoken = "8.0.1" jsonwebtoken = "8.0.1"
rand = "0.8.4" rand = "0.8.4"
base64 = "0.13.0" base64 = "0.13.0"
sqlx = { version = "^0.5", features=["sqlite", "migrate"] }
[target.'cfg(all(target_env = "musl", target_pointer_width = "64"))'.dependencies.jemallocator]
version = "0.3"

6
Dockerfile Normal file
View File

@ -0,0 +1,6 @@
FROM alpine:latest
COPY target/x86_64-unknown-linux-musl/release/vade /app/vade
EXPOSE 8080
WORKDIR app
RUN touch data.db
CMD ["./vade"]

View File

@ -1,9 +0,0 @@
use std::process::Command;
fn main() {
Command::new("npm")
.args(&["run", "build-dev"])
.current_dir("./vade-ui")
.status()
.unwrap();
}

View File

@ -68,6 +68,23 @@ impl From<sea_orm::DbErr> for Error {
} }
} }
impl From<sqlx::Error> for Error {
fn from(e: sqlx::Error) -> Self {
Self {
code: ErrorCode::DatabaseError,
message: e.to_string(),
}
}
}
impl From<sqlx::migrate::MigrateError> for Error {
fn from(e: sqlx::migrate::MigrateError) -> Self {
Self {
code: ErrorCode::DatabaseError,
message: e.to_string(),
}
}
}
impl std::fmt::Display for Error { impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message) write!(f, "{}", self.message)

View File

@ -9,6 +9,10 @@ mod auth;
mod entity; mod entity;
mod error; mod error;
#[cfg(all(target_env = "musl", target_pointer_width = "64"))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
#[derive(Debug)] #[derive(Debug)]
pub struct AppState { pub struct AppState {
pub db: DatabaseConnection, pub db: DatabaseConnection,
@ -21,7 +25,7 @@ async fn main() {
.pretty() .pretty()
.with_writer(std::io::stdout) .with_writer(std::io::stdout)
.with_ansi(true) .with_ansi(true)
.with_filter(tracing_subscriber::filter::LevelFilter::DEBUG), .with_filter(tracing_subscriber::filter::LevelFilter::TRACE),
); );
tracing::subscriber::set_global_default(subscriber).expect("Unable to set a global collector"); tracing::subscriber::set_global_default(subscriber).expect("Unable to set a global collector");
@ -33,10 +37,11 @@ async fn main() {
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.app_data(state.clone()) .app_data(state.clone())
.wrap(tracing_actix_web::TracingLogger::default())
.service(api::routes()) .service(api::routes())
.service(dist) .service(dist)
}) })
.bind("127.0.0.1:8080") .bind("0.0.0.0:8080")
.unwrap() .unwrap()
.run() .run()
.await .await
@ -65,6 +70,9 @@ async fn dist(path: web::Path<String>) -> HttpResponse {
} }
#[instrument] #[instrument]
async fn setup_database() -> Result<DatabaseConnection, sea_orm::DbErr> { async fn setup_database() -> error::Result<DatabaseConnection> {
Database::connect("sqlite://data.db").await let pool = sqlx::SqlitePool::connect("sqlite://data.db").await?;
sqlx::migrate!("./migrations").run(&pool).await?;
tracing::info!("Database migrated");
Ok(Database::connect("sqlite://data.db").await?)
} }