mirror of
				https://github.com/docker/login-action.git
				synced 2025-10-31 18:20:10 +08:00 
			
		
		
		
	Check AWS CLI version
Add tests
This commit is contained in:
		
							parent
							
								
									1a211c6f27
								
							
						
					
					
						commit
						25aa6aa30c
					
				
							
								
								
									
										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 | ||||||
| @ -1,6 +1,7 @@ | |||||||
| [](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=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) | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										46
									
								
								__tests__/aws.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								__tests__/aws.test.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,46 @@ | |||||||
|  | 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('getCLIVersion', () => { | ||||||
|  |   it('valid', async () => { | ||||||
|  |     const cliVersion = await aws.getCLIVersion(); | ||||||
|  |     console.log(`cliVersion: ${cliVersion}`); | ||||||
|  |     expect(semver.valid(cliVersion)).toBe(true); | ||||||
|  |   }); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | 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); | ||||||
|  |     } | ||||||
|  |   ); | ||||||
|  | }); | ||||||
							
								
								
									
										2241
									
								
								dist/index.js
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										2241
									
								
								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 | ||||||
|  | } | ||||||
							
								
								
									
										10
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								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", | ||||||
|     "@vercel/ncc": "^0.23.0", |     "@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" | ||||||
|   } |   } | ||||||
|  | |||||||
							
								
								
									
										32
									
								
								src/aws.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/aws.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,32 @@ | |||||||
|  | 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 getCLI = async (): Promise<string> => { | ||||||
|  |   return io.which('aws', true); | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | export const getCLIVersion = async (): Promise<string | undefined> => { | ||||||
|  |   return execm.exec('aws', ['--version'], true).then(res => { | ||||||
|  |     if (res.stderr != '' && !res.success) { | ||||||
|  |       throw new Error(res.stderr); | ||||||
|  |     } | ||||||
|  |     return parseCLIVersion(res.stdout); | ||||||
|  |   }); | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | export const parseCLIVersion = async (stdout: string): Promise<string | undefined> => { | ||||||
|  |   const matches = /aws-cli\/([0-9.]+)/.exec(stdout); | ||||||
|  |   if (matches) { | ||||||
|  |     return semver.clean(matches[1]); | ||||||
|  |   } | ||||||
|  |   return undefined; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | export const getRegion = async (registry: string): Promise<string> => { | ||||||
|  |   return registry.substring(registry.indexOf('ecr.') + 4, registry.indexOf('.amazonaws')); | ||||||
|  | }; | ||||||
| @ -1,10 +1,10 @@ | |||||||
| import * as exec from '@actions/exec'; | import * as exec from '@actions/exec'; | ||||||
| import * as core from '@actions/core'; | import * as core from '@actions/core'; | ||||||
| import * as ecr from './ecr'; | import * as aws from './aws'; | ||||||
| import * as execm from './exec'; | import * as execm from './exec'; | ||||||
| 
 | 
 | ||||||
| export async function login(registry: string, username: string, password: string): Promise<void> { | export async function login(registry: string, username: string, password: string): Promise<void> { | ||||||
|   if (await ecr.isECR(registry)) { |   if (await aws.isECR(registry)) { | ||||||
|     await loginECR(registry, username, password); |     await loginECR(registry, username, password); | ||||||
|   } else { |   } else { | ||||||
|     await loginStandard(registry, username, password); |     await loginStandard(registry, username, password); | ||||||
| @ -40,13 +40,15 @@ export async function loginStandard(registry: string, username: string, password | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function loginECR(registry: string, username: string, password: string): Promise<void> { | export async function loginECR(registry: string, username: string, password: string): Promise<void> { | ||||||
|   await exec.exec('aws', ['--version']); |   const cliPath = await aws.getCLI(); | ||||||
|   const ecrRegion = await ecr.getRegion(registry); |   const cliVersion = await aws.getCLIVersion(); | ||||||
|  |   const ecrRegion = await aws.getRegion(registry); | ||||||
|  |   core.info(`💡 AWS ECR registry detected with ${ecrRegion} region`); | ||||||
|  | 
 | ||||||
|   process.env.AWS_ACCESS_KEY_ID = username; |   process.env.AWS_ACCESS_KEY_ID = username; | ||||||
|   process.env.AWS_SECRET_ACCESS_KEY = password; |   process.env.AWS_SECRET_ACCESS_KEY = password; | ||||||
| 
 |   core.info(`⬇️ Retrieving docker login command through AWS CLI ${cliVersion}...`); | ||||||
|   core.info(`⬇️ Retrieving docker login command for ECR region ${ecrRegion}...`); |   await execm.exec(cliPath, ['ecr', 'get-login', '--region', ecrRegion, '--no-include-email'], true).then(res => { | ||||||
|   await execm.exec('aws', ['ecr', 'get-login', '--region', ecrRegion, '--no-include-email'], true).then(res => { |  | ||||||
|     if (res.stderr != '' && !res.success) { |     if (res.stderr != '' && !res.success) { | ||||||
|       throw new Error(res.stderr); |       throw new Error(res.stderr); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -1,7 +0,0 @@ | |||||||
| 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')); |  | ||||||
| }; |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 CrazyMax
						CrazyMax