Skip to content

Commit d3160f6

Browse files
committed
Handle Amazon ECR registries associated with other accounts
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
1 parent 3b14bab commit d3160f6

4 files changed

Lines changed: 77 additions & 54 deletions

File tree

README.md

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,33 @@ jobs:
251251
password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
252252
```
253253

254+
If you need to log in to Amazon ECR registries associated with other accounts, you can use the `AWS_ECR_REGISTRY_IDS`
255+
environment variable:
256+
257+
```yaml
258+
name: ci
259+
260+
on:
261+
push:
262+
branches: master
263+
264+
jobs:
265+
login:
266+
runs-on: ubuntu-latest
267+
steps:
268+
-
269+
name: Login to ECR
270+
uses: docker/login-action@v1
271+
with:
272+
registry: <aws-account-number>.dkr.ecr.<region>.amazonaws.com
273+
username: ${{ secrets.AWS_ACCESS_KEY_ID }}
274+
password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
275+
env:
276+
AWS_ECR_REGISTRY_IDS: "012345678910 023456789012"
277+
```
278+
279+
> Only available with [AWS CLI version 1](https://docs.aws.amazon.com/cli/latest/reference/ecr/get-login.html)
280+
254281
You can also use the [Configure AWS Credentials](https://github.com/aws-actions/configure-aws-credentials) action in
255282
combination with this action:
256283

@@ -311,41 +338,15 @@ jobs:
311338

312339
> Replace `<region>` with its respective value (default `us-east-1`).
313340

314-
You can also use the [Configure AWS Credentials](https://github.com/aws-actions/configure-aws-credentials) action in
315-
combination with this action:
316-
317-
```yaml
318-
name: ci
319-
320-
on:
321-
push:
322-
branches: master
323-
324-
jobs:
325-
login:
326-
runs-on: ubuntu-latest
327-
steps:
328-
-
329-
name: Configure AWS Credentials
330-
uses: aws-actions/configure-aws-credentials@v1
331-
with:
332-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
333-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
334-
aws-region: <region>
335-
-
336-
name: Login to Public ECR
337-
uses: docker/login-action@v1
338-
with:
339-
registry: public.ecr.aws
340-
```
341-
342-
> Replace `<region>` with its respective value.
343-
344341
### OCI Oracle Cloud Infrastructure Registry (OCIR)
342+
345343
To push into OCIR in specific tenancy the [username](https://www.oracle.com/webfolder/technetwork/tutorials/obe/oci/registry/index.html#LogintoOracleCloudInfrastructureRegistryfromtheDockerCLI)
346-
must be placed in format `<tenancy>/<username>` (in case of federated tenancy use the format `<tenancy-namespace>/oracleidentitycloudservice/<username>`).
347-
For password [create an auth token](https://www.oracle.com/webfolder/technetwork/tutorials/obe/oci/registry/index.html#GetanAuthToken). Save username and token
348-
[as a secrets](https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets#creating-encrypted-secrets-for-a-repository) in your GitHub repo.
344+
must be placed in format `<tenancy>/<username>` (in case of federated tenancy use the format
345+
`<tenancy-namespace>/oracleidentitycloudservice/<username>`).
346+
347+
For password [create an auth token](https://www.oracle.com/webfolder/technetwork/tutorials/obe/oci/registry/index.html#GetanAuthToken).
348+
Save username and token [as a secrets](https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets#creating-encrypted-secrets-for-a-repository)
349+
in your GitHub repo.
349350

350351
```yaml
351352
name: ci
@@ -366,6 +367,7 @@ jobs:
366367
username: ${{ secrets.OCI_USERNAME }}
367368
password: ${{ secrets.OCI_TOKEN }}
368369
```
370+
369371
> Replace `<region>` with their respective values from [availability regions](https://docs.cloud.oracle.com/iaas/Content/Registry/Concepts/registryprerequisites.htm#Availab)
370372

371373
## Customizing

dist/index.js

Lines changed: 22 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/aws.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,19 @@ export const parseCLIVersion = async (stdout: string): Promise<string> => {
4545
return semver.clean(matches[1]);
4646
};
4747

48-
export const getDockerLoginCmd = async (cliVersion: string, registry: string, region: string): Promise<string> => {
48+
export const getDockerLoginCmds = async (cliVersion: string, registry: string, region: string): Promise<string[]> => {
4949
let ecrCmd = (await isPubECR(registry)) ? 'ecr-public' : 'ecr';
5050
if (semver.satisfies(cliVersion, '>=2.0.0')) {
5151
return execCLI([ecrCmd, 'get-login-password', '--region', region]).then(pwd => {
52-
return `docker login --username AWS --password ${pwd} ${registry}`;
52+
return [`docker login --username AWS --password ${pwd} ${registry}`];
5353
});
5454
} else {
55-
return execCLI([ecrCmd, 'get-login', '--region', region, '--no-include-email']).then(dockerLoginCmd => {
56-
return dockerLoginCmd;
55+
let args: Array<string> = [ecrCmd, 'get-login', '--region', region, '--no-include-email'];
56+
if (process.env.AWS_ECR_REGISTRY_IDS) {
57+
args.push('--registry-ids', process.env.AWS_ECR_REGISTRY_IDS);
58+
}
59+
return execCLI(args).then(dockerLoginCmds => {
60+
return dockerLoginCmds.trim().split(`\n`);
5761
});
5862
}
5963
};

src/docker.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,19 @@ export async function loginECR(registry: string, username: string, password: str
5555
process.env.AWS_SECRET_ACCESS_KEY = password || process.env.AWS_SECRET_ACCESS_KEY;
5656

5757
core.info(`⬇️ Retrieving docker login command through AWS CLI ${cliVersion} (${cliPath})...`);
58-
const loginCmd = await aws.getDockerLoginCmd(cliVersion, registry, region);
58+
const loginCmds = await aws.getDockerLoginCmds(cliVersion, registry, region);
5959

6060
core.info(`🔑 Logging into ${registry}...`);
61-
execm.exec(loginCmd, [], true).then(res => {
62-
if (res.stderr != '' && !res.success) {
63-
throw new Error(res.stderr);
64-
}
65-
core.info('🎉 Login Succeeded!');
61+
loginCmds.forEach((loginCmd, index) => {
62+
execm.exec(loginCmd, [], true).then(res => {
63+
if (res.stderr != '' && !res.success) {
64+
throw new Error(res.stderr);
65+
}
66+
if (loginCmds.length > 1) {
67+
core.info(`🎉 Login Succeeded! (${index}/${loginCmds.length})`);
68+
} else {
69+
core.info('🎉 Login Succeeded!');
70+
}
71+
});
6672
});
6773
}

0 commit comments

Comments
 (0)