Add bridge code to the project
This commit is contained in:
20
.gitea/workflows/deploy.yml
Normal file
20
.gitea/workflows/deploy.yml
Normal file
@@ -0,0 +1,20 @@
|
||||
name: Deploy Runner-Bridge Application
|
||||
run-name: ${{gitea.actor}} is deploying Runner-Bridge Application 🚀
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install Railway CLI
|
||||
uses: npm i -g @railway/cli
|
||||
|
||||
- name: Deploy to Railway
|
||||
uses: railway up --ci --service runner-bridge
|
||||
env:
|
||||
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}
|
||||
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
1967
Cargo.lock
generated
Normal file
1967
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "Runner-Bridge"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
axum = "0.8.8"
|
||||
reqwest = { version = "0.13.2", features = ["json"] }
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.149"
|
||||
tokio = { version = "1.50.0", features = ["full"] }
|
||||
18
Dockerfile
Normal file
18
Dockerfile
Normal file
@@ -0,0 +1,18 @@
|
||||
# Example multi-stage Dockerfile
|
||||
FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef
|
||||
WORKDIR /app
|
||||
|
||||
FROM chef AS planner
|
||||
COPY . .
|
||||
RUN cargo chef prepare --recipe-path recipe.json
|
||||
|
||||
FROM chef AS builder
|
||||
COPY --from=planner /app/recipe.json recipe.json
|
||||
RUN cargo chef cook --release --recipe-path recipe.json
|
||||
COPY . .
|
||||
RUN cargo build --release --bin runner-bridge
|
||||
|
||||
FROM debian:bookworm-slim
|
||||
WORKDIR /app
|
||||
COPY --from=builder /app/target/release/runner-bridge .
|
||||
CMD ["./runner-bridge"]
|
||||
71
src/main.rs
Normal file
71
src/main.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use axum::{Router, response::IntoResponse, routing::get};
|
||||
use reqwest::StatusCode;
|
||||
use serde_json::json;
|
||||
use std::env;
|
||||
|
||||
// Define an asynchronous main function with the tokio runtime macro
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Build our application with one route: a GET request to "/"
|
||||
let app = Router::new().route("/", get(trigger_runner));
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
// Handler that returns a simple string
|
||||
async fn trigger_runner() -> impl IntoResponse {
|
||||
let token = match env::var("RAILWAY_TOKEN") {
|
||||
Ok(t) => t,
|
||||
Err(_) => {
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"Missing Railway project token",
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let service_id = match env::var("RUNNER_SERVICE_ID") {
|
||||
Ok(t) => t,
|
||||
Err(_) => {
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"Missing Runner Service ID",
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let env_id = match env::var("RAILWAY_ENVIRONMENT_ID") {
|
||||
Ok(id) => id,
|
||||
Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, "Missing Railway env ID"),
|
||||
};
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let query = json!({
|
||||
"query": "mutation Wake($sid: String!, $eid: String!) { serviceInstanceRedeploy(serviceId: $sid, environmentId: $env) }",
|
||||
"variables": {
|
||||
"sid": service_id,
|
||||
"eid": env_id
|
||||
}
|
||||
});
|
||||
|
||||
let res = client
|
||||
.post("https://backboard.railway.com/graphql/v2")
|
||||
.header("Project-Access-Token", token)
|
||||
.json(&query)
|
||||
.send()
|
||||
.await;
|
||||
|
||||
match res {
|
||||
Ok(res) if res.status().is_success() => (StatusCode::OK, "Runner is waking up"),
|
||||
Ok(res) => {
|
||||
let err_test = res.text().await.unwrap_or_default();
|
||||
println!("Railway API error: {}", err_test);
|
||||
(StatusCode::BAD_GATEWAY, "Railway API rejected the request")
|
||||
}
|
||||
Err(_) => (
|
||||
StatusCode::SERVICE_UNAVAILABLE,
|
||||
"Could not connect to Railway",
|
||||
),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user