-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdocker.js
More file actions
104 lines (90 loc) · 2.87 KB
/
Copy pathdocker.js
File metadata and controls
104 lines (90 loc) · 2.87 KB
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
import { ECRClient, BatchGetImageCommand } from '@aws-sdk/client-ecr';
import fs from 'fs';
import ora from 'ora';
import Handlebars from 'handlebars';
const retries = {};
const MAX_RETRIES = 60;
/**
* Ensure docker artifacts are present before deploy
*
* @param {Credentials} creds Credentials
*/
export default async function check(creds) {
const images = [];
// docker check explicitly disabled
if (
creds.dotdeploy.artifacts
&& creds.dotdeploy.artifacts.docker === false
) {
return;
} else if (
!creds.dotdeploy.artifacts
|| !creds.dotdeploy.artifacts.docker
) {
// No dotdeploy or docker file found
try {
fs.accessSync('./Dockerfile');
} catch (err) {
return;
}
images.push('{{project}}:{{gitsha}}');
} else if (
creds.dotdeploy.artifacts
&& creds.dotdeploy.artifacts.docker
) {
if (typeof creds.dotdeploy.artifacts.docker === 'string') {
images.push(creds.dotdeploy.artifacts.docker);
} else {
creds.dotdeploy.artifacts.docker.forEach((image) => {
images.push(image);
});
}
}
for (const image of images) {
await single(creds, image);
}
return true;
}
function single(creds, image) {
return new Promise((resolve, reject) => {
const ecr = new ECRClient({
credentials: creds.aws,
region: creds.region
});
image = Handlebars.compile(image)({
rootStackName: `${creds.repo}-${creds.stack}`,
fullStackName: `${creds.repo}-${creds.name}`,
accountId: creds._accountId,
stack: creds.stack,
region: creds.region,
project: creds.repo,
gitsha: creds.sha
});
const progress = ora(`Docker Image: AWS::ECR:${image}`).start();
retries[image] = 0;
if (image.split(':').length !== 2) {
return reject(new Error('docker artifact must be in format <ECR>:<TAG>'));
}
checkecr();
async function checkecr() {
try {
const data = await ecr.send(new BatchGetImageCommand({
imageIds: [{ imageTag: image.split(':')[1] }],
repositoryName: image.split(':')[0]
}));
if (data && data.images.length) {
progress.succeed();
return resolve(image);
} else if (retries[image] < MAX_RETRIES) {
retries[image] += 1;
setTimeout(checkecr, 5000);
} else {
progress.fail();
return reject(new Error(`No image found for: ${image}`));
}
} catch (err) {
return reject(err);
}
}
});
}