-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (57 loc) · 1.65 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
import fetch from 'node-fetch';
import core from '@actions/core';
import limitTags from './modules/limitTags.js';
let repoUrl, images, regex, versionLimit;
let imageArray = [];
let outputImages = [];
let promises = [];
try {
repoUrl = core.getInput('repo-url');
images = core.getInput('images');
regex = new RegExp(core.getInput('tag-regex'));
versionLimit = core.getInput('version-limit');
} catch (err) {
const msg = 'Failed to initialize action: ' + err.message;
core.setFailed(msg);
console.error(msg);
console.error(err);
process.exit(1);
}
const processResponse = (res) => {
if (res.status < 400) {
return res.json();
} else {
throw new Error(
`Response from server: '${res.statusText}' (${res.status}) for URL ${res.url}`
);
}
};
try {
imageArray = JSON.parse(images);
} catch (err) {
imageArray.push(images);
}
imageArray.forEach((image) => {
promises.push(
fetch(`${repoUrl}/v2/${image}/tags/list`)
.then((response) => processResponse(response))
.then((json) => {
let { tags } = json;
let filteredTags = tags.filter(tag => regex.test(tag));
let limitedTags = limitTags(filteredTags, versionLimit);
let fullNames = limitedTags.map((tag) => `${image}:${tag}`);
outputImages.push(...fullNames);
})
.catch((err) => {
const msg = 'Failed to scan Docker repository: ' + err.message;
core.setFailed(msg);
console.error(msg);
console.error(err);
process.exit(1);
})
)
});
Promise.all(promises).then(() => {
console.log(`Images discovered:\n${outputImages.join('\n')}`);
core.setOutput('images', outputImages);
});