generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (44 loc) · 1.55 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
const core = require('@actions/core');
const aws = require('aws-sdk');
async function run() {
try {
// Get input
const registryId = core.getInput("registry-id", {required: false}) || undefined;
const repositoryName = core.getInput("repository-name", {required: true});
const sourceImageTag = core.getInput("source-image-tag", {required: true});
const targetImageTag = core.getInput("target-image-tag", {required: true});
// Create required resources
const ecr = new aws.ECR();
// Fetch image manifest from ECR
core.info(`Fetching image data for ${repositoryName}:${sourceImageTag}.`);
const getImageResp = await ecr.batchGetImage({
registryId,
repositoryName,
imageIds: [{imageTag: sourceImageTag}],
}).promise();
const image = getImageResp.images[0];
const imageDigest = image.imageId.imageDigest;
const imageManifest = image.imageManifest;
const imageManifestMediaType = image.imageManifestMediaType;
// Put image manifest on ECR with new tag
core.info(`Putting tag ${repositoryName}:${targetImageTag} on digest ${imageDigest}.`);
try {
await ecr.putImage({
registryId,
repositoryName,
imageManifest,
imageManifestMediaType,
imageTag: targetImageTag,
}).promise();
} catch (error) {
if (error.code === "ImageAlreadyExistsException") {
core.info(`Digest ${imageDigest} already has tag ${targetImageTag}`)
} else {
throw error;
}
}
} catch (error) {
core.setFailed(error.message);
}
}
run();