Skip to content

Getting started ​

In this tutorial we will build a small deploy pipeline: lint the code, run the tests and build the site, deploy it, and clean up afterwards. We will start with one job, connect jobs through what they need, watch a failure travel through the graph, add a cleanup that runs whatever happens, and finish by reading the typed report. Each step is a complete program you can run.

Install ​

Add the package, @sekhmet/enum, whose match we will use to read the report, and @std/assert, which each step uses to check its output:

sh
deno add jsr:@sekhmet/job jsr:@sekhmet/enum jsr:@std/assert

Run one job ​

First, we define a job that lints the code. Our jobs run commands, so we hand every run a shell with a sh function. Here it only records each command, which keeps the tutorial runnable anywhere:

ts
import { defineJobs, runJobs, Succeeded } from "@sekhmet/job";
import { assertEquals } from "@std/assert";

interface Shell {
	readonly sh: (command: string) => Promise<void>;
}

const { job } = defineJobs((shell: Shell) => shell);

const lint = job({ name: "lint", run: (r) => r.ctx.sh("deno lint") });

const ran: string[] = [];
const shell: Shell = {
	sh: async (command) => {
		ran.push(command);
	},
};
const report = (await runJobs({ jobs: { lint }, meta: shell })).unwrap();

assertEquals(
	report.outcomes.lint,
	Succeeded(undefined),
	"lint should succeed, returning nothing",
);

assertEquals(ran, ["deno lint"], "lint should run its command");

defineJobs takes a context factory. The type we annotate its parameter with, Shell, is the run's metadata, which we hand runJobs as meta. What the factory returns is each job's context, r.ctx; ours passes the shell straight through, so r.ctx.sh is typed in every job we define with this job. runJobs takes the jobs to run as a record, and its report gives each one's outcome under the same key. The lint job returned nothing, so it Succeeded with undefined.

runJobs returns a Result, because a set of jobs might not form a valid graph. Ours does, so we call its unwrap() method.

Connect jobs through what they need ​

Next, we add the tests, the build and the deploy. A job declares what it needs in its plan, and its run receives what those jobs returned:

ts
import { defineJobs, runJobs, Succeeded } from "@sekhmet/job";
import { assertEquals } from "@std/assert";

interface Shell {
	readonly sh: (command: string) => Promise<void>;
}

const { job } = defineJobs((shell: Shell) => shell);

const lint = job({ name: "lint", run: (r) => r.ctx.sh("deno lint") });

const test = job({
	name: "test",
	plan: (p) => ({ lint: p.needs(lint) }),
	run: (r) => r.ctx.sh("deno test"),
});

const build = job({
	name: "build",
	plan: (p) => ({ lint: p.needs(lint) }),
	run: async (r) => {
		await r.ctx.sh("deno task build");
		return "dist";
	},
});

const deploy = job({
	name: "deploy",
	plan: (p) => ({ tested: p.needs(test), site: p.needs(build) }),
	run: (r, { site }) => r.ctx.sh(`deploy ${site}`),
});

const ran: string[] = [];
const shell: Shell = {
	sh: async (command) => {
		ran.push(command);
	},
};
const report = (await runJobs({ jobs: { deploy }, meta: shell })).unwrap();

assertEquals(
	report.outcomes.deploy,
	Succeeded(undefined),
	"deploy should succeed once its needs did",
);

assertEquals(
	ran.length,
	4,
	"lint should run once, though test and build both need it",
);

assertEquals(ran[0], "deno lint", "lint should run first");

assertEquals(
	ran[3],
	"deploy dist",
	"deploy should run last, with the site build returned",
);

We only asked for deploy, but everything it needs ran first. Lint ran once, although both test and build need it: a job runs once per run, and every job that needs it shares its result. test and build don't need each other, so they ran side by side once lint passed.

Inside deploy, site is the string "dist" that build returned, typed from the plan with no annotation.

