Skip to content

Commit

Permalink
Add example for Azure Container Registry (ACR)
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyMax committed Aug 21, 2020
1 parent 1bd3567 commit e56233c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 20 deletions.
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![CI workflow](https://img.shields.io/github/workflow/status/crazy-max/ghaction-docker-login/test?label=ci&logo=github&style=flat-square)](https://github.com/crazy-max/ghaction-docker-login/actions?workflow=ci)
[![Test workflow](https://img.shields.io/github/workflow/status/crazy-max/ghaction-docker-login/test?label=test&logo=github&style=flat-square)](https://github.com/crazy-max/ghaction-docker-login/actions?workflow=test)
[![Codecov](https://img.shields.io/codecov/c/github/crazy-max/ghaction-docker-login?logo=codecov&style=flat-square)](https://codecov.io/gh/crazy-max/ghaction-docker-login)

[![Become a sponsor](https://img.shields.io/badge/sponsor-crazy--max-181717.svg?logo=github&style=flat-square)](https://github.com/sponsors/crazy-max)
[![Paypal Donate](https://img.shields.io/badge/donate-paypal-00457c.svg?logo=paypal&style=flat-square)](https://www.paypal.me/crazyws)

Expand All @@ -20,8 +21,9 @@ ___
* [DockerHub](#dockerhub)
* [GitHub Package Registry](#github-package-registry)
* [GitLab](#gitlab)
* [Google Container Registry (GCR)](#gitlab)
* [AWS Elastic Container Registry (ECR)](#gitlab)
* [Azure Container Registry (ACR)](#azure-container-registry-acr)
* [Google Container Registry (GCR)](#google-container-registry-gcr)
* [AWS Elastic Container Registry (ECR)](#aws-elastic-container-registry-ecr)
* [Customizing](#customizing)
* [inputs](#inputs)
* [Keep up-to-date with GitHub Dependabot](#keep-up-to-date-with-github-dependabot)
Expand Down Expand Up @@ -105,6 +107,37 @@ jobs:
password: ${{ secrets.GITLAB_PASSWORD }}
```
### Azure Container Registry (ACR)
[Create a service principal](https://docs.microsoft.com/en-us/azure/container-registry/container-registry-auth-service-principal#create-a-service-principal)
with access to your container registry through the [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli)
and take note of the generated service principal's ID (also called _client ID_) and password (also called _client secret_).
```yaml
name: ci

on:
push:
branches: master

jobs:
login:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Login to ACR
uses: crazy-max/ghaction-docker-login@v1
with:
registry: <registry-name>.azurecr.io
username: ${{ secrets.AZURE_CLIENT_ID }}
password: ${{ secrets.AZURE_CLIENT_SECRET }}
```
> Replace `<registry-name>` with the name of your registry.

### Google Container Registry (GCR)

Use a service account with the ability to push to GCR and [configure access control](https://cloud.google.com/container-registry/docs/access-control).
Expand Down
4 changes: 2 additions & 2 deletions __tests__/aws.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ describe('getCLI', () => {
});
});

describe('getCLICmdOutput', () => {
describe('execCLI', () => {
it('--version not empty', async () => {
const cliCmdOutput = await aws.getCLICmdOutput(['--version']);
const cliCmdOutput = await aws.execCLI(['--version']);
console.log(`cliCmdOutput: ${cliCmdOutput}`);
expect(cliCmdOutput).not.toEqual('');
});
Expand Down
16 changes: 8 additions & 8 deletions dist/index.js

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

10 changes: 5 additions & 5 deletions src/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const getCLI = async (): Promise<string> => {
return io.which('aws', true);
};

export const getCLICmdOutput = async (args: string[]): Promise<string> => {
export const execCLI = async (args: string[]): Promise<string> => {
return execm.exec(await getCLI(), args, true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
Expand All @@ -27,7 +27,7 @@ export const getCLICmdOutput = async (args: string[]): Promise<string> => {
};

export const getCLIVersion = async (): Promise<string> => {
return parseCLIVersion(await getCLICmdOutput(['--version']));
return parseCLIVersion(await execCLI(['--version']));
};

export const parseCLIVersion = async (stdout: string): Promise<string> => {
Expand All @@ -38,13 +38,13 @@ export const parseCLIVersion = async (stdout: string): Promise<string> => {
return semver.clean(matches[1]);
};

export const getECRLoginCmd = async (cliVersion: string, registry: string, region: string): Promise<string> => {
export const getDockerLoginCmd = async (cliVersion: string, registry: string, region: string): Promise<string> => {
if (semver.satisfies(cliVersion, '>=2.0.0')) {
return getCLICmdOutput(['ecr', 'get-login-password', '--region', region]).then(pwd => {
return execCLI(['ecr', 'get-login-password', '--region', region]).then(pwd => {
return `docker login --username AWS --password ${pwd} ${registry}`;
});
} else {
return getCLICmdOutput(['ecr', 'get-login', '--region', region, '--no-include-email']).then(dockerLoginCmd => {
return execCLI(['ecr', 'get-login', '--region', region, '--no-include-email']).then(dockerLoginCmd => {
return dockerLoginCmd;
});
}
Expand Down
5 changes: 2 additions & 3 deletions src/docker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as exec from '@actions/exec';
import * as core from '@actions/core';
import * as aws from './aws';
import * as execm from './exec';
Expand Down Expand Up @@ -43,13 +42,13 @@ 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 registry detected with ${region} region`);
core.info(`💡 AWS ECR detected with ${region} region`);

process.env.AWS_ACCESS_KEY_ID = username;
process.env.AWS_SECRET_ACCESS_KEY = password;

core.info(`⬇️ Retrieving docker login command through AWS CLI ${cliVersion} (${cliPath})...`);
const loginCmd = await aws.getECRLoginCmd(cliVersion, registry, region);
const loginCmd = await aws.getDockerLoginCmd(cliVersion, registry, region);

core.info(`🔑 Logging into ${registry}...`);
execm.exec(loginCmd, [], true).then(res => {
Expand Down

0 comments on commit e56233c

Please sign in to comment.