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

feat(agent): git clone a repo from github #52

Merged
merged 4 commits into from
Jun 26, 2024
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fluentci studio
fluentci --help

Usage: fluentci [pipeline] [jobs...]
Version: 0.14.9
Version: 0.15.0

Description:

Expand All @@ -120,7 +120,7 @@ Description:
/ _// / // / -_) _ \/ __/ /___/ /
/_/ /_/\_,_/\__/_//_/\__/\___/___/

FluentCI CLI - An Open Source CI/CD tool written in TypeScript (Deno) based on Dagger
FluentCI CLI - An Open Source CI/CD tool written in TypeScript (Deno) based on Wasm Plugins and Dagger

Options:

Expand Down
2 changes: 1 addition & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function main() {
/ _// / // / -_) _ \\/ __/ /___/ /
/_/ /_/\\_,_/\\__/_//_/\\__/\\___/___/

FluentCI CLI - An Open Source CI/CD tool written in TypeScript (Deno) based on Dagger
FluentCI CLI - An Open Source CI/CD tool written in TypeScript (Deno) based on Wasm Plugins and Dagger

`
)
Expand Down
89 changes: 56 additions & 33 deletions src/cmd/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from "../utils.ts";
import { hostname, release, cpus, arch, totalmem, platform } from "node:os";
import { Action, Agent, Log, Run } from "../types.ts";
import { setupPkgx } from "../utils.ts";

const accessToken = await getAccessToken();
const headers = {
Expand Down Expand Up @@ -78,12 +79,38 @@ async function startAgent() {
try {
logger.info(`Message from server ${event.data}`);
const data = JSON.parse(event.data);
const { action, src, query, runId, actions, run } = JSON.parse(
const { action, src, query, runId, actions, run, repoUrl } = JSON.parse(
data.event
);
const { jobs, pipeline, wasm, workDir } = JSON.parse(query);

if (action === "build") {
if (repoUrl) {
logger.info(
`Cloning ${brightGreen("https://" + repoUrl.split("@")[1])} ...`
);
const id = createId();
await gitClone(repoUrl, id);

await spawnFluentCI(
logger,
`${dir("home")}/.fluentci/builds/${id}/${repoUrl
.split("/")
.pop()}/${workDir || "."}`,
pipeline,
jobs,
runId,
wasm,
actions,
run
);

await Deno.remove(`${dir("home")}/.fluentci/builds/${id}`, {
recursive: true,
});

return;
}
const project_id = src.split("/")[0];
const sha256 = src.split("/")[1].replace(".zip", "");

Expand All @@ -95,15 +122,15 @@ async function startAgent() {
);
await spawnFluentCI(
logger,
project_id,
sha256,
`${dir(
"home"
)}/.fluentci/builds/${project_id}/${sha256}/${workDir}`,
pipeline,
jobs,
runId,
wasm,
actions,
run,
workDir
run
);
return;
}
Expand All @@ -118,15 +145,13 @@ async function startAgent() {
await extractZipBlob(blob, project_id, sha256);
await spawnFluentCI(
logger,
project_id,
sha256,
`${dir("home")}/.fluentci/builds/${project_id}/${sha256}/${workDir}`,
pipeline,
jobs,
runId,
wasm,
actions,
run,
workDir
run
);
}
} catch (e) {
Expand Down Expand Up @@ -166,28 +191,32 @@ async function extractZipBlob(blob: Blob, project_id: string, sha256: string) {
}
}

async function gitClone(url: string, id: string, branch?: string) {
await Deno.mkdir(`${dir("home")}/.fluentci/builds/${id}`, {
recursive: true,
});
await setupPkgx();
const git = new Deno.Command("pkgx", {
args: ["git", "clone", url],
cwd: `${dir("home")}/.fluentci/builds/${id}`,
stdout: "inherit",
stderr: "inherit",
});
await git.spawn().status;
}

async function spawnFluentCI(
logger: Logger,
project_id: string,
sha256: string,
cwd: string,
pipeline: string,
jobs: [string, ...Array<string>],
clientId: string,
wasm: boolean = false,
actions: Action[] = [],
run?: Run,
workDir = "."
run?: Run
) {
if (actions.length > 0) {
await executeActions(
actions,
project_id,
sha256,
run!,
logger,
clientId,
workDir
);
await executeActions(actions, cwd, run!, logger, clientId);
return;
}

Expand All @@ -199,9 +228,7 @@ async function spawnFluentCI(
pipeline,
...jobs.filter((x) => x !== "--remote-exec"),
],
cwd: `${dir(
"home"
)}/.fluentci/builds/${project_id}/${sha256}/${workDir}`,
cwd,
stdout: "piped",
stderr: "piped",
})
Expand All @@ -215,9 +242,7 @@ async function spawnFluentCI(
pipeline,
...jobs.filter((x) => x !== "--remote-exec"),
],
cwd: `${dir(
"home"
)}/.fluentci/builds/${project_id}/${sha256}/${workDir}`,
cwd,
stdout: "piped",
stderr: "piped",
});
Expand Down Expand Up @@ -270,12 +295,10 @@ async function spawnFluentCI(

async function executeActions(
actions: Action[],
project_id: string,
sha256: string,
cwd: string,
run: Run,
logger: Logger,
clientId: string,
workDir = "."
clientId: string
) {
let currentActionIndex = 0;
const runStart = dayjs();
Expand Down Expand Up @@ -317,7 +340,7 @@ async function executeActions(
`fluentci run ${action.use_wasm ? "--wasm" : ""} ${
action.plugin
} ${cmd}`,
`${dir("home")}/.fluentci/builds/${project_id}/${sha256}/${workDir}`,
cwd,
jobs[currentActionIndex].job_id,
logger,
clientId
Expand Down
4 changes: 2 additions & 2 deletions src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dir } from "../deps.ts";
export const VERSION = "0.14.9";
import { dir } from "../dep.ts";
export const VERSION = "0.15.0";

export const BASE_URL = "https://api.fluentci.io/v1";

Expand Down
30 changes: 28 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,34 @@ export async function setupFluentCIengine() {

export async function setupFluentCIStudio() {
await setupPkgx();
let FLUENTCI_STUDIO_VERSION =
Deno.env.get("FLUENTCI_STUDIO_VERSION") || "v0.1.1";

let FLUENTCI_STUDIO_VERSION = Deno.env.get("FLUENTCI_STUDIO_VERSION");

const headers: Record<string, string> = {};

if (Deno.env.has("GITHUB_ACCESS_TOKEN")) {
headers["Authorization"] = `token ${Deno.env.get("GITHUB_ACCESS_TOKEN")}`;
}

if (!FLUENTCI_STUDIO_VERSION) {
FLUENTCI_STUDIO_VERSION = await fetch(
"https://api.github.com/repos/fluentci-io/fluentci-studio/releases/latest",
{
headers,
}
)
.then((res) => res.json())
.then((data) => data.tag_name)
.catch(() => {
console.error("Failed to fetch latest release.");
Deno.exit(1);
});
}

if (!FLUENTCI_STUDIO_VERSION) {
console.error("Failed to fetch latest release.");
Deno.exit(1);
}

if (!FLUENTCI_STUDIO_VERSION.startsWith("v")) {
FLUENTCI_STUDIO_VERSION = `v${FLUENTCI_STUDIO_VERSION}`;
Expand Down