Watch a failure ​

Now let's make the tests fail. Our shell throws for deno test, as a real one would on a non-zero exit:

ts
import { Blocked, defineJobs, runJobs, Succeeded } from "@sekhmet/job";
import { assert, assertEquals } from "@std/assert";

interface Shell {
	readonly sh: (command: string) => Promise<void>;
}

const { job } = defineJobs((shell: Shell) => shell);

const lint = job({ name: "lint", run: (r) => r.ctx.sh("deno lint") });

const test = job({
	name: "test",
	plan: (p) => ({ lint: p.needs(lint) }),
	run: (r) => r.ctx.sh("deno test"),
});

const build = job({
	name: "build",
	plan: (p) => ({ lint: p.needs(lint) }),
	run: async (r) => {
		await r.ctx.sh("deno task build");
		return "dist";
	},
});

const deploy = job({
	name: "deploy",
	plan: (p) => ({ tested: p.needs(test), site: p.needs(build) }),
	run: (r, { site }) => r.ctx.sh(`deploy ${site}`),
});

const ran: string[] = [];
const shell: Shell = {
	sh: async (command) => {
		ran.push(command);
		if (command === "deno test") throw new Error("deno test exited 1");
	},
};
const report = (await runJobs({ jobs: { deploy }, meta: shell })).unwrap();

assert(
	report.all.get("test")?.isFailed(),
	"the failing tests should end Failed",
);

assertEquals(
	report.all.get("build"),
	Succeeded("dist"),
	"build, which doesn't need the tests, should still run",
);

assertEquals(
	report.outcomes.deploy,
	Blocked(["test"]),
	"deploy should be blocked by the failed tests",
);

assertEquals(report.passed, false, "a failure should fail the run");

assert(!ran.includes("deploy dist"), "a blocked deploy should never run");

The throw didn't escape runJobs: it became the test job's Failed outcome. build doesn't need the tests, so it still ran. deploy needs both, so it never started: it is Blocked, naming the need that stopped it.

report.all holds every job the run reached, by name, in the order they settled, while report.outcomes holds only the ones we passed in. report.passed is false, since a job failed.

Clean up whatever happens ​

The build leaves a dist directory behind, and we want it gone even when the deploy doesn't happen. A job's runIf decides whether it runs once its needs have settled. The default is success(), which runs only when every need succeeded. A cleanup wants always():

ts
import { always, defineJobs, runJobs } from "@sekhmet/job";
import { assert, assertEquals } from "@std/assert";

interface Shell {
	readonly sh: (command: string) => Promise<void>;
}

const { job } = defineJobs((shell: Shell) => shell);

const lint = job({ name: "lint", run: (r) => r.ctx.sh("deno lint") });

const test = job({
	name: "test",
	plan: (p) => ({ lint: p.needs(lint) }),
	run: (r) => r.ctx.sh("deno test"),
});

const build = job({
	name: "build",
	plan: (p) => ({ lint: p.needs(lint) }),
	run: async (r) => {
		await r.ctx.sh("deno task build");
		return "dist";
	},
});

const deploy = job({
	name: "deploy",
	plan: (p) => ({ tested: p.needs(test), site: p.needs(build) }),
	run: (r, { site }) => r.ctx.sh(`deploy ${site}`),
});

const cleanup = job({
	name: "cleanup",
	plan: (p) => ({ site: p.needs(build), deploy: p.needs(deploy) }),
	runIf: always(),
	run: async (r, { site, deploy }) => {
		await r.ctx.sh(`rm -rf ${site.value().unwrapOr("dist")}`);
		return deploy.isSucceeded();
	},
});

const ran: string[] = [];
const shell: Shell = {
	sh: async (command) => {
		ran.push(command);
		if (command === "deno test") throw new Error("deno test exited 1");
	},
};
const report = (
	await runJobs({ jobs: { deploy, cleanup }, meta: shell })
).unwrap();

