diff options
author | Elizabeth Alexander Hunt <me@liz.coffee> | 2025-05-10 16:57:03 -0700 |
---|---|---|
committer | Elizabeth Alexander Hunt <me@liz.coffee> | 2025-05-10 16:57:03 -0700 |
commit | fa8f3f9465e87d499f7d6428323f496a884b7818 (patch) | |
tree | ab477dfa52ef30282029c4f136bf605cb24d67a9 /model | |
download | ci-fa8f3f9465e87d499f7d6428323f496a884b7818.tar.gz ci-fa8f3f9465e87d499f7d6428323f496a884b7818.zip |
initial commit
Diffstat (limited to 'model')
-rw-r--r-- | model/deno.json | 5 | ||||
-rw-r--r-- | model/job.ts | 41 | ||||
-rw-r--r-- | model/mod.ts | 2 | ||||
-rw-r--r-- | model/pipeline.ts | 76 |
4 files changed, 124 insertions, 0 deletions
diff --git a/model/deno.json b/model/deno.json new file mode 100644 index 0000000..d22242e --- /dev/null +++ b/model/deno.json @@ -0,0 +1,5 @@ +{ + "name": "@liz-ci/model", + "version": "0.1.0", + "exports": "./mod.ts" +} diff --git a/model/job.ts b/model/job.ts new file mode 100644 index 0000000..96e0959 --- /dev/null +++ b/model/job.ts @@ -0,0 +1,41 @@ +export type JobArgT = Record<string, string>; +export interface Job { + readonly type: string; + readonly arguments: JobArgT; +} + +export interface FetchCodeJobProps extends JobArgT { + readonly remoteUrl: string; + readonly checkout: string; + readonly path: string; +} + +export interface FetchCodeJob { + readonly type: "fetch_code"; + readonly arguments: FetchCodeJobProps; +} + +export interface BuildDockerImageJobProps extends JobArgT { + readonly registry: string; + readonly namespace: string; + readonly repository: string; + readonly imageTag: string; + + readonly context: string; + readonly dockerfile: string; + readonly buildTarget: string; +} + +export interface BuildDockerImageJob extends Job { + readonly type: "build_docker_image"; + readonly arguments: BuildDockerImageJobProps; +} + +export interface AnsiblePlaybookJobProps extends JobArgT { + readonly playbooks: string; +} + +export interface AnsiblePlaybookJob extends Job { + readonly type: "ansible_playbook"; + readonly arguments: AnsiblePlaybookJobProps; +} diff --git a/model/mod.ts b/model/mod.ts new file mode 100644 index 0000000..944ab7d --- /dev/null +++ b/model/mod.ts @@ -0,0 +1,2 @@ +export * from "./job.ts"; +export * from "./pipeline.ts"; diff --git a/model/pipeline.ts b/model/pipeline.ts new file mode 100644 index 0000000..06361a4 --- /dev/null +++ b/model/pipeline.ts @@ -0,0 +1,76 @@ +import type { FetchCodeJob, Job } from "./mod.ts"; + +export interface Pipeline { + getStages(): ReadonlyArray<PipelineStage>; + serialize(): string; +} + +export interface PipelineStage { + readonly parallelJobs: Array<Job>; +} + +export interface PipelineBuilder { + addStage(stage: PipelineStage): PipelineBuilder; + build(): Pipeline; +} + +export class PipelineImpl implements Pipeline { + constructor(private readonly serialJobs: ReadonlyArray<PipelineStage>) {} + + public getStages() { + return this.serialJobs; + } + + public serialize() { + return JSON.stringify(this.serialJobs); + } + + public static from(s: string): Pipeline { + return new PipelineImpl(JSON.parse(s)); + } +} + +abstract class BasePipelineBuilder implements PipelineBuilder { + protected readonly stages: Array<PipelineStage> = []; + + public addStage(stage: PipelineStage): PipelineBuilder { + this.stages.push(stage); + return this; + } + + public build() { + return new PipelineImpl(this.stages); + } +} + +export class DefaultGitHookPipelineBuilder extends BasePipelineBuilder { + constructor( + private readonly remoteUrl = Deno.env.get("remote")!, + rev = Deno.env.get("rev")!, + private readonly ref = Deno.env.get("ref")!, + ) { + super(); + + this.addStage({ + parallelJobs: [ + <FetchCodeJob> { + type: "fetch_code", + arguments: { + remoteUrl, + checkout: rev, + path: this.getSourceDestination(), + }, + }, + ], + }); + } + + public getSourceDestination() { + return this.remoteUrl.split("/").at(-1) ?? "src"; + } + + public getBranch(): string | undefined { + const branchRefPrefix = "refs/heads/"; + return this.ref.split(branchRefPrefix).at(1); + } +} |