forked from jwalton/gh-ecr-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
77 lines (68 loc) · 2.68 KB
/
main.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
const { execSync } = require('child_process');
const core = require('@actions/core');
const { getImagesToPush } = require('./images.js');
const AWS_ACCESS_KEY_ID = core.getInput('access-key-id', { required: true });
const AWS_SECRET_ACCESS_KEY = core.getInput('secret-access-key', { required: true });
const image = core.getInput('image', { required: true });
const localImage = core.getInput('local-image') || image;
const awsRegion = core.getInput('region') || process.env.AWS_DEFAULT_REGION || 'us-east-1';
const direction = core.getInput('direction') || 'push';
const isSemver = core.getInput('is-semver');
const skipTag = core.getInput('skip-tag');
function run(cmd, options = {}) {
if (!options.hide) {
console.log(`$ ${cmd}`);
}
return execSync(cmd, {
shell: '/bin/bash',
encoding: 'utf-8',
env: {
...process.env,
AWS_PAGER: '', // Disable the pager.
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
},
});
}
const accountLoginPassword = `aws ecr get-login-password --region ${awsRegion}`;
const accountData = run(`aws sts get-caller-identity --output json --region ${awsRegion}`);
const awsAccountId = JSON.parse(accountData).Account;
let imageUrl;
run(
`${accountLoginPassword} | docker login --username AWS --password-stdin ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com`
);
if (localImage.includes(',')) {
if (!core.getInput('local-image')) {
throw new Error('local-image must be specified if image is a list.');
} else {
throw new Error('local-image may not be a list of images.');
}
}
if (direction === 'push') {
const imagesToPush = getImagesToPush(localImage, image, isSemver);
for (const imageToPush of imagesToPush) {
const uri = skipTag
? imageToPush.remoteImage
: `${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${imageToPush.remoteImage}`;
console.log(`Pushing local image ${imageToPush.localImage} to ${uri}`);
if (!skipTag) {
run(`docker tag ${imageToPush.localImage} ${uri}`);
} else {
console.log('Skip tagging because of skipTag=true')
}
run(`docker push ${uri}`);
imageUrl = uri;
}
} else if (direction == 'pull') {
if (image.includes(',')) {
throw new Error('image may not be a list of images when pulling');
}
const uri = `${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${image}`;
console.log(`Pulling ${uri} to ${localImage}`);
run(`docker pull ${uri}`);
run(`docker tag ${uri} ${localImage} `);
imageUrl = uri;
} else {
throw new Error(`Unknown direction ${direction}`);
}
core.setOutput('imageUrl', imageUrl);