@tahminator/pipeline 1.0.6 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,42 @@
1
+ export declare class DockerClient {
2
+ private readonly username;
3
+ private constructor();
4
+ /**
5
+ * @note This class must be cleaned up manually by calling `cleanup()`.
6
+ * You can also do this manually by utilizing the [`usage`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management) keyword.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * export async function main() {
11
+ * await using client = await DockerClient.create(username, pat);
12
+ *
13
+ * client.promoteDockerImage({});
14
+ *
15
+ * // client will automatically be cleaned up at the end of the scope.
16
+ * }
17
+ * ```
18
+ */
19
+ static create(username: string, pat: string): Promise<DockerClient>;
20
+ /**
21
+ * Will lookup the Docker Hub image of `originalTag` & add `newGithubTags` to it.
22
+ */
23
+ promoteDockerImage({ originalTag, newGithubTags, repository, }: {
24
+ originalTag: string;
25
+ newGithubTags: string[];
26
+ repository: string;
27
+ }): Promise<void>;
28
+ buildImage(args: {
29
+ dockerFileLocation: string;
30
+ /**
31
+ * @example ["staging-latest", `staging-${sha}`, `staging-%{timestamp.toString()}`]
32
+ */
33
+ tags: string[];
34
+ dockerUsername: string;
35
+ dockerRepository: string;
36
+ shouldUpload?: boolean;
37
+ platforms?: string[];
38
+ buildArgs?: Record<string, string | number | boolean>;
39
+ }): Promise<void>;
40
+ [Symbol.asyncDispose](): Promise<void>;
41
+ cleanup(): Promise<void>;
42
+ }
@@ -0,0 +1,83 @@
1
+ import { $ } from "bun";
2
+ import { cyan } from "../utils/colors";
3
+ export class DockerClient {
4
+ username;
5
+ constructor(username) {
6
+ this.username = username;
7
+ }
8
+ /**
9
+ * @note This class must be cleaned up manually by calling `cleanup()`.
10
+ * You can also do this manually by utilizing the [`usage`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management) keyword.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * export async function main() {
15
+ * await using client = await DockerClient.create(username, pat);
16
+ *
17
+ * client.promoteDockerImage({});
18
+ *
19
+ * // client will automatically be cleaned up at the end of the scope.
20
+ * }
21
+ * ```
22
+ */
23
+ static async create(username, pat) {
24
+ const client = new DockerClient(username);
25
+ await $ `echo ${pat} | docker login -u ${username} --password-stdin`;
26
+ return client;
27
+ }
28
+ /**
29
+ * Will lookup the Docker Hub image of `originalTag` & add `newGithubTags` to it.
30
+ */
31
+ async promoteDockerImage({ originalTag, newGithubTags, repository, }) {
32
+ const fullRepo = `${this.username}/${repository}`;
33
+ const oldImage = `${fullRepo}:${originalTag}`;
34
+ await $ `docker pull ${oldImage}`;
35
+ for (const tag of newGithubTags) {
36
+ const newImage = `${fullRepo}:${tag}`;
37
+ console.log(`Promoting to ${newImage}...`);
38
+ await $ `docker tag ${oldImage} ${newImage}`;
39
+ await $ `docker push ${newImage}`;
40
+ }
41
+ console.log(`Promoted ${originalTag} to: ${newGithubTags.join(", ")}`);
42
+ }
43
+ async buildImage(args) {
44
+ const { dockerFileLocation, tags, dockerUsername, dockerRepository, shouldUpload = true, platforms = ["linux/amd64"], buildArgs = {}, } = args;
45
+ if (!tags.length) {
46
+ throw new Error("You must provide atleast one tag.");
47
+ }
48
+ console.log("Building image with following tags:");
49
+ tags.forEach((tag) => console.log(cyan(tag)));
50
+ try {
51
+ await $ `docker buildx create --use --name ${dockerRepository}-builder`;
52
+ }
53
+ catch {
54
+ await $ `docker buildx use ${dockerRepository}-builder`;
55
+ }
56
+ const buildModeFlag = shouldUpload ? "--push" : "--load";
57
+ const tagFlags = tags.flatMap((tag) => [
58
+ "--tag",
59
+ `${dockerUsername}/${dockerRepository}:${tag}`,
60
+ ]);
61
+ const buildArgFlags = Object.entries(buildArgs).flatMap(([k, v]) => [
62
+ "--build-arg",
63
+ `${k}=${v.toString()}`,
64
+ ]);
65
+ await $ `docker buildx build ${buildModeFlag} \
66
+ --platform ${platforms.join(",")} \
67
+ --file ${dockerFileLocation} \
68
+ --cache-from=type=gha \
69
+ --cache-to=type=gha,mode=max \
70
+ ${buildArgFlags} \
71
+ ${tagFlags} \
72
+ .`;
73
+ console.log(shouldUpload ?
74
+ `Image build & successfully uploaded to ${dockerUsername}/${dockerRepository}`
75
+ : "Image has been built (upload skipped.)");
76
+ }
77
+ async [Symbol.asyncDispose]() {
78
+ await this.cleanup();
79
+ }
80
+ async cleanup() {
81
+ await $ `docker logout`;
82
+ }
83
+ }
@@ -1,42 +1 @@
1
- export declare class DockerClient {
2
- private readonly username;
3
- private constructor();
4
- /**
5
- * @note This class must be cleaned up manually by calling `cleanup()`.
6
- * You can also do this manually by utilizing the [`usage`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management) keyword.
7
- *
8
- * @example
9
- * ```ts
10
- * export async function main() {
11
- * await using client = await DockerClient.create(username, pat);
12
- *
13
- * client.promoteDockerImage({});
14
- *
15
- * // client will automatically be cleaned up at the end of the scope.
16
- * }
17
- * ```
18
- */
19
- static create(username: string, pat: string): Promise<DockerClient>;
20
- /**
21
- * Will lookup the Docker Hub image of `originalTag` & add `newGithubTags` to it.
22
- */
23
- promoteDockerImage({ originalTag, newGithubTags, repository, }: {
24
- originalTag: string;
25
- newGithubTags: string[];
26
- repository: string;
27
- }): Promise<void>;
28
- buildImage(args: {
29
- dockerFileLocation: string;
30
- /**
31
- * @example ["staging-latest", `staging-${sha}`, `staging-%{timestamp.toString()}`]
32
- */
33
- tags: string[];
34
- dockerUsername: string;
35
- dockerRepository: string;
36
- shouldUpload?: boolean;
37
- platforms?: string[];
38
- buildArgs?: Record<string, string | number | boolean>;
39
- }): Promise<void>;
40
- [Symbol.asyncDispose](): Promise<void>;
41
- cleanup(): Promise<void>;
42
- }
1
+ export * from "./client";
@@ -1,83 +1 @@
1
- import { $ } from "bun";
2
- import { cyan } from "../utils/colors";
3
- export class DockerClient {
4
- username;
5
- constructor(username) {
6
- this.username = username;
7
- }
8
- /**
9
- * @note This class must be cleaned up manually by calling `cleanup()`.
10
- * You can also do this manually by utilizing the [`usage`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management) keyword.
11
- *
12
- * @example
13
- * ```ts
14
- * export async function main() {
15
- * await using client = await DockerClient.create(username, pat);
16
- *
17
- * client.promoteDockerImage({});
18
- *
19
- * // client will automatically be cleaned up at the end of the scope.
20
- * }
21
- * ```
22
- */
23
- static async create(username, pat) {
24
- const client = new DockerClient(username);
25
- await $ `echo ${pat} | docker login -u ${username} --password-stdin`;
26
- return client;
27
- }
28
- /**
29
- * Will lookup the Docker Hub image of `originalTag` & add `newGithubTags` to it.
30
- */
31
- async promoteDockerImage({ originalTag, newGithubTags, repository, }) {
32
- const fullRepo = `${this.username}/${repository}`;
33
- const oldImage = `${fullRepo}:${originalTag}`;
34
- await $ `docker pull ${oldImage}`;
35
- for (const tag of newGithubTags) {
36
- const newImage = `${fullRepo}:${tag}`;
37
- console.log(`Promoting to ${newImage}...`);
38
- await $ `docker tag ${oldImage} ${newImage}`;
39
- await $ `docker push ${newImage}`;
40
- }
41
- console.log(`Promoted ${originalTag} to: ${newGithubTags.join(", ")}`);
42
- }
43
- async buildImage(args) {
44
- const { dockerFileLocation, tags, dockerUsername, dockerRepository, shouldUpload = true, platforms = ["linux/amd64"], buildArgs = {}, } = args;
45
- if (!tags.length) {
46
- throw new Error("You must provide atleast one tag.");
47
- }
48
- console.log("Building image with following tags:");
49
- tags.forEach((tag) => console.log(cyan(tag)));
50
- try {
51
- await $ `docker buildx create --use --name ${dockerRepository}-builder`;
52
- }
53
- catch {
54
- await $ `docker buildx use ${dockerRepository}-builder`;
55
- }
56
- const buildModeFlag = shouldUpload ? "--push" : "--load";
57
- const tagFlags = tags.flatMap((tag) => [
58
- "--tag",
59
- `${dockerUsername}/${dockerRepository}:${tag}`,
60
- ]);
61
- const buildArgFlags = Object.entries(buildArgs).flatMap(([k, v]) => [
62
- "--build-arg",
63
- `${k}=${v.toString()}`,
64
- ]);
65
- await $ `docker buildx build ${buildModeFlag} \
66
- --platform ${platforms.join(",")} \
67
- --file ${dockerFileLocation} \
68
- --cache-from=type=gha \
69
- --cache-to=type=gha,mode=max \
70
- ${buildArgFlags} \
71
- ${tagFlags} \
72
- .`;
73
- console.log(shouldUpload ?
74
- `Image build & successfully uploaded to ${dockerUsername}/${dockerRepository}`
75
- : "Image has been built (upload skipped.)");
76
- }
77
- async [Symbol.asyncDispose]() {
78
- await this.cleanup();
79
- }
80
- async cleanup() {
81
- await $ `docker logout`;
82
- }
83
- }
1
+ export * from "./client";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tahminator/pipeline",
3
3
  "type": "module",
4
- "version": "1.0.6",
4
+ "version": "1.0.7",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",