UNPKG

6.3 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 automation_client_1 = require("@atomist/automation-client");
27const sdm_1 = require("@atomist/sdm");
28const sdm_core_1 = require("@atomist/sdm-core");
29const _ = require("lodash");
30/**
31 * Execute a Docker build for the project
32 * @param {DockerImageNameCreator} imageNameCreator
33 * @param {DockerOptions} options
34 * @returns {ExecuteGoal}
35 */
36function 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 // 1. run docker login
53 let result = yield dockerLogin(options, progressLog);
54 if (result.code !== 0) {
55 return result;
56 }
57 // 2. run docker build
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 // 3. run docker push
64 result = yield dockerPush(images, p, options, progressLog);
65 if (result.code !== 0) {
66 return result;
67 }
68 // 4. create image link
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}
78exports.executeDockerBuild = executeDockerBuild;
79function 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 // 2. run docker login
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}
98function dockerPush(images, project, options, progressLog) {
99 return __awaiter(this, void 0, void 0, function* () {
100 let push;
101 // tslint:disable-next-line:no-boolean-literal-compare
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 // 1. run docker push
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}
130exports.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//# sourceMappingURL=executeDockerBuild.js.map
\No newline at end of file