UNPKG

5.48 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright © 2018 Atomist, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17var __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};
25Object.defineProperty(exports, "__esModule", { value: true });
26const sdm_core_1 = require("@atomist/sdm-core");
27const spawned_1 = require("@atomist/sdm/api-helper/misc/spawned");
28/**
29 * Execute a Docker build for the project
30 * @param {DockerImageNameCreator} imageNameCreator
31 * @param {DockerOptions} options
32 * @returns {ExecuteGoal}
33 */
34function 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 // 1. run docker build
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 // 4. create image link
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}
72exports.executeDockerBuild = executeDockerBuild;
73function dockerPush(image, options, progressLog) {
74 return __awaiter(this, void 0, void 0, function* () {
75 const spOpts = {
76 errorFinder: code => code !== 0,
77 };
78 // Default so that we don't attempt to push in local mode
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 // 2. run docker login
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 // 3. run docker push
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}
112exports.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//# sourceMappingURL=executeDockerBuild.js.map
\No newline at end of file