-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
75 lines (63 loc) · 2.15 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
const core = require('@actions/core');
const aws = require("@aws-sdk/client-ecr");
async function run() {
try {
const region = core.getInput('aws-region', { required: false })
const ecrArgs = {}
if (region) {
ecrArgs['region'] = region
}
const ecr = new aws.ECR(ecrArgs)
const registryId = core.getInput('aws-account-id', { required: false })
const repositoryName = core.getInput('repository', { required: true })
const imageTag = core.getInput('tag', { required: true })
const newTags = core.getInput('new-tags', { required: true }).replace(/\s+/g, '').split(',')
const getImageParams = { repositoryName, imageIds: [{ imageTag }] }
if (registryId) {
getImageParams['registryId'] = registryId
}
let putImageCallback = function (err, result) {
if (err) {
if (err instanceof aws.ImageAlreadyExistsException) {
core.info(`${err.message}, no action`)
return
}
core.setFailed(err.message)
}
let image = result.image
core.info(`Image tagged: ${image.repositoryName}:${image.imageId.imageTag}`)
core.debug(result)
}
let getImageCallback = function (err, result) {
if (err) {
core.setFailed(err.message)
}
if (result.failures.length > 0) {
const failure = result.failures[0]
core.setFailed(`${failure.failureCode}: ${failure.failureReason} for tag ${failure.imageId.imageTag}`)
}
let image = result.images[0]
core.info(`Image found: ${image.repositoryName}:${image.imageId.imageTag}`)
core.debug(image)
newTags.forEach(function (tag) {
ecr.putImage(
{
registryId: image.registryId,
repositoryName: image.repositoryName, /* required */
imageManifest: image.imageManifest, /* required */
imageTag: tag,
},
putImageCallback
)
})
}
ecr.batchGetImage(getImageParams, getImageCallback);
} catch (e) {
core.setFailed(e instanceof Error ? e.message : JSON.stringify(e))
}
}
module.exports = run;
/* istanbul ignore next */
if (require.main === module) {
run();
}