-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
138 lines (122 loc) · 4.06 KB
/
index.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const _ = require('lodash');
const colors = require('colors');
const rp = require('request-promise');
const config = require('./config');
const {host, apiToken, image} = config;
const {parseFamiliarName} = require('@codefresh-io/docker-reference');
const DIGEST_PREFIX_REGEX = /^sha256:/;
Promise.resolve()
.then(getPreRequires)
.then(buildArgs)
.then(createImage)
.then(() => console.log(colors.green(`Image created successfully`)))
.then(() => process.exit(0))
.catch(error => {
console.log(colors.red(error.message));
process.exit(1);
});
async function getPreRequires() {
try {
const reference = parseFamiliarName(image);
return Promise.all([
config.buildId && rp({
method: 'GET',
uri: `${host}/api/builds/${config.buildId}`,
headers: _getAuthHeader(),
json: true
}).then(build => {
if (!build) {
throw Error(`Can't find build with id "${config.buildId}"`);
}
return build;
}),
config.pipelineId && rp({
method: 'GET',
uri: `${host}/api/pipelines/${config.pipelineId}`,
headers: _getAuthHeader(),
json: true
}).then(pipeline => {
if (!pipeline) {
throw Error(`Can't find pipeline with id "${config.pipelineId}"`);
}
return pipeline;
})
]).then(([build, pipeline]) => ({
reference, build, pipeline,
imageName: reference.repository
}));
} catch (err) {
throw Error(`Image is incorrect: ${err}`);
}
}
function buildArgs({pipeline, build, imageName, reference}) {
const progressId = build ? build.progress_id : undefined;
const internalImageId = config.inspectId.replace(DIGEST_PREFIX_REGEX, '');
const sha = config.commitUrl && _.last(config.commitUrl.replace(/\?.+$/,'').split('/'));
const imageInfo = {
pipeline: pipeline,
buildDetails: {
progressId,
id: config.buildId,
branch: config.branch,
sha: config.sha || sha,
commit: config.commitMsg,
commitURL: config.commitUrl,
},
imageDetails: {
inspect: {
'Id': config.inspectId,
'Size': config.inspectSize,
'Os': config.inspectOs,
'Architecture': config.inspectArc,
Config: {
Labels: {
'io.codefresh.repo.owner': config.gitRepoOwner,
'io.codefresh.repo.name': config.gitRepoName,
'io.codefresh.repo.branch': config.branch,
'io.codefresh.repo.commit.message': config.commitMsg,
'io.codefresh.repo.commit.url': config.commitUrl,
'io.codefresh.repo.sha': config.sha || sha,
}
}
},
imageName,
hash: config.hash,
imageDisplayName: imageName,
dockerFile: config.dockerFile
}
};
const digest = config.digest.replace(DIGEST_PREFIX_REGEX, '');
const repoDigest = `${reference.repositoryUrl}@sha256:${digest}`;
const registryInfo = {
query: {
internalImageId
},
imageDetails: {
internalImageId,
repoDigest,
imageName,
},
tagInfo: {
registry: reference.domain,
repository: reference.repository,
tag: reference.tag
}
};
return {imageInfo, registryInfo};
}
async function createImage({imageInfo, registryInfo}) {
return rp({
method: 'POST',
uri: `${host}/api/images`,
body: {
imageInfo,
registryInfo
},
headers: _getAuthHeader(),
json: true
});
}
function _getAuthHeader() {
return {'Authorization': `Bearer ${apiToken}`};
}