mirror of
https://github.com/docker/login-action.git
synced 2025-08-14 20:38:29 +08:00
Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
a5ba64f65e | ||
|
64702f4db1 | ||
|
e56233ce43 | ||
|
1bd3567034 | ||
|
52b67bd7c8 | ||
|
d833f7c2ad | ||
|
16d491f0ca | ||
|
04f461cc60 | ||
|
25aa6aa30c | ||
|
1a211c6f27 | ||
|
12991b4d6c | ||
|
34e505eb5e | ||
|
2c57607524 | ||
|
26618cd0df | ||
|
da3da99964 | ||
|
b7cd11b1fa | ||
|
16b2f90c24 | ||
|
826c451920 | ||
|
f37c715508 | ||
|
e6dc03b339 |
42
.github/workflows/ci.yml
vendored
42
.github/workflows/ci.yml
vendored
@@ -8,17 +8,21 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
dockerhub:
|
dockerhub:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ${{ matrix.os }}
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
|
os:
|
||||||
|
- ubuntu-20.04
|
||||||
|
- ubuntu-18.04
|
||||||
|
- ubuntu-16.04
|
||||||
logout:
|
logout:
|
||||||
- true
|
- true
|
||||||
- false
|
- false
|
||||||
steps:
|
steps:
|
||||||
-
|
-
|
||||||
name: Checkout
|
name: Checkout
|
||||||
uses: actions/checkout@v2.3.1
|
uses: actions/checkout@v2.3.2
|
||||||
-
|
-
|
||||||
name: Login to DockerHub
|
name: Login to DockerHub
|
||||||
uses: ./
|
uses: ./
|
||||||
@@ -43,7 +47,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
-
|
-
|
||||||
name: Checkout
|
name: Checkout
|
||||||
uses: actions/checkout@v2.3.1
|
uses: actions/checkout@v2.3.2
|
||||||
-
|
-
|
||||||
name: Login to GitHub Package Registry
|
name: Login to GitHub Package Registry
|
||||||
uses: ./
|
uses: ./
|
||||||
@@ -69,7 +73,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
-
|
-
|
||||||
name: Checkout
|
name: Checkout
|
||||||
uses: actions/checkout@v2.3.1
|
uses: actions/checkout@v2.3.2
|
||||||
-
|
-
|
||||||
name: Login to GitLab
|
name: Login to GitLab
|
||||||
uses: ./
|
uses: ./
|
||||||
@@ -83,3 +87,33 @@ jobs:
|
|||||||
if: always()
|
if: always()
|
||||||
run: |
|
run: |
|
||||||
rm -f ${HOME}/.docker/config.json
|
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
|
||||||
|
2
.github/workflows/labels.yml
vendored
2
.github/workflows/labels.yml
vendored
@@ -14,7 +14,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
-
|
-
|
||||||
name: Checkout
|
name: Checkout
|
||||||
uses: actions/checkout@v2.3.1
|
uses: actions/checkout@v2.3.2
|
||||||
-
|
-
|
||||||
name: Run Labeler
|
name: Run Labeler
|
||||||
if: success()
|
if: success()
|
||||||
|
2
.github/workflows/pre-checkin.yml
vendored
2
.github/workflows/pre-checkin.yml
vendored
@@ -14,7 +14,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
-
|
-
|
||||||
name: Checkout
|
name: Checkout
|
||||||
uses: actions/checkout@v2.3.1
|
uses: actions/checkout@v2.3.2
|
||||||
-
|
-
|
||||||
name: Install
|
name: Install
|
||||||
run: yarn install
|
run: yarn install
|
||||||
|
40
.github/workflows/test.yml
vendored
Normal file
40
.github/workflows/test.yml
vendored
Normal 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
|
13
CHANGELOG.md
13
CHANGELOG.md
@@ -1,5 +1,18 @@
|
|||||||
# Changelog
|
# 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)
|
## 1.1.1 (2020/08/16)
|
||||||
|
|
||||||
* Typo
|
* Typo
|
||||||
|
120
README.md
120
README.md
@@ -1,6 +1,9 @@
|
|||||||
[](https://github.com/crazy-max/ghaction-docker-login/releases/latest)
|
[](https://github.com/crazy-max/ghaction-docker-login/releases/latest)
|
||||||
[](https://github.com/marketplace/actions/docker-login)
|
[](https://github.com/marketplace/actions/docker-login)
|
||||||
[](https://github.com/crazy-max/ghaction-docker-login/actions?workflow=ci)
|
[](https://github.com/crazy-max/ghaction-docker-login/actions?workflow=ci)
|
||||||
|
[](https://github.com/crazy-max/ghaction-docker-login/actions?workflow=test)
|
||||||
|
[](https://codecov.io/gh/crazy-max/ghaction-docker-login)
|
||||||
|
|
||||||
[](https://github.com/sponsors/crazy-max)
|
[](https://github.com/sponsors/crazy-max)
|
||||||
[](https://www.paypal.me/crazyws)
|
[](https://www.paypal.me/crazyws)
|
||||||
|
|
||||||
@@ -18,8 +21,12 @@ ___
|
|||||||
* [DockerHub](#dockerhub)
|
* [DockerHub](#dockerhub)
|
||||||
* [GitHub Package Registry](#github-package-registry)
|
* [GitHub Package Registry](#github-package-registry)
|
||||||
* [GitLab](#gitlab)
|
* [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)
|
* [Customizing](#customizing)
|
||||||
* [inputs](#inputs)
|
* [inputs](#inputs)
|
||||||
|
* [Keep up-to-date with GitHub Dependabot](#keep-up-to-date-with-github-dependabot)
|
||||||
* [Limitation](#limitation)
|
* [Limitation](#limitation)
|
||||||
* [How can I help?](#how-can-i-help)
|
* [How can I help?](#how-can-i-help)
|
||||||
* [License](#license)
|
* [License](#license)
|
||||||
@@ -34,7 +41,6 @@ name: ci
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: master
|
branches: master
|
||||||
tags:
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
login:
|
login:
|
||||||
@@ -59,7 +65,6 @@ name: ci
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: master
|
branches: master
|
||||||
tags:
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
login:
|
login:
|
||||||
@@ -85,7 +90,6 @@ name: ci
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: master
|
branches: master
|
||||||
tags:
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
login:
|
login:
|
||||||
@@ -103,6 +107,98 @@ jobs:
|
|||||||
password: ${{ secrets.GITLAB_PASSWORD }}
|
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
|
## Customizing
|
||||||
|
|
||||||
### inputs
|
### 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 |
|
| `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 |
|
| `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
|
## 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).
|
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
54
__tests__/aws.test.ts
Normal 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
3583
dist/index.js
generated
vendored
File diff suppressed because it is too large
Load Diff
12
jest.config.js
Normal file
12
jest.config.js
Normal 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
|
||||||
|
}
|
12
package.json
12
package.json
@@ -6,6 +6,7 @@
|
|||||||
"build": "tsc && ncc build",
|
"build": "tsc && ncc build",
|
||||||
"format": "prettier --write **/*.ts",
|
"format": "prettier --write **/*.ts",
|
||||||
"format-check": "prettier --check **/*.ts",
|
"format-check": "prettier --check **/*.ts",
|
||||||
|
"test": "jest --coverage",
|
||||||
"pre-checkin": "yarn run format && yarn run build"
|
"pre-checkin": "yarn run format && yarn run build"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
@@ -21,13 +22,20 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/core": "^1.2.4",
|
"@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": {
|
"devDependencies": {
|
||||||
"@types/jest": "^26.0.3",
|
"@types/jest": "^26.0.3",
|
||||||
"@types/node": "^14.0.14",
|
"@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",
|
"prettier": "^2.0.5",
|
||||||
|
"ts-jest": "^26.1.1",
|
||||||
"typescript": "^3.9.5",
|
"typescript": "^3.9.5",
|
||||||
"typescript-formatter": "^7.2.2"
|
"typescript-formatter": "^7.2.2"
|
||||||
}
|
}
|
||||||
|
51
src/aws.ts
Normal file
51
src/aws.ts
Normal 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
17
src/context.ts
Normal 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
60
src/docker.ts
Normal 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!');
|
||||||
|
});
|
||||||
|
}
|
32
src/main.ts
32
src/main.ts
@@ -1,6 +1,7 @@
|
|||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import * as core from '@actions/core';
|
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';
|
import * as stateHelper from './state-helper';
|
||||||
|
|
||||||
async function run(): Promise<void> {
|
async function run(): Promise<void> {
|
||||||
@@ -10,25 +11,10 @@ async function run(): Promise<void> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const registry: string = core.getInput('registry');
|
let inputs: Inputs = await getInputs();
|
||||||
stateHelper.setRegistry(registry);
|
stateHelper.setRegistry(inputs.registry);
|
||||||
stateHelper.setLogout(core.getInput('logout'));
|
stateHelper.setLogout(inputs.logout);
|
||||||
|
await docker.login(inputs.registry, inputs.username, inputs.password);
|
||||||
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!');
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.setFailed(error.message);
|
core.setFailed(error.message);
|
||||||
}
|
}
|
||||||
@@ -38,11 +24,7 @@ async function logout(): Promise<void> {
|
|||||||
if (!stateHelper.logout) {
|
if (!stateHelper.logout) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await exec.exec('docker', ['logout', stateHelper.registry], false).then(res => {
|
await docker.logout(stateHelper.registry);
|
||||||
if (res.stderr != '' && !res.success) {
|
|
||||||
core.warning(res.stderr);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stateHelper.IsPost) {
|
if (!stateHelper.IsPost) {
|
||||||
|
Reference in New Issue
Block a user