|
| 1 | +import { |
| 2 | + dag, |
| 3 | + Container, |
| 4 | + Directory, |
| 5 | + object, |
| 6 | + func, |
| 7 | + argument, |
| 8 | +} from "@dagger.io/dagger"; |
| 9 | + |
| 10 | +@object() |
| 11 | +export class AtomicServer { |
| 12 | + /** |
| 13 | + * Publish the application container after building and testing it on-the-fly |
| 14 | + */ |
| 15 | + @func() |
| 16 | + async publish( |
| 17 | + @argument({ defaultPath: "/" }) source: Directory |
| 18 | + ): Promise<string> { |
| 19 | + await this.test(source); |
| 20 | + return await this.build(source).publish( |
| 21 | + "ttl.sh/hello-dagger-" + Math.floor(Math.random() * 10000000) |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Build the application container |
| 27 | + */ |
| 28 | + @func() |
| 29 | + build(@argument({ defaultPath: "/" }) source: Directory): Container { |
| 30 | + const build = this.buildEnv(source) |
| 31 | + .withExec(["npm", "run", "build"]) |
| 32 | + .directory("./dist"); |
| 33 | + return dag |
| 34 | + .container() |
| 35 | + .from("nginx:1.25-alpine") |
| 36 | + .withDirectory("/usr/share/nginx/html", build) |
| 37 | + .withExposedPort(80); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Return the result of running unit tests |
| 42 | + */ |
| 43 | + @func() |
| 44 | + async test( |
| 45 | + @argument({ defaultPath: "/" }) source: Directory |
| 46 | + ): Promise<string> { |
| 47 | + return this.buildEnv(source) |
| 48 | + .withExec(["npm", "run", "test:unit", "run"]) |
| 49 | + .stdout(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Build a ready-to-use development environment |
| 54 | + */ |
| 55 | + @func() |
| 56 | + buildEnv(@argument({ defaultPath: "/" }) source: Directory): Container { |
| 57 | + const nodeCache = dag.cacheVolume("node"); |
| 58 | + return dag |
| 59 | + .container() |
| 60 | + .from("node:21-slim") |
| 61 | + .withDirectory("/src", source) |
| 62 | + .withMountedCache("/root/.npm", nodeCache) |
| 63 | + .withWorkdir("/src") |
| 64 | + .withExec(["npm", "install"]); |
| 65 | + } |
| 66 | +} |
0 commit comments