Compare commits

...

21 Commits

Author SHA1 Message Date
CrazyMax
a5ba64f65e Fix tests 2020-08-21 16:33:18 +02:00
CrazyMax
64702f4db1 Update CHANGELOG 2020-08-21 16:31:25 +02:00
CrazyMax
e56233ce43 Add example for Azure Container Registry (ACR) 2020-08-21 16:29:54 +02:00
CrazyMax
1bd3567034 Fix workflow 2020-08-21 15:29:42 +02:00
CrazyMax
52b67bd7c8 Update CI workflow 2020-08-21 15:28:57 +02:00
CrazyMax
d833f7c2ad Handle AWS CLI v2 2020-08-21 15:27:22 +02:00
CrazyMax
16d491f0ca Fix cmd output 2020-08-21 15:07:43 +02:00
CrazyMax
04f461cc60 Fix tests 2020-08-21 14:56:11 +02:00
CrazyMax
25aa6aa30c Check AWS CLI version
Add tests
2020-08-21 14:45:16 +02:00
CrazyMax
1a211c6f27 Move zeit/ncc to vercel/ncc 2020-08-21 13:33:37 +02:00
CrazyMax
12991b4d6c Add note about dependabot 2020-08-20 17:31:36 +02:00
CrazyMax
34e505eb5e Update CHANGELOG 2020-08-20 16:45:26 +02:00
CrazyMax
2c57607524 Refactor 2020-08-20 16:40:33 +02:00
CrazyMax
26618cd0df Retrieve command 2020-08-20 16:24:35 +02:00
CrazyMax
da3da99964 Builtin exec 2020-08-20 16:14:02 +02:00
CrazyMax
b7cd11b1fa Unsilent 2020-08-20 16:12:46 +02:00
CrazyMax
16b2f90c24 Display AWS CLI version 2020-08-20 16:10:17 +02:00
CrazyMax
826c451920 Log AWS region 2020-08-20 16:03:38 +02:00
CrazyMax
f37c715508 Add support for AWS Elastic Container Registry (ECR)
Add example for Google Container Registry (GCR)
2020-08-20 15:59:41 +02:00
dependabot[bot]
e6dc03b339 Bump actions/checkout from v2.3.1 to v2.3.2 (#1)
Bumps [actions/checkout](https://github.com/actions/checkout) from v2.3.1 to v2.3.2.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v2.3.1...2036a08e25fa78bbd946711a407b529a0a1204bf)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2020-08-17 09:34:12 +00:00
CrazyMax
ef43051dd2 Typo 2020-08-16 02:04:19 +02:00
15 changed files with 6948 additions and 653 deletions

View File

@@ -8,17 +8,21 @@ on:
jobs:
dockerhub:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-20.04
- ubuntu-18.04
- ubuntu-16.04
logout:
- true
- false
steps:
-
name: Checkout
uses: actions/checkout@v2.3.1
uses: actions/checkout@v2.3.2
-
name: Login to DockerHub
uses: ./
@@ -43,7 +47,7 @@ jobs:
steps:
-
name: Checkout
uses: actions/checkout@v2.3.1
uses: actions/checkout@v2.3.2
-
name: Login to GitHub Package Registry
uses: ./
@@ -69,7 +73,7 @@ jobs:
steps:
-
name: Checkout
uses: actions/checkout@v2.3.1
uses: actions/checkout@v2.3.2
-
name: Login to GitLab
uses: ./
@@ -83,3 +87,33 @@ jobs:
if: always()
run: |
rm -f ${HOME}/.docker/config.json
ecr:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-20.04
- ubuntu-18.04
- ubuntu-16.04
logout:
- true
- false
steps:
-
name: Checkout
uses: actions/checkout@v2.3.1
-
name: Login to ECR
uses: ./
with:
registry: ${{ secrets.AWS_ACCOUNT_NUMBER }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com
username: ${{ secrets.AWS_ACCESS_KEY_ID }}
password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
logout: ${{ matrix.logout }}
-
name: Clear
if: always()
run: |
rm -f ${HOME}/.docker/config.json

View File

@@ -14,7 +14,7 @@ jobs:
steps:
-
name: Checkout
uses: actions/checkout@v2.3.1
uses: actions/checkout@v2.3.2
-
name: Run Labeler
if: success()

View File

@@ -14,7 +14,7 @@ jobs:
steps:
-
name: Checkout
uses: actions/checkout@v2.3.1
uses: actions/checkout@v2.3.2
-
name: Install
run: yarn install

40
.github/workflows/test.yml vendored Normal file
View File

@@ -0,0 +1,40 @@
name: test
on:
push:
branches:
- master
- releases/v*
paths-ignore:
- '**.md'
pull_request:
paths-ignore:
- '**.md'
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-20.04
- ubuntu-18.04
- ubuntu-16.04
steps:
-
name: Checkout
uses: actions/checkout@v2.3.2
-
name: Install
run: yarn install
-
name: Test
run: yarn run test
-
name: Upload coverage
uses: codecov/codecov-action@v1.0.13
if: success()
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage/clover.xml

View File

@@ -1,5 +1,22 @@
# Changelog
## 1.3.0 (2020/08/21)
* Handle AWS CLI v2
* Check AWS CLI version
* Add tests
* Add example for Azure Container Registry (ACR)
* Move zeit/ncc to vercel/ncc
## 1.2.0 (2020/08/20)
* Add support for AWS Elastic Container Registry (ECR)
* Add example for Google Container Registry (GCR)
## 1.1.1 (2020/08/16)
* Typo
## 1.1.0 (2020/08/15)
* Add tests and examples for GitLab and GitHub Package Registry

122
README.md
View File

@@ -1,6 +1,9 @@
[![GitHub release](https://img.shields.io/github/release/crazy-max/ghaction-docker-login.svg?style=flat-square)](https://github.com/crazy-max/ghaction-docker-login/releases/latest)
[![GitHub marketplace](https://img.shields.io/badge/marketplace-docker--login-blue?logo=github&style=flat-square)](https://github.com/marketplace/actions/docker-login)
[![CI workflow](https://img.shields.io/github/workflow/status/crazy-max/ghaction-docker-login/ci?label=ci&logo=github&style=flat-square)](https://github.com/crazy-max/ghaction-docker-login/actions?workflow=ci)
[![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)
@@ -18,8 +21,12 @@ ___
* [DockerHub](#dockerhub)
* [GitHub Package Registry](#github-package-registry)
* [GitLab](#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)
* [Limitation](#limitation)
* [How can I help?](#how-can-i-help)
* [License](#license)
@@ -34,7 +41,6 @@ name: ci
on:
push:
branches: master
tags:
jobs:
login:
@@ -59,7 +65,6 @@ name: ci
on:
push:
branches: master
tags:
jobs:
login:
@@ -69,7 +74,7 @@ jobs:
name: Checkout
uses: actions/checkout@v2
-
name: Login to DockerHub
name: Login to GitHub Package Registry
uses: crazy-max/ghaction-docker-login@v1
with:
registry: docker.pkg.github.com
@@ -85,7 +90,6 @@ name: ci
on:
push:
branches: master
tags:
jobs:
login:
@@ -103,6 +107,98 @@ 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).
Then create and download the JSON key for this service account and save content of `.json` file
[as a secret](https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets#creating-encrypted-secrets-for-a-repository)
called `GCR_JSON_KEY` in your GitHub repo. Ensure you set the username to `_json_key`.
```yaml
name: ci
on:
push:
branches: master
jobs:
login:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Login to GCR
uses: crazy-max/ghaction-docker-login@v1
with:
registry: gcr.io
username: _json_key
password: ${{ secrets.GCR_JSON_KEY }}
```
### 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).
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: Checkout
uses: actions/checkout@v2
-
name: Login to ECR
uses: crazy-max/ghaction-docker-login@v1
with:
registry: <aws-account-number>.dkr.ecr.<region>.amazonaws.com
username: ${{ secrets.AWS_ACCESS_KEY_ID }}
password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
```
> Replace `<aws-account-number>` and `<region>` with their respective values.
## Customizing
### inputs
@@ -116,6 +212,22 @@ Following inputs can be used as `step.with` keys
| `password` | String | | Password or personal access token used to log against the Docker registry |
| `logout` | Bool | `true` | Log out from the Docker registry at the end of a job |
## Keep up-to-date with GitHub Dependabot
Since [Dependabot](https://docs.github.com/en/github/administering-a-repository/keeping-your-actions-up-to-date-with-github-dependabot)
has [native GitHub Actions support](https://docs.github.com/en/github/administering-a-repository/configuration-options-for-dependency-updates#package-ecosystem),
to enable it on your GitHub repo all you need to do is add the `.github/dependabot.yml` file:
```yaml
version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
```
## Limitation
This action is only available for Linux [virtual environments](https://help.github.com/en/articles/virtual-environments-for-github-actions#supported-virtual-environments-and-hardware-resources).

54
__tests__/aws.test.ts Normal file
View File

@@ -0,0 +1,54 @@
import * as semver from 'semver';
import * as aws from '../src/aws';
describe('isECR', () => {
test.each([
['registry.gitlab.com', false],
['gcr.io', false],
['012345678901.dkr.ecr.eu-west-3.amazonaws.com', true]
])('given registry %p', async (registry, expected) => {
expect(await aws.isECR(registry)).toEqual(expected);
});
});
describe('getCLI', () => {
it('exists', async () => {
const awsPath = await aws.getCLI();
console.log(`awsPath: ${awsPath}`);
expect(awsPath).not.toEqual('');
});
});
describe('execCLI', () => {
it('--version not empty', async () => {
const cliCmdOutput = await aws.execCLI(['--version']);
console.log(`cliCmdOutput: ${cliCmdOutput}`);
expect(cliCmdOutput).not.toEqual('');
}, 100000);
});
describe('getCLIVersion', () => {
it('valid', async () => {
const cliVersion = await aws.getCLIVersion();
console.log(`cliVersion: ${cliVersion}`);
expect(semver.valid(cliVersion)).not.toBeNull();
}, 100000);
});
describe('parseCLIVersion', () => {
test.each([
['v1', 'aws-cli/1.18.120 Python/2.7.17 Linux/5.3.0-1034-azure botocore/1.17.43', '1.18.120'],
['v2', 'aws-cli/2.0.41 Python/3.7.3 Linux/4.19.104-microsoft-standard exe/x86_64.ubuntu.18', '2.0.41']
])('given aws %p', async (version, stdout, expected) => {
expect(await aws.parseCLIVersion(stdout)).toEqual(expected);
});
});
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);
}
);
});

3583
dist/index.js generated vendored

File diff suppressed because it is too large Load Diff

12
jest.config.js Normal file
View File

@@ -0,0 +1,12 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
setupFiles: ["dotenv/config"],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: false
}

View File

@@ -6,6 +6,7 @@
"build": "tsc && ncc build",
"format": "prettier --write **/*.ts",
"format-check": "prettier --check **/*.ts",
"test": "jest --coverage",
"pre-checkin": "yarn run format && yarn run build"
},
"repository": {
@@ -21,13 +22,20 @@
"license": "MIT",
"dependencies": {
"@actions/core": "^1.2.4",
"@actions/exec": "^1.0.4"
"@actions/exec": "^1.0.4",
"@actions/io": "^1.0.2",
"semver": "^7.3.2"
},
"devDependencies": {
"@types/jest": "^26.0.3",
"@types/node": "^14.0.14",
"@zeit/ncc": "^0.22.3",
"@vercel/ncc": "^0.23.0",
"dotenv": "^8.2.0",
"jest": "^26.1.0",
"jest-circus": "^26.1.0",
"jest-runtime": "^26.1.0",
"prettier": "^2.0.5",
"ts-jest": "^26.1.1",
"typescript": "^3.9.5",
"typescript-formatter": "^7.2.2"
}

51
src/aws.ts Normal file
View File

@@ -0,0 +1,51 @@
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');
};
export const getRegion = async (registry: string): Promise<string> => {
return registry.substring(registry.indexOf('ecr.') + 4, registry.indexOf('.amazonaws'));
};
export const getCLI = async (): Promise<string> => {
return io.which('aws', true);
};
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);
} else if (res.stderr != '') {
return res.stderr.trim();
} else {
return res.stdout.trim();
}
});
};
export const getCLIVersion = async (): Promise<string> => {
return parseCLIVersion(await execCLI(['--version']));
};
export const parseCLIVersion = async (stdout: string): Promise<string> => {
const matches = /aws-cli\/([0-9.]+)/.exec(stdout);
if (!matches) {
throw new Error(`Cannot parse AWS CLI version`);
}
return semver.clean(matches[1]);
};
export const getDockerLoginCmd = async (cliVersion: string, registry: string, region: string): Promise<string> => {
if (semver.satisfies(cliVersion, '>=2.0.0')) {
return execCLI(['ecr', '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 dockerLoginCmd;
});
}
};

17
src/context.ts Normal file
View File

@@ -0,0 +1,17 @@
import * as core from '@actions/core';
export interface Inputs {
registry: string;
username: string;
password: string;
logout: string;
}
export async function getInputs(): Promise<Inputs> {
return {
registry: core.getInput('registry'),
username: core.getInput('username'),
password: core.getInput('password', {required: true}),
logout: core.getInput('logout')
};
}

60
src/docker.ts Normal file
View File

@@ -0,0 +1,60 @@
import * as core from '@actions/core';
import * as aws from './aws';
import * as execm from './exec';
export async function login(registry: string, username: string, password: string): Promise<void> {
if (await aws.isECR(registry)) {
await loginECR(registry, username, password);
} else {
await loginStandard(registry, username, password);
}
}
export async function logout(registry: string): Promise<void> {
await execm.exec('docker', ['logout', registry], false).then(res => {
if (res.stderr != '' && !res.success) {
core.warning(res.stderr);
}
});
}
export async function loginStandard(registry: string, username: string, password: string): Promise<void> {
let loginArgs: Array<string> = ['login', '--password', password];
if (username) {
loginArgs.push('--username', username);
}
loginArgs.push(registry);
if (registry) {
core.info(`🔑 Logging into ${registry}...`);
} else {
core.info(`🔑 Logging into DockerHub...`);
}
await execm.exec('docker', loginArgs, true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
core.info('🎉 Login Succeeded!');
});
}
export async function loginECR(registry: string, username: string, password: string): Promise<void> {
const cliPath = await aws.getCLI();
const cliVersion = await aws.getCLIVersion();
const region = await aws.getRegion(registry);
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.getDockerLoginCmd(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!');
});
}

View File

@@ -1,6 +1,7 @@
import * as os from 'os';
import * as core from '@actions/core';
import * as exec from './exec';
import {getInputs, Inputs} from './context';
import * as docker from './docker';
import * as stateHelper from './state-helper';
async function run(): Promise<void> {
@@ -10,25 +11,10 @@ async function run(): Promise<void> {
return;
}
const registry: string = core.getInput('registry');
stateHelper.setRegistry(registry);
stateHelper.setLogout(core.getInput('logout'));
const username: string = core.getInput('username');
const password: string = core.getInput('password', {required: true});
let loginArgs: Array<string> = ['login', '--password', password];
if (username) {
loginArgs.push('--username', username);
}
loginArgs.push(registry);
await exec.exec('docker', loginArgs, true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
core.info('🎉 Login Succeeded!');
});
let inputs: Inputs = await getInputs();
stateHelper.setRegistry(inputs.registry);
stateHelper.setLogout(inputs.logout);
await docker.login(inputs.registry, inputs.username, inputs.password);
} catch (error) {
core.setFailed(error.message);
}
@@ -38,11 +24,7 @@ async function logout(): Promise<void> {
if (!stateHelper.logout) {
return;
}
await exec.exec('docker', ['logout', stateHelper.registry], false).then(res => {
if (res.stderr != '' && !res.success) {
core.warning(res.stderr);
}
});
await docker.logout(stateHelper.registry);
}
if (!stateHelper.IsPost) {

3555
yarn.lock

File diff suppressed because it is too large Load Diff