forked from jwalton/gh-ecr-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
41 lines (36 loc) · 1.7 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
const { execSync } = require('child_process');
const core = require('@actions/core');
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';
function run(cmd, options = {}) {
if (!options.hide) {
console.log(`$ ${cmd}`);
}
return execSync(cmd, {
shell: '/bin/bash',
encoding: 'utf-8',
env: {
...process.env,
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
},
});
}
run(`$(aws ecr get-login --no-include-email --region ${awsRegion})`);
const accountData = run(`aws sts get-caller-identity --output json`);
const awsAccountId = JSON.parse(accountData).Account;
if (direction === 'push') {
console.log(`Pushing local image ${localImage} to ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${image}`);
run(`docker tag ${localImage} ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${image}`);
run(`docker push ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${image}`);
} else if (direction == 'pull') {
console.log("Pulling ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${image} to ${localImage}");
run(`docker pull ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${image}`);
run(`docker tag ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${image} ${localImage} `);
} else {
throw new Error(`Unknown direction ${direction}`);
}