From d3160f671f5d9d7f2024934cb0d15488a6dc44d0 Mon Sep 17 00:00:00 2001
From: CrazyMax <crazy-max@users.noreply.github.com>
Date: Wed, 16 Dec 2020 21:53:24 +0100
Subject: [PATCH 1/2] Handle Amazon ECR registries associated with other
 accounts

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
---
 README.md     | 68 ++++++++++++++++++++++++++-------------------------
 dist/index.js | 33 ++++++++++++++++---------
 src/aws.ts    | 12 ++++++---
 src/docker.ts | 18 +++++++++-----
 4 files changed, 77 insertions(+), 54 deletions(-)

diff --git a/README.md b/README.md
index 667e1d5..54ceed4 100644
--- a/README.md
+++ b/README.md
@@ -251,6 +251,33 @@ jobs:
           password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
 ```
 
+If you need to log in to Amazon ECR registries associated with other accounts, you can use the `AWS_ECR_REGISTRY_IDS`
+environment variable:
+
+```yaml
+name: ci
+
+on:
+  push:
+    branches: master
+
+jobs:
+  login:
+    runs-on: ubuntu-latest
+    steps:
+      -
+        name: Login to ECR
+        uses: docker/login-action@v1
+        with:
+          registry: <aws-account-number>.dkr.ecr.<region>.amazonaws.com
+          username: ${{ secrets.AWS_ACCESS_KEY_ID }}
+          password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
+        env:
+          AWS_ECR_REGISTRY_IDS: "012345678910 023456789012"
+```
+
+> Only available with [AWS CLI version 1](https://docs.aws.amazon.com/cli/latest/reference/ecr/get-login.html)
+
 You can also use the [Configure AWS Credentials](https://github.com/aws-actions/configure-aws-credentials) action in
 combination with this action:
 
@@ -311,41 +338,15 @@ jobs:
 
 > Replace `<region>` with its respective value (default `us-east-1`).
 
-You can also use the [Configure AWS Credentials](https://github.com/aws-actions/configure-aws-credentials) action in
-combination with this action:
-
-```yaml
-name: ci
-
-on:
-  push:
-    branches: master
-
-jobs:
-  login:
-    runs-on: ubuntu-latest
-    steps:
-      -
-        name: Configure AWS Credentials
-        uses: aws-actions/configure-aws-credentials@v1
-        with:
-          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
-          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
-          aws-region: <region>
-      -
-        name: Login to Public ECR
-        uses: docker/login-action@v1
-        with:
-          registry: public.ecr.aws
-```
-
-> Replace `<region>` with its respective value.
-
 ### OCI Oracle Cloud Infrastructure Registry (OCIR)
+
 To push into OCIR in specific tenancy the [username](https://www.oracle.com/webfolder/technetwork/tutorials/obe/oci/registry/index.html#LogintoOracleCloudInfrastructureRegistryfromtheDockerCLI)
-must be placed in format `<tenancy>/<username>` (in case of federated tenancy use the format `<tenancy-namespace>/oracleidentitycloudservice/<username>`). 
-For password [create an auth token](https://www.oracle.com/webfolder/technetwork/tutorials/obe/oci/registry/index.html#GetanAuthToken). 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) in your GitHub repo. 
+must be placed in format `<tenancy>/<username>` (in case of federated tenancy use the format
+`<tenancy-namespace>/oracleidentitycloudservice/<username>`).
+
+For password [create an auth token](https://www.oracle.com/webfolder/technetwork/tutorials/obe/oci/registry/index.html#GetanAuthToken).
+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)
+in your GitHub repo. 
 
 ```yaml
 name: ci
