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 automation_client_1 = require("@atomist/automation-client");
|
27 | const sdm_1 = require("@atomist/sdm");
|
28 | const sdm_core_1 = require("@atomist/sdm-core");
|
29 | const _ = require("lodash");
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function executeDockerBuild(imageNameCreator, options) {
|
37 | return (goalInvocation) => __awaiter(this, void 0, void 0, function* () {
|
38 | const { configuration, sdmGoal, credentials, id, context, progressLog } = goalInvocation;
|
39 | return configuration.sdm.projectLoader.doWithProject({
|
40 | credentials,
|
41 | id,
|
42 | context,
|
43 | readOnly: false,
|
44 | cloneOptions: { detachHead: true },
|
45 | }, (p) => __awaiter(this, void 0, void 0, function* () {
|
46 | const opts = {
|
47 | cwd: p.baseDir,
|
48 | };
|
49 | const imageName = yield imageNameCreator(p, sdmGoal, options, context);
|
50 | const images = imageName.tags.map(tag => `${imageName.registry ? `${imageName.registry}/` : ""}${imageName.name}:${tag}`);
|
51 | const dockerfilePath = yield (options.dockerfileFinder ? options.dockerfileFinder(p) : "Dockerfile");
|
52 |
|
53 | let result = yield dockerLogin(options, progressLog);
|
54 | if (result.code !== 0) {
|
55 | return result;
|
56 | }
|
57 |
|
58 | const tags = _.flatten(images.map(i => ["-t", i]));
|
59 | result = yield sdm_1.spawnAndLog(progressLog, "docker", ["build", ".", "-f", dockerfilePath, ...tags], opts);
|
60 | if (result.code !== 0) {
|
61 | return result;
|
62 | }
|
63 |
|
64 | result = yield dockerPush(images, p, options, progressLog);
|
65 | if (result.code !== 0) {
|
66 | return result;
|
67 | }
|
68 |
|
69 | if (yield sdm_core_1.postLinkImageWebhook(sdmGoal.repo.owner, sdmGoal.repo.name, sdmGoal.sha, images[0], context.workspaceId)) {
|
70 | return result;
|
71 | }
|
72 | else {
|
73 | return { code: 1, message: "Image link failed" };
|
74 | }
|
75 | }));
|
76 | });
|
77 | }
|
78 | exports.executeDockerBuild = executeDockerBuild;
|
79 | function dockerLogin(options, progressLog) {
|
80 | return __awaiter(this, void 0, void 0, function* () {
|
81 | if (options.user && options.password) {
|
82 | progressLog.write("Running 'docker login'");
|
83 | const loginArgs = ["login", "--username", options.user, "--password", options.password];
|
84 | if (/[^A-Za-z0-9]/.test(options.registry)) {
|
85 | loginArgs.push(options.registry);
|
86 | }
|
87 |
|
88 | return sdm_1.spawnAndLog(progressLog, "docker", loginArgs, {
|
89 | logCommand: false,
|
90 | });
|
91 | }
|
92 | else {
|
93 | progressLog.write("Skipping 'docker login' because user and password are not configured");
|
94 | return automation_client_1.Success;
|
95 | }
|
96 | });
|
97 | }
|
98 | function dockerPush(images, project, options, progressLog) {
|
99 | return __awaiter(this, void 0, void 0, function* () {
|
100 | let push;
|
101 |
|
102 | if (options.push === true || options.push === false) {
|
103 | push = options.push;
|
104 | }
|
105 | else {
|
106 | push = !sdm_core_1.isInLocalMode();
|
107 | }
|
108 | let result = automation_client_1.Success;
|
109 | if ((yield sdm_1.projectConfigurationValue("docker.push.enabled", project, push))) {
|
110 | if (!options.user || !options.password) {
|
111 | const message = "Required configuration missing for pushing docker image. Please make sure to set " +
|
112 | "'registry', 'user' and 'password' in your configuration.";
|
113 | progressLog.write(message);
|
114 | return { code: 1, message };
|
115 | }
|
116 |
|
117 | for (const image of images) {
|
118 | result = yield sdm_1.spawnAndLog(progressLog, "docker", ["push", image]);
|
119 | if (result && result.code !== 0) {
|
120 | return result;
|
121 | }
|
122 | }
|
123 | }
|
124 | else {
|
125 | progressLog.write("Skipping 'docker push'");
|
126 | }
|
127 | return result;
|
128 | });
|
129 | }
|
130 | exports.DefaultDockerImageNameCreator = (p, sdmGoal, options, context) => __awaiter(this, void 0, void 0, function* () {
|
131 | const name = p.name;
|
132 | const tags = [yield sdm_core_1.readSdmVersion(sdmGoal.repo.owner, sdmGoal.repo.name, sdmGoal.repo.providerId, sdmGoal.sha, sdmGoal.branch, context)];
|
133 | const latestTag = yield sdm_1.projectConfigurationValue("docker.tag.latest", p, false);
|
134 | if (latestTag && sdmGoal.branch === sdmGoal.push.repo.defaultBranch) {
|
135 | tags.push("latest");
|
136 | }
|
137 | return {
|
138 | registry: options.registry,
|
139 | name,
|
140 | tags,
|
141 | };
|
142 | });
|
143 |
|
\ | No newline at end of file |