assertEquals(
	report.outcomes.cleanup.value().unwrap(),
	false,
	"the cleanup should run and see that deploy didn't succeed",
);

assert(
	ran.includes("rm -rf dist"),
	"the cleanup should remove the build's directory",
);

Because the cleanup may run when its needs didn't succeed, its run gets each need's outcome rather than its value. site is the build's outcome, and site.value() is the directory if the build succeeded. deploy is the deploy's outcome, Blocked here, so the cleanup returns false.

We now pass two roots, deploy and cleanup. The cleanup needs the deploy anyway, but naming both puts both in report.outcomes.

Read the report ​

Last, we turn the report into something to print. Each root's outcome is typed by its job, and enum's match makes us handle every way a job can end:

ts
import { match } from "@sekhmet/enum/core/mini";
import { always, defineJobs, type JobEnd, runJobs } from "@sekhmet/job";
import { assertEquals } from "@std/assert";

interface Shell {
	readonly sh: (command: string) => Promise<void>;
}

const { job } = defineJobs((shell: Shell) => shell);

const lint = job({ name: "lint", run: (r) => r.ctx.sh("deno lint") });

const test = job({
	name: "test",
	plan: (p) => ({ lint: p.needs(lint) }),
	run: (r) => r.ctx.sh("deno test"),
});

const build = job({
	name: "build",
	plan: (p) => ({ lint: p.needs(lint) }),
	run: async (r) => {
		await r.ctx.sh("deno task build");
		return "dist";
	},
});

const deploy = job({
	name: "deploy",
	plan: (p) => ({ tested: p.needs(test), site: p.needs(build) }),
	run: (r, { site }) => r.ctx.sh(`deploy ${site}`),
});

const cleanup = job({
	name: "cleanup",
	plan: (p) => ({ site: p.needs(build), deploy: p.needs(deploy) }),
	runIf: always(),
	run: async (r, { site, deploy }) => {
		await r.ctx.sh(`rm -rf ${site.value().unwrapOr("dist")}`);
		return deploy.isSucceeded();
	},
});

const shell: Shell = {
	sh: async (command) => {
		if (command === "deno test") throw new Error("deno test exited 1");
	},
};
const report = (
	await runJobs({ jobs: { deploy, cleanup }, meta: shell })
).unwrap();

const deployed: JobEnd<void> = report.outcomes.deploy;
const cleanedUp: JobEnd<boolean> = report.outcomes.cleanup;

const summary = match(deployed)({
	Succeeded: () => "deployed",
	Failed: (error) => `deploy failed: ${String(error)}`,
	Skipped: () => "deploy skipped",
	Blocked: (blockedBy) => `deploy blocked by ${blockedBy.join(", ")}`,
	Cancelled: () => "deploy cancelled",
});

assertEquals(
	summary,
	"deploy blocked by test",
	"the summary should say what blocked the deploy",
);

assertEquals(cleanedUp.isSucceeded(), true, "the cleanup should still succeed");

assertEquals(
	report.all.size,
	5,
	"the report should hold all five jobs the run reached",
);

report.outcomes.deploy is a JobEnd<void> and report.outcomes.cleanup a JobEnd<boolean>, as Promise.all types each promise it's given. A job ends in one of five ways: Succeeded, Failed, Skipped, Blocked or Cancelled. match takes one arm per way, so the compiler tells us if we forget one.

What we built ​

You have built a deploy pipeline as a graph of jobs. Each job declared what it needs, ran once, and handed its result to the jobs after it. A failure blocked only what depended on it, a cleanup ran anyway, and the run ended with a typed report instead of a thrown error.

From here:

  • Recipes show how to make a need conditional, notify on failure, generate jobs from data, preview a run, cancel one, and more.
  • Concepts explain why a plan is separate from its run, how outcomes combine, and how job relates to GitHub Actions.
  • The API reference on JSR documents every export.