summaryrefslogtreecommitdiff
path: root/worker/scripts/build_image
blob: 71072241dc51390437e22170c298183159cc147c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env -S deno run --allow-env --allow-net --allow-run

import type { BuildDockerImageJobProps } from "@liz-ci/model";
import {
  BitwardenSession,
  getRequiredEnv,
  getStdout,
  type LoginItem,
} from "@liz-ci/utils";

const args: BuildDockerImageJobProps = {
  registry: getRequiredEnv("registry"),
  namespace: getRequiredEnv("namespace"),
  repository: getRequiredEnv("repository"),
  imageTag: getRequiredEnv("imageTag"),

  context: getRequiredEnv("context"),
  dockerfile: getRequiredEnv("dockerfile"),
  buildTarget: getRequiredEnv("buildTarget"),
};

const bitwardenSession = new BitwardenSession();
const { username: registryUsername, password: registryPassword } =
  (await bitwardenSession.getItem<LoginItem>(args.registry))?.login ?? {};
if (!(registryUsername && registryPassword)) {
  throw new Error("where's the login info bruh");
}

await getStdout(
  [
    "docker",
    "login",
    "--username",
    registryUsername,
    "--password",
    registryPassword,
    args.registry,
  ],
);

const tag =
  `${args.registry}/${args.namespace}/${args.repository}:${args.imageTag}`;
await getStdout(
  [
    "docker",
    "build",
    "--target",
    args.buildTarget,
    "-t",
    tag,
    "-f",
    `${args.dockerfile}`,
    `${args.context}`,
  ],
  {
    clearEnv: true,
    env: {},
  },
);

await getStdout(
  [
    "docker",
    "push",
    tag,
  ],
);