Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support trigger bazel build after git push #184

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ MEGA_DB_MAX_CONNECTIONS = 32
MEGA_DB_MIN_CONNECTIONS = 16


# If the object file size exceeds a threshold value, it will be stored in the specified location instead of the database.
# If the object file size exceeds the threshold value, it will be stored in the specified location instead of the database.
MEGA_BIG_OBJ_THRESHOLD_SIZE = 1024 # Unit KB
MEGA_BIG_OBJ_STORAGR_PATH = "/tmp/.mega/objects"

Expand Down
11 changes: 11 additions & 0 deletions build-tool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "build-tool"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
git2 = "0.18.1"
tracing = "0.1.39"
url = "2.4.1"
64 changes: 64 additions & 0 deletions build-tool/src/bazel_build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use git2::Repository;
use std::{
env, fs,
io::{BufRead, BufReader},
path::PathBuf,
process::{Command, Stdio},
};
use url::Url;

pub fn build() {
let repo_path = PathBuf::from("/projects/mega");
let project_name = repo_path.file_name().unwrap();

let mut temp = PathBuf::from("/tmp/.mega/bazel_build_projects");
temp.push(project_name);
let mut project_url = Url::parse("http://localhost:8000").unwrap();
project_url.set_path(repo_path.to_str().unwrap());
if temp.exists() {
if let Err(err) = fs::remove_dir_all(&temp) {
tracing::error!("Error: {}", err);
} else {
tracing::info!("repo removed successfully: {:?}", project_name);
}
}
Repository::clone(project_url.as_ref(), &temp).expect("failed to clone");

if let Err(err) = env::set_current_dir(&temp) {
tracing::error!("Failed to change the working directory: {}", err);
} else {
tracing::info!("Changed working directory to: {:?}", temp);

// Execute cargo generate-lockfile command
Command::new("cargo")
.arg("generate-lockfile")
.output()
.unwrap();
tracing::info!("project {:?} generate lockfile successfully", project_name);

// Execute bazel sync crates command
let mut sync_child = Command::new("bazel")
.env("CARGO_BAZEL_REPIN", "1")
.args(["sync", "--only=crate_index"])
.stdout(Stdio::piped())
.spawn()
.expect("Failed to start bazel sync");

let sync_stdout = sync_child.stdout.take().unwrap();
for line in BufReader::new(sync_stdout).lines().flatten() {
tracing::info!("project {:?} bazel sync: {}", project_name, line);
}

// Execute bazel build
let mut bazel_build_child = Command::new("bazel")
.args(["build", "//:mega"])
.stdout(Stdio::piped())
.spawn()
.expect("Failed to start the bazel build");

let build_stdout = bazel_build_child.stdout.take().unwrap();
for line in BufReader::new(build_stdout).lines().flatten() {
tracing::info!("project {:?} bazel build: {}", project_name, line);
}
}
}
1 change: 1 addition & 0 deletions build-tool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod bazel_build;
3 changes: 2 additions & 1 deletion git/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ diff_mydrs = []
database = { path = "../database" }
common = { path = "../common" }
entity = { path = "../database/entity" }
kvcache ={ path = "../kvcache"}
kvcache = { path = "../kvcache" }
build-tool = { path = "../build-tool" }
anyhow = "1.0.75"
bstr = "1.5.0"
chrono = "0.4.24"
Expand Down
11 changes: 7 additions & 4 deletions git/src/protocol/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::protocol::ZERO_ID;
use crate::structure::conversion;
use anyhow::Result;
use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::collections::HashSet;
use std::{collections::HashSet, thread};

use super::{Capability, PackProtocol, Protocol, RefCommand, ServiceType, SideBind};

Expand All @@ -25,7 +25,7 @@ const RECEIVE_CAP_LIST: &str = "report-status report-status-v2 delete-refs quiet

// The ofs-delta and side-band-64k capabilities are sent and recognized by both upload-pack and receive-pack protocols.
// The agent and session-id capabilities may optionally be sent in both protocols.
const CAP_LIST: &str = "side-band-64k ofs-delta object-format=sha1";
const CAP_LIST: &str = "side-band-64k ofs-delta";

// All other capabilities are only recognized by the upload-pack (fetch from server) process.
const UPLOAD_CAP_LIST: &str =
Expand Down Expand Up @@ -82,7 +82,7 @@ impl PackProtocol {
ref_list.push(pkt_line);
}
let pkt_line_stream = self.build_smart_reply(&ref_list, service_type.to_string());
tracing::info!("git_info_refs response: {:?}", pkt_line_stream);
tracing::debug!("git_info_refs response: {:?}", pkt_line_stream);
pkt_line_stream
}

Expand All @@ -95,7 +95,7 @@ impl PackProtocol {

let mut read_first_line = false;
loop {
tracing::info!("loop start");
tracing::debug!("loop start");
let (bytes_take, pkt_line) = read_pkt_line(upload_request);
// read 0000 to continue and read empty str to break
if bytes_take == 0 {
Expand Down Expand Up @@ -196,7 +196,10 @@ impl PackProtocol {
conversion::save_node_from_mr(self.storage.clone(), mr_id, path).await;
if parse_obj_result.is_ok() {
command.save_to_db(self.storage.clone(), path).await;
// save project directory
self.handle_directory().await.unwrap();
// start building
thread::spawn(build_tool::bazel_build::build);
} else {
tracing::error!("{}", parse_obj_result.err().unwrap());
command.failed(String::from("db operation failed"));
Expand Down