@@ -366,6 +367,7 @@ jobs:
           username: ${{ secrets.OCI_USERNAME }}
           password: ${{ secrets.OCI_TOKEN }}
 ```
+
 > Replace `<region>` with their respective values from [availability regions](https://docs.cloud.oracle.com/iaas/Content/Registry/Concepts/registryprerequisites.htm#Availab)
 
 ## Customizing
diff --git a/dist/index.js b/dist/index.js
index 0b7bfa5..a8c8bd0 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -3097,13 +3097,20 @@ function loginECR(registry, username, password) {
         process.env.AWS_ACCESS_KEY_ID = username || process.env.AWS_ACCESS_KEY_ID;
         process.env.AWS_SECRET_ACCESS_KEY = password || process.env.AWS_SECRET_ACCESS_KEY;
         core.info(`⬇️ Retrieving docker login command through AWS CLI ${cliVersion} (${cliPath})...`);
-        const loginCmd = yield aws.getDockerLoginCmd(cliVersion, registry, region);
+        const loginCmds = yield aws.getDockerLoginCmds(cliVersion, registry, region);
         core.info(`🔑 Logging into ${registry}...`);
-        execm.exec(loginCmd, [], true).then(res => {
-            if (res.stderr != '' && !res.success) {
-                throw new Error(res.stderr);
-            }
-            core.info('🎉 Login Succeeded!');
+        loginCmds.forEach((loginCmd, index) => {
+            execm.exec(loginCmd, [], true).then(res => {
+                if (res.stderr != '' && !res.success) {
+                    throw new Error(res.stderr);
+                }
+                if (loginCmds.length > 1) {
+                    core.info(`🎉 Login Succeeded! (${index}/${loginCmds.length})`);
+                }
+                else {
+                    core.info('🎉 Login Succeeded!');
+                }
+            });
         });
     });
 }
@@ -4160,7 +4167,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
     });
 };
 Object.defineProperty(exports, "__esModule", { value: true });
-exports.getDockerLoginCmd = exports.parseCLIVersion = exports.getCLIVersion = exports.execCLI = exports.getCLI = exports.getRegion = exports.isPubECR = exports.isECR = void 0;
+exports.getDockerLoginCmds = exports.parseCLIVersion = exports.getCLIVersion = exports.execCLI = exports.getCLI = exports.getRegion = exports.isPubECR = exports.isECR = void 0;
 const semver = __importStar(__webpack_require__(383));
 const io = __importStar(__webpack_require__(436));
 const execm = __importStar(__webpack_require__(757));
@@ -4202,16 +4209,20 @@ exports.parseCLIVersion = (stdout) => __awaiter(void 0, void 0, void 0, function
     }
     return semver.clean(matches[1]);
 });
-exports.getDockerLoginCmd = (cliVersion, registry, region) => __awaiter(void 0, void 0, void 0, function* () {
+exports.getDockerLoginCmds = (cliVersion, registry, region) => __awaiter(void 0, void 0, void 0, function* () {
     let ecrCmd = (yield exports.isPubECR(registry)) ? 'ecr-public' : 'ecr';
     if (semver.satisfies(cliVersion, '>=2.0.0')) {
         return exports.execCLI([ecrCmd, 'get-login-password', '--region', region]).then(pwd => {
-            return `docker login --username AWS --password ${pwd} ${registry}`;
+            return [`docker login --username AWS --password ${pwd} ${registry}`];
         });
     }
     else {
-        return exports.execCLI([ecrCmd, 'get-login', '--region', region, '--no-include-email']).then(dockerLoginCmd => {
-            return dockerLoginCmd;
+        let args = [ecrCmd, 'get-login', '--region', region, '--no-include-email'];
+        if (process.env.AWS_ECR_REGISTRY_IDS) {
+            args.push('--registry-ids', process.env.AWS_ECR_REGISTRY_IDS);
+        }
+        return exports.execCLI(args).then(dockerLoginCmds => {
+            return dockerLoginCmds.trim().split(`\n`);
         });
     }
 });
diff --git a/src/aws.ts b/src/aws.ts
index c6aa9c7..22c1d89 100644
--- a/src/aws.ts
+++ b/src/aws.ts
@@ -45,15 +45,19 @@ export const parseCLIVersion = async (stdout: string): Promise<string> => {
   return semver.clean(matches[1]);
 };
 
-export const getDockerLoginCmd = async (cliVersion: string, registry: string, region: string): Promise<string> => {
+export const getDockerLoginCmds = async (cliVersion: string, registry: string, region: string): Promise<string[]> => {
   let ecrCmd = (await isPubECR(registry)) ? 'ecr-public' : 'ecr';
   if (semver.satisfies(cliVersion, '>=2.0.0')) {
     return execCLI([ecrCmd, 'get-login-password', '--region', region]).then(pwd => {
-      return `docker login --username AWS --password ${pwd} ${registry}`;
+      return [`docker login --username AWS --password ${pwd} ${registry}`];
     });
   } else {
-    return execCLI([ecrCmd, 'get-login', '--region', region, '--no-include-email']).then(dockerLoginCmd => {
-      return dockerLoginCmd;
+    let args: Array<string> = [ecrCmd, 'get-login', '--region', region, '--no-include-email'];
+    if (process.env.AWS_ECR_REGISTRY_IDS) {
+      args.push('--registry-ids', process.env.AWS_ECR_REGISTRY_IDS);
+    }
+    return execCLI(args).then(dockerLoginCmds => {
+      return dockerLoginCmds.trim().split(`\n`);
     });
   }
 };
diff --git a/src/docker.ts b/src/docker.ts
index fd3f773..e2c005d 100644
--- a/src/docker.ts
+++ b/src/docker.ts
@@ -55,13 +55,19 @@ export async function loginECR(registry: string, username: string, password: str
   process.env.AWS_SECRET_ACCESS_KEY = password || process.env.AWS_SECRET_ACCESS_KEY;
 
   core.info(`⬇️ Retrieving docker login command through AWS CLI ${cliVersion} (${cliPath})...`);
-  const loginCmd = await aws.getDockerLoginCmd(cliVersion, registry, region);
+  const loginCmds = await aws.getDockerLoginCmds(cliVersion, registry, region);
 
   core.info(`🔑 Logging into ${registry}...`);
-  execm.exec(loginCmd, [], true).then(res => {
-    if (res.stderr != '' && !res.success) {
-      throw new Error(res.stderr);
-    }
-    core.info('🎉 Login Succeeded!');
+  loginCmds.forEach((loginCmd, index) => {
+    execm.exec(loginCmd, [], true).then(res => {
+      if (res.stderr != '' && !res.success) {
+        throw new Error(res.stderr);
+      }
+      if (loginCmds.length > 1) {
+        core.info(`🎉 Login Succeeded! (${index}/${loginCmds.length})`);
+      } else {
+        core.info('🎉 Login Succeeded!');
+      }
+    });
   });
 }

From 24646ef465dfd8406cf895b1db665da90db9e185 Mon Sep 17 00:00:00 2001
From: CrazyMax <crazy-max@users.noreply.github.com>
Date: Thu, 17 Dec 2020 20:21:48 +0100
Subject: [PATCH 2/2] Get AccountID from registry URL and handle ECR registry
 through regexp

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
---
 README.md             |  8 +++---
 __tests__/aws.test.ts | 33 ++++++++++++++++++++++++
 dist/index.js         | 58 ++++++++++++++++++++++++++++++-------------
 src/aws.ts            | 54 +++++++++++++++++++++++++++++++---------
 src/docker.ts         |  3 ++-
 5 files changed, 122 insertions(+), 34 deletions(-)

diff --git a/README.md b/README.md
index 54ceed4..ebe1a50 100644
--- a/README.md
+++ b/README.md
@@ -227,7 +227,7 @@ jobs:
 
 ### AWS Elastic Container Registry (ECR)
 
-Use an IAM user with the [ability to push to ECR](https://docs.aws.amazon.com/AmazonECR/latest/userguide/ecr_managed_policies.html).
+Use an IAM user with the ability to [push to ECR with `AmazonEC2ContainerRegistryPowerUser` managed policy for example](https://docs.aws.amazon.com/AmazonECR/latest/userguide/ecr_managed_policies.html#AmazonEC2ContainerRegistryPowerUser).
 Then create and download access keys and save `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` [as 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.
 
@@ -251,7 +251,7 @@ jobs:
           password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
 ```
 
-If you need to log in to Amazon ECR registries associated with other accounts, you can use the `AWS_ECR_REGISTRY_IDS`
+If you need to log in to Amazon ECR registries associated with other accounts, you can use the `AWS_ACCOUNT_IDS`
 environment variable:
 
 ```yaml
@@ -273,7 +273,7 @@ jobs:
           username: ${{ secrets.AWS_ACCESS_KEY_ID }}
           password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
         env:
-          AWS_ECR_REGISTRY_IDS: "012345678910 023456789012"
+          AWS_ACCOUNT_IDS: 012345678910,023456789012
 ```
 
 > Only available with [AWS CLI version 1](https://docs.aws.amazon.com/cli/latest/reference/ecr/get-login.html)
@@ -310,7 +310,7 @@ jobs:
 
 ### AWS Public Elastic Container Registry (ECR)
 
-Use an IAM user with the [ability to push to ECR](https://docs.aws.amazon.com/AmazonECR/latest/userguide/ecr_managed_policies.html).
+Use an IAM user with the ability to [push to ECR Public with `AmazonElasticContainerRegistryPublicPowerUser` managed policy for example](https://docs.aws.amazon.com/AmazonECR/latest/public/public-ecr-managed-policies.html#AmazonElasticContainerRegistryPublicPowerUser).
 Then create and download access keys and save `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` [as 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.
 
diff --git a/__tests__/aws.test.ts b/__tests__/aws.test.ts
index 07bf272..963ed27 100644
--- a/__tests__/aws.test.ts
+++ b/__tests__/aws.test.ts
@@ -6,6 +6,8 @@ describe('isECR', () => {
     ['registry.gitlab.com', false],
     ['gcr.io', false],
     ['012345678901.dkr.ecr.eu-west-3.amazonaws.com', true],
+    ['876820548815.dkr.ecr.cn-north-1.amazonaws.com.cn', true],
+    ['390948362332.dkr.ecr.cn-northwest-1.amazonaws.com.cn', true],
     ['public.ecr.aws', true]
   ])('given registry %p', async (registry, expected) => {
     expect(await aws.isECR(registry)).toEqual(expected);
@@ -17,6 +19,8 @@ describe('isPubECR', () => {
     ['registry.gitlab.com', false],
     ['gcr.io', false],
     ['012345678901.dkr.ecr.eu-west-3.amazonaws.com', false],
+    ['876820548815.dkr.ecr.cn-north-1.amazonaws.com.cn', false],
+    ['390948362332.dkr.ecr.cn-northwest-1.amazonaws.com.cn', false],
     ['public.ecr.aws', true]
   ])('given registry %p', async (registry, expected) => {
     expect(await aws.isPubECR(registry)).toEqual(expected);
@@ -59,8 +63,37 @@ describe('parseCLIVersion', () => {
 describe('getRegion', () => {
   test.each([
     ['012345678901.dkr.ecr.eu-west-3.amazonaws.com', 'eu-west-3'],
+    ['876820548815.dkr.ecr.cn-north-1.amazonaws.com.cn', 'cn-north-1'],
+    ['390948362332.dkr.ecr.cn-northwest-1.amazonaws.com.cn', 'cn-northwest-1'],
     ['public.ecr.aws', 'us-east-1']
   ])('given registry %p', async (registry, expected) => {
     expect(await aws.getRegion(registry)).toEqual(expected);
   });
 });
+
+describe('getAccountIDs', () => {
+  test.each([
+    ['012345678901.dkr.ecr.eu-west-3.amazonaws.com', undefined, ['012345678901']],
+    [
+      '012345678901.dkr.ecr.eu-west-3.amazonaws.com',
+      '012345678910,023456789012',
+      ['012345678901', '012345678910', '023456789012']
+    ],
+    [
+      '012345678901.dkr.ecr.eu-west-3.amazonaws.com',
+      '012345678901,012345678910,023456789012',
+      ['012345678901', '012345678910', '023456789012']
+    ],
+    [
+      '390948362332.dkr.ecr.cn-northwest-1.amazonaws.com.cn',
+      '012345678910,023456789012',
+      ['390948362332', '012345678910', '023456789012']
+    ],
+    ['public.ecr.aws', undefined, []]
+  ])('given registry %p', async (registry, accountIDsEnv, expected) => {
+    if (accountIDsEnv) {
+      process.env.AWS_ACCOUNT_IDS = accountIDsEnv;
+    }
+    expect(await aws.getAccountIDs(registry)).toEqual(expected);
+  });
+});
diff --git a/dist/index.js b/dist/index.js
index a8c8bd0..0379ce5 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -3088,6 +3088,7 @@ function loginECR(registry, username, password) {
         const cliPath = yield aws.getCLI();
         const cliVersion = yield aws.getCLIVersion();
         const region = yield aws.getRegion(registry);
+        const accountIDs = yield aws.getAccountIDs(registry);
         if (yield aws.isPubECR(registry)) {
             core.info(`💡 AWS Public ECR detected with ${region} region`);
         }
@@ -3097,7 +3098,7 @@ function loginECR(registry, username, password) {
         process.env.AWS_ACCESS_KEY_ID = username || process.env.AWS_ACCESS_KEY_ID;
         process.env.AWS_SECRET_ACCESS_KEY = password || process.env.AWS_SECRET_ACCESS_KEY;
         core.info(`⬇️ Retrieving docker login command through AWS CLI ${cliVersion} (${cliPath})...`);
-        const loginCmds = yield aws.getDockerLoginCmds(cliVersion, registry, region);
+        const loginCmds = yield aws.getDockerLoginCmds(cliVersion, registry, region, accountIDs);
         core.info(`🔑 Logging into ${registry}...`);
         loginCmds.forEach((loginCmd, index) => {
             execm.exec(loginCmd, [], true).then(res => {
@@ -4167,22 +4168,41 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
     });
 };
 Object.defineProperty(exports, "__esModule", { value: true });
-exports.getDockerLoginCmds = exports.parseCLIVersion = exports.getCLIVersion = exports.execCLI = exports.getCLI = exports.getRegion = exports.isPubECR = exports.isECR = void 0;
+exports.getDockerLoginCmds = exports.parseCLIVersion = exports.getCLIVersion = exports.execCLI = exports.getCLI = exports.getAccountIDs = exports.getRegion = exports.isPubECR = exports.isECR = void 0;
 const semver = __importStar(__webpack_require__(383));
 const io = __importStar(__webpack_require__(436));
 const execm = __importStar(__webpack_require__(757));
-exports.isECR = (registry) => __awaiter(void 0, void 0, void 0, function* () {
-    return registry.includes('amazonaws') || (yield exports.isPubECR(registry));
-});
-exports.isPubECR = (registry) => __awaiter(void 0, void 0, void 0, function* () {
+const ecrRegistryRegex = /^(([0-9]{12})\.dkr\.ecr\.(.+)\.amazonaws\.com(.cn)?)(\/([^:]+)(:.+)?)?$/;
+exports.isECR = (registry) => {
+    return ecrRegistryRegex.test(registry) || exports.isPubECR(registry);
+};
+exports.isPubECR = (registry) => {
     return registry === 'public.ecr.aws';
-});
-exports.getRegion = (registry) => __awaiter(void 0, void 0, void 0, function* () {
-    if (yield exports.isPubECR(registry)) {
+};
+exports.getRegion = (registry) => {
+    if (exports.isPubECR(registry)) {
         return process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || 'us-east-1';
     }
-    return registry.substring(registry.indexOf('ecr.') + 4, registry.indexOf('.amazonaws'));
-});
+    const matches = registry.match(ecrRegistryRegex);
+    if (!matches) {
+        return '';
+    }
+    return matches[3];
+};
+exports.getAccountIDs = (registry) => {
+    if (exports.isPubECR(registry)) {
+        return [];
+    }
+    const matches = registry.match(ecrRegistryRegex);
+    if (!matches) {
+        return [];
+    }
+    let accountIDs = [matches[2]];
+    if (process.env.AWS_ACCOUNT_IDS) {
+        accountIDs.push(...process.env.AWS_ACCOUNT_IDS.split(','));
+    }
+    return accountIDs.filter((item, index) => accountIDs.indexOf(item) === index);
+};
 exports.getCLI = () => __awaiter(void 0, void 0, void 0, function* () {
     return io.which('aws', true);
 });
@@ -4209,7 +4229,7 @@ exports.parseCLIVersion = (stdout) => __awaiter(void 0, void 0, void 0, function
     }
     return semver.clean(matches[1]);
 });
-exports.getDockerLoginCmds = (cliVersion, registry, region) => __awaiter(void 0, void 0, void 0, function* () {
+exports.getDockerLoginCmds = (cliVersion, registry, region, accountIDs) => __awaiter(void 0, void 0, void 0, function* () {
     let ecrCmd = (yield exports.isPubECR(registry)) ? 'ecr-public' : 'ecr';
     if (semver.satisfies(cliVersion, '>=2.0.0')) {
         return exports.execCLI([ecrCmd, 'get-login-password', '--region', region]).then(pwd => {
@@ -4217,11 +4237,15 @@ exports.getDockerLoginCmds = (cliVersion, registry, region) => __awaiter(void 0,
         });
     }
     else {
-        let args = [ecrCmd, 'get-login', '--region', region, '--no-include-email'];
-        if (process.env.AWS_ECR_REGISTRY_IDS) {
-            args.push('--registry-ids', process.env.AWS_ECR_REGISTRY_IDS);
-        }
-        return exports.execCLI(args).then(dockerLoginCmds => {
+        return exports.execCLI([
+            ecrCmd,
+            'get-login',
+            '--region',
+            region,
+            '--registry-ids',
+            accountIDs.join(' '),
+            '--no-include-email'
+        ]).then(dockerLoginCmds => {
             return dockerLoginCmds.trim().split(`\n`);
         });
     }
diff --git a/src/aws.ts b/src/aws.ts
index 22c1d89..ceea150 100644
--- a/src/aws.ts
+++ b/src/aws.ts
@@ -2,19 +2,40 @@ import * as semver from 'semver';
 import * as io from '@actions/io';
 import * as execm from './exec';
 
-export const isECR = async (registry: string): Promise<boolean> => {
-  return registry.includes('amazonaws') || (await isPubECR(registry));
+const ecrRegistryRegex = /^(([0-9]{12})\.dkr\.ecr\.(.+)\.amazonaws\.com(.cn)?)(\/([^:]+)(:.+)?)?$/;
+
+export const isECR = (registry: string): boolean => {
+  return ecrRegistryRegex.test(registry) || isPubECR(registry);
 };
 
-export const isPubECR = async (registry: string): Promise<boolean> => {
+export const isPubECR = (registry: string): boolean => {
   return registry === 'public.ecr.aws';
 };
 
-export const getRegion = async (registry: string): Promise<string> => {
-  if (await isPubECR(registry)) {
+export const getRegion = (registry: string): string => {
+  if (isPubECR(registry)) {
     return process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || 'us-east-1';
   }
-  return registry.substring(registry.indexOf('ecr.') + 4, registry.indexOf('.amazonaws'));
+  const matches = registry.match(ecrRegistryRegex);
+  if (!matches) {
+    return '';
+  }
+  return matches[3];
+};
+
+export const getAccountIDs = (registry: string): string[] => {
+  if (isPubECR(registry)) {
+    return [];
+  }
+  const matches = registry.match(ecrRegistryRegex);
+  if (!matches) {
+    return [];
+  }
+  let accountIDs: Array<string> = [matches[2]];
+  if (process.env.AWS_ACCOUNT_IDS) {
+    accountIDs.push(...process.env.AWS_ACCOUNT_IDS.split(','));
+  }
+  return accountIDs.filter((item, index) => accountIDs.indexOf(item) === index);
 };
 
 export const getCLI = async (): Promise<string> => {
@@ -45,18 +66,27 @@ export const parseCLIVersion = async (stdout: string): Promise<string> => {
   return semver.clean(matches[1]);
 };
 
-export const getDockerLoginCmds = async (cliVersion: string, registry: string, region: string): Promise<string[]> => {
+export const getDockerLoginCmds = async (
+  cliVersion: string,
+  registry: string,
+  region: string,
+  accountIDs: string[]
+): Promise<string[]> => {
   let ecrCmd = (await isPubECR(registry)) ? 'ecr-public' : 'ecr';
   if (semver.satisfies(cliVersion, '>=2.0.0')) {
     return execCLI([ecrCmd, 'get-login-password', '--region', region]).then(pwd => {
       return [`docker login --username AWS --password ${pwd} ${registry}`];
     });
   } else {
-    let args: Array<string> = [ecrCmd, 'get-login', '--region', region, '--no-include-email'];
-    if (process.env.AWS_ECR_REGISTRY_IDS) {
-      args.push('--registry-ids', process.env.AWS_ECR_REGISTRY_IDS);
-    }
-    return execCLI(args).then(dockerLoginCmds => {
+    return execCLI([
+      ecrCmd,
+      'get-login',
+      '--region',
+      region,
+      '--registry-ids',
+      accountIDs.join(' '),
+      '--no-include-email'
+    ]).then(dockerLoginCmds => {
       return dockerLoginCmds.trim().split(`\n`);
     });
   }
diff --git a/src/docker.ts b/src/docker.ts
index e2c005d..19aeb63 100644
--- a/src/docker.ts
+++ b/src/docker.ts
@@ -44,6 +44,7 @@ export async function loginECR(registry: string, username: string, password: str
   const cliPath = await aws.getCLI();
   const cliVersion = await aws.getCLIVersion();
   const region = await aws.getRegion(registry);
+  const accountIDs = await aws.getAccountIDs(registry);
 
   if (await aws.isPubECR(registry)) {
     core.info(`💡 AWS Public ECR detected with ${region} region`);
@@ -55,7 +56,7 @@ export async function loginECR(registry: string, username: string, password: str
   process.env.AWS_SECRET_ACCESS_KEY = password || process.env.AWS_SECRET_ACCESS_KEY;
 
   core.info(`⬇️ Retrieving docker login command through AWS CLI ${cliVersion} (${cliPath})...`);
-  const loginCmds = await aws.getDockerLoginCmds(cliVersion, registry, region);
+  const loginCmds = await aws.getDockerLoginCmds(cliVersion, registry, region, accountIDs);
 
   core.info(`🔑 Logging into ${registry}...`);
   loginCmds.forEach((loginCmd, index) => {