1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
18 | return new (P || (P = Promise))(function (resolve, reject) {
|
19 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
20 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
21 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
22 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
23 | });
|
24 | };
|
25 | Object.defineProperty(exports, "__esModule", { value: true });
|
26 | const sdm_core_1 = require("@atomist/sdm-core");
|
27 | const spawned_1 = require("@atomist/sdm/api-helper/misc/spawned");
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function executeDockerBuild(imageNameCreator, preparations = [], options) {
|
35 | return (goalInvocation) => __awaiter(this, void 0, void 0, function* () {
|
36 | const { configuration, sdmGoal, credentials, id, context, progressLog } = goalInvocation;
|
37 | return configuration.sdm.projectLoader.doWithProject({ credentials, id, context, readOnly: false }, (p) => __awaiter(this, void 0, void 0, function* () {
|
38 | for (const preparation of preparations) {
|
39 | const pResult = yield preparation(p, goalInvocation);
|
40 | if (pResult.code !== 0) {
|
41 | return pResult;
|
42 | }
|
43 | }
|
44 | const opts = {
|
45 | cwd: p.baseDir,
|
46 | };
|
47 | const spOpts = {
|
48 | errorFinder: code => code !== 0,
|
49 | };
|
50 | const imageName = yield imageNameCreator(p, sdmGoal, options, context);
|
51 | const image = `${imageName.registry}/${imageName.name}:${imageName.version}`;
|
52 | const dockerfilePath = yield (options.dockerfileFinder ? options.dockerfileFinder(p) : "Dockerfile");
|
53 |
|
54 | let result = yield spawned_1.spawnAndWatch({
|
55 | command: "docker",
|
56 | args: ["build", ".", "-f", dockerfilePath, "-t", image],
|
57 | }, opts, progressLog, spOpts);
|
58 | if (result.code !== 0) {
|
59 | return result;
|
60 | }
|
61 | result = yield dockerPush(image, options, progressLog);
|
62 |
|
63 | if (yield sdm_core_1.postLinkImageWebhook(sdmGoal.repo.owner, sdmGoal.repo.name, sdmGoal.sha, image, context.workspaceId)) {
|
64 | return result;
|
65 | }
|
66 | else {
|
67 | return { code: 1, message: "Image link failed" };
|
68 | }
|
69 | }));
|
70 | });
|
71 | }
|
72 | exports.executeDockerBuild = executeDockerBuild;
|
73 | function dockerPush(image, options, progressLog) {
|
74 | return __awaiter(this, void 0, void 0, function* () {
|
75 | const spOpts = {
|
76 | errorFinder: code => code !== 0,
|
77 | };
|
78 |
|
79 | if (options.push === undefined) {
|
80 | options.push = !sdm_core_1.isInLocalMode();
|
81 | }
|
82 | if (options.push) {
|
83 | if (!options.user || !options.password) {
|
84 | const message = "Required configuration missing for pushing docker image. Please make sure to set " +
|
85 | "'registry', 'user' and 'password' in your configuration.";
|
86 | progressLog.write(message);
|
87 | return { code: 1, message };
|
88 | }
|
89 | const loginArgs = ["login", "--username", options.user, "--password", options.password];
|
90 | if (/[^A-Za-z0-9]/.test(options.registry)) {
|
91 | loginArgs.push(options.registry);
|
92 | }
|
93 |
|
94 | let result = yield spawned_1.spawnAndWatch({
|
95 | command: "docker",
|
96 | args: loginArgs,
|
97 | }, {}, progressLog, Object.assign({}, spOpts, { logCommand: false }));
|
98 | if (result.code !== 0) {
|
99 | return result;
|
100 | }
|
101 |
|
102 | result = yield spawned_1.spawnAndWatch({
|
103 | command: "docker",
|
104 | args: ["push", image],
|
105 | }, {}, progressLog, spOpts);
|
106 | if (result.code !== 0) {
|
107 | return result;
|
108 | }
|
109 | }
|
110 | });
|
111 | }
|
112 | exports.DefaultDockerImageNameCreator = (p, sdmGoal, options, context) => __awaiter(this, void 0, void 0, function* () {
|
113 | const name = p.name;
|
114 | const version = yield sdm_core_1.readSdmVersion(sdmGoal.repo.owner, sdmGoal.repo.name, sdmGoal.repo.providerId, sdmGoal.sha, sdmGoal.branch, context);
|
115 | return {
|
116 | registry: options.registry,
|
117 | name,
|
118 | version,
|
119 | };
|
120 | });
|
121 |
|
\ | No newline at end of file |