Skip to content

Commit

Permalink
Merge pull request #37 from crazy-max/public-ecr
Browse files Browse the repository at this point in the history
Add support for public ECR
  • Loading branch information
Chad Metcalf authored Dec 11, 2020
2 parents 7c9afe2 + 1e75de0 commit 3b14bab
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
name: Checkout
uses: actions/checkout@v2
-
name: Login to DockerHub
name: Login to Docker Hub
uses: ./
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
Expand Down
69 changes: 65 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ GitHub Action to login against a Docker registry.
___

* [Usage](#usage)
* [DockerHub](#dockerhub)
* [Docker Hub](#docker-hub)
* [GitHub Packages Docker Registry](#github-packages-docker-registry)
* [GitHub Container Registry](#github-container-registry)
* [GitLab](#gitlab)
* [Azure Container Registry (ACR)](#azure-container-registry-acr)
* [Google Container Registry (GCR)](#google-container-registry-gcr)
* [Google Artifact Registry (GAR)](#google-artifact-registry-gar)
* [AWS Elastic Container Registry (ECR)](#aws-elastic-container-registry-ecr)
* [AWS Public Elastic Container Registry (ECR)](#aws-public-elastic-container-registry-ecr)
* [OCI Oracle Cloud Infrastructure Registry (OCIR)](#oci-oracle-cloud-infrastructure-registry-ocir)
* [Customizing](#customizing)
* [inputs](#inputs)
Expand All @@ -34,9 +35,9 @@ ___

## Usage

### DockerHub
### Docker Hub

To authenticate against [DockerHub](https://hub.docker.com) it's strongly recommended to create a
To authenticate against [Docker Hub](https://hub.docker.com) it's strongly recommended to create a
[personal access token](https://docs.docker.com/docker-hub/access-tokens/) as an alternative to your password.

```yaml
Expand All @@ -51,7 +52,7 @@ jobs:
runs-on: ubuntu-latest
steps:
-
name: Login to DockerHub
name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
Expand Down Expand Up @@ -280,6 +281,66 @@ jobs:

> Replace `<aws-account-number>` and `<region>` with their respective values.

### 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).
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.

```yaml
name: ci
on:
push:
branches: master
jobs:
login:
runs-on: ubuntu-latest
steps:
-
name: Login to Public ECR
uses: docker/login-action@v1
with:
registry: public.ecr.aws
username: ${{ secrets.AWS_ACCESS_KEY_ID }}
password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
env:
AWS_REGION: <region>
```

> 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>`).
Expand Down
26 changes: 19 additions & 7 deletions __tests__/aws.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ describe('isECR', () => {
test.each([
['registry.gitlab.com', false],
['gcr.io', false],
['012345678901.dkr.ecr.eu-west-3.amazonaws.com', true]
['012345678901.dkr.ecr.eu-west-3.amazonaws.com', true],
['public.ecr.aws', true]
])('given registry %p', async (registry, expected) => {
expect(await aws.isECR(registry)).toEqual(expected);
});
});

describe('isPubECR', () => {
test.each([
['registry.gitlab.com', false],
['gcr.io', false],
['012345678901.dkr.ecr.eu-west-3.amazonaws.com', false],
['public.ecr.aws', true]
])('given registry %p', async (registry, expected) => {
expect(await aws.isPubECR(registry)).toEqual(expected);
});
});

describe('getCLI', () => {
it('exists', async () => {
const awsPath = await aws.getCLI();
Expand Down Expand Up @@ -45,10 +57,10 @@ describe('parseCLIVersion', () => {
});

describe('getRegion', () => {
test.each([['012345678901.dkr.ecr.eu-west-3.amazonaws.com', 'eu-west-3']])(
'given registry %p',
async (registry, expected) => {
expect(await aws.getRegion(registry)).toEqual(expected);
}
);
test.each([
['012345678901.dkr.ecr.eu-west-3.amazonaws.com', 'eu-west-3'],
['public.ecr.aws', 'us-east-1']
])('given registry %p', async (registry, expected) => {
expect(await aws.getRegion(registry)).toEqual(expected);
});
});
24 changes: 18 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions src/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import * as io from '@actions/io';
import * as execm from './exec';

export const isECR = async (registry: string): Promise<boolean> => {
return registry.includes('amazonaws');
return registry.includes('amazonaws') || (await isPubECR(registry));
};

export const isPubECR = async (registry: string): Promise<boolean> => {
return registry === 'public.ecr.aws';
};

export const getRegion = async (registry: string): Promise<string> => {
if (await 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'));
};

Expand Down Expand Up @@ -39,12 +46,13 @@ export const parseCLIVersion = async (stdout: string): Promise<string> => {
};

export const getDockerLoginCmd = 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(['ecr', 'get-login-password', '--region', region]).then(pwd => {
return execCLI([ecrCmd, 'get-login-password', '--region', region]).then(pwd => {
return `docker login --username AWS --password ${pwd} ${registry}`;
});
} else {
return execCLI(['ecr', 'get-login', '--region', region, '--no-include-email']).then(dockerLoginCmd => {
return execCLI([ecrCmd, 'get-login', '--region', region, '--no-include-email']).then(dockerLoginCmd => {
return dockerLoginCmd;
});
}
Expand Down
9 changes: 7 additions & 2 deletions src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function loginStandard(registry: string, username: string, password
if (registry) {
core.info(`πŸ”‘ Logging into ${registry}...`);
} else {
core.info(`πŸ”‘ Logging into DockerHub...`);
core.info(`πŸ”‘ Logging into Docker Hub...`);
}
await execm.exec('docker', loginArgs, true, password).then(res => {
if (res.stderr != '' && !res.success) {
Expand All @@ -44,7 +44,12 @@ 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);
core.info(`πŸ’‘ AWS ECR detected with ${region} region`);

if (await aws.isPubECR(registry)) {
core.info(`πŸ’‘ AWS Public ECR detected with ${region} region`);
} else {
core.info(`πŸ’‘ AWS ECR detected with ${region} region`);
}

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;
Expand Down

0 comments on commit 3b14bab

Please sign in to comment.