forked from aws-actions/amazon-ecr-login
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (139 loc) · 5.45 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const core = require('@actions/core');
const exec = require('@actions/exec');
const aws = require('aws-sdk');
const ECR_LOGIN_GITHUB_ACTION_USER_AGENT = 'amazon-ecr-login-for-github-actions';
const ECR_PUBLIC_REGISTRY_URI = 'public.ecr.aws';
const INPUTS = {
skipLogout: 'skip-logout',
registries: 'registries',
registryType: 'registry-type'
};
const OUTPUTS = {
registry: 'registry',
dockerUsername: 'docker_username',
dockerPassword: 'docker_password'
};
const STATES = {
registries: 'registries'
};
const REGISTRY_TYPES = {
private: 'private',
public: 'public'
};
function replaceSpecialCharacters(registryUri) {
return registryUri.replace(/[^a-zA-Z0-9_]+/g, '_');
}
async function getEcrAuthTokenWrapper(authTokenRequest) {
const ecr = new aws.ECR({
customUserAgent: ECR_LOGIN_GITHUB_ACTION_USER_AGENT
});
const authTokenResponse = await ecr.getAuthorizationToken(authTokenRequest).promise();
if (!authTokenResponse) {
throw new Error('Amazon ECR authorization token returned no data');
} else if (!authTokenResponse.authorizationData || !Array.isArray(authTokenResponse.authorizationData)) {
throw new Error('Amazon ECR authorization token is invalid');
} else if (!authTokenResponse.authorizationData.length) {
throw new Error('Amazon ECR authorization token does not contain any authorization data');
}
return authTokenResponse;
}
async function getEcrPublicAuthTokenWrapper(authTokenRequest) {
const ecrPublic = new aws.ECRPUBLIC({
customUserAgent: ECR_LOGIN_GITHUB_ACTION_USER_AGENT
});
const authTokenResponse = await ecrPublic.getAuthorizationToken(authTokenRequest).promise();
if (!authTokenResponse) {
throw new Error('Amazon ECR Public authorization token returned no data');
} else if (!authTokenResponse.authorizationData) {
throw new Error('Amazon ECR Public authorization token is invalid');
} else if (Object.keys(authTokenResponse.authorizationData).length === 0) {
throw new Error('Amazon ECR Public authorization token does not contain any authorization data');
}
return {
authorizationData: [
{
authorizationToken: authTokenResponse.authorizationData.authorizationToken,
proxyEndpoint: ECR_PUBLIC_REGISTRY_URI
}
]
};
}
async function run() {
// Get inputs
const skipLogout = core.getInput(INPUTS.skipLogout, { required: false }).toLowerCase() === 'true';
const registries = core.getInput(INPUTS.registries, { required: false });
const registryType = core.getInput(INPUTS.registryType, { required: false }).toLowerCase() || REGISTRY_TYPES.private;
const registryUriState = [];
try {
if (registryType !== REGISTRY_TYPES.private && registryType !== REGISTRY_TYPES.public) {
throw new Error(`Invalid input for '${INPUTS.registryType}', possible options are [${REGISTRY_TYPES.private}, ${REGISTRY_TYPES.public}]`);
}
// Get the ECR/ECR Public authorization token(s)
const authTokenRequest = {};
if (registryType === REGISTRY_TYPES.private && registries) {
const registryIds = registries.split(',');
core.debug(`Requesting auth token for ${registryIds.length} registries:`);
for (const id of registryIds) {
core.debug(` '${id}'`);
}
authTokenRequest.registryIds = registryIds;
}
const authTokenResponse = registryType === REGISTRY_TYPES.private ?
await getEcrAuthTokenWrapper(authTokenRequest) :
await getEcrPublicAuthTokenWrapper(authTokenRequest);
// Login to each registry
for (const authData of authTokenResponse.authorizationData) {
const authToken = Buffer.from(authData.authorizationToken, 'base64').toString('utf-8');
const creds = authToken.split(':', 2);
const proxyEndpoint = authData.proxyEndpoint;
const registryUri = proxyEndpoint.replace(/^https?:\/\//,'');
core.info(`Logging into registry ${registryUri}`);
// output the registry URI if this action is doing a single registry login
if (authTokenResponse.authorizationData.length === 1) {
core.setOutput(OUTPUTS.registry, registryUri);
}
// Execute the docker login command
let doLoginStdout = '';
let doLoginStderr = '';
const exitCode = await exec.exec('docker', ['login', '-u', creds[0], '-p', creds[1], 'docker.billogram.com'], {
silent: true,
ignoreReturnCode: true,
listeners: {
stdout: (data) => {
doLoginStdout += data.toString();
},
stderr: (data) => {
doLoginStderr += data.toString();
}
}
});
if (exitCode !== 0) {
core.debug(doLoginStdout);
throw new Error(`Could not login to registry ${registryUri}: ${doLoginStderr}`);
}
// Output docker username and password
const secretSuffix = replaceSpecialCharacters(registryUri);
core.setOutput(`${OUTPUTS.dockerUsername}_${secretSuffix}`, creds[0]);
core.setOutput(`${OUTPUTS.dockerPassword}_${secretSuffix}`, creds[1]);
registryUriState.push(registryUri);
}
}
catch (error) {
core.setFailed(error.message);
}
// Pass the logged-in registry URIs to the post action for logout
if (registryUriState.length) {
if (!skipLogout) {
core.saveState(STATES.registries, registryUriState.join());
}
core.debug(`'${INPUTS.skipLogout}' is ${skipLogout} for ${registryUriState.length} registries.`);
}
}
module.exports = {
run,
replaceSpecialCharacters
};
/* istanbul ignore next */
if (require.main === module) {
run();
}