1- import * as aws from './aws' ;
21import * as core from '@actions/core' ;
32
3+ import * as aws from './aws' ;
4+ import * as context from './context' ;
5+
46import { Docker } from '@docker/actions-toolkit/lib/docker/docker' ;
57
6- export async function login ( registry : string , username : string , password : string , ecr : string ) : Promise < void > {
7- if ( / t r u e / i. test ( ecr ) || ( ecr == 'auto' && aws . isECR ( registry ) ) ) {
8- await loginECR ( registry , username , password ) ;
8+ export async function login ( auth : context . Auth ) : Promise < void > {
9+ if ( / t r u e / i. test ( auth . ecr ) || ( auth . ecr == 'auto' && aws . isECR ( auth . registry ) ) ) {
10+ await loginECR ( auth . registry , auth . username , auth . password , auth . scope ) ;
911 } else {
10- await loginStandard ( registry , username , password ) ;
12+ await loginStandard ( auth . registry , auth . username , auth . password , auth . scope ) ;
1113 }
1214}
1315
14- export async function logout ( registry : string ) : Promise < void > {
16+ export async function logout ( registry : string , configDir : string ) : Promise < void > {
17+ let envs : { [ key : string ] : string } | undefined ;
18+ if ( configDir !== '' ) {
19+ envs = Object . assign ( { } , process . env , {
20+ DOCKER_CONFIG : configDir
21+ } ) as {
22+ [ key : string ] : string ;
23+ } ;
24+ core . info ( `Alternative config dir: ${ configDir } ` ) ;
25+ }
1526 await Docker . getExecOutput ( [ 'logout' , registry ] , {
16- ignoreReturnCode : true
27+ ignoreReturnCode : true ,
28+ env : envs
1729 } ) . then ( res => {
1830 if ( res . stderr . length > 0 && res . exitCode != 0 ) {
1931 core . warning ( res . stderr . trim ( ) ) ;
2032 }
2133 } ) ;
2234}
2335
24- export async function loginStandard ( registry : string , username : string , password : string ) : Promise < void > {
36+ export async function loginStandard ( registry : string , username : string , password : string , scope ?: string ) : Promise < void > {
2537 if ( ! username && ! password ) {
2638 throw new Error ( 'Username and password required' ) ;
2739 }
@@ -31,38 +43,39 @@ export async function loginStandard(registry: string, username: string, password
3143 if ( ! password ) {
3244 throw new Error ( 'Password required' ) ;
3345 }
46+ await loginExec ( registry , username , password , scope ) ;
47+ }
3448
35- const loginArgs : Array < string > = [ 'login' , '--password-stdin' ] ;
36- loginArgs . push ( '--username' , username ) ;
37- loginArgs . push ( registry ) ;
49+ export async function loginECR ( registry : string , username : string , password : string , scope ?: string ) : Promise < void > {
50+ core . info ( `Retrieving registries data through AWS SDK...` ) ;
51+ const regDatas = await aws . getRegistriesData ( registry , username , password ) ;
52+ for ( const regData of regDatas ) {
53+ await loginExec ( regData . registry , regData . username , regData . password , scope ) ;
54+ }
55+ }
3856
39- core . info ( `Logging into ${ registry } ...` ) ;
40- await Docker . getExecOutput ( loginArgs , {
57+ async function loginExec ( registry : string , username : string , password : string , scope ?: string ) : Promise < void > {
58+ let envs : { [ key : string ] : string } | undefined ;
59+ const configDir = context . scopeToConfigDir ( registry , scope ) ;
60+ if ( configDir !== '' ) {
61+ envs = Object . assign ( { } , process . env , {
62+ DOCKER_CONFIG : configDir
63+ } ) as {
64+ [ key : string ] : string ;
65+ } ;
66+ core . info ( `Logging into ${ registry } (scope ${ scope } )...` ) ;
67+ } else {
68+ core . info ( `Logging into ${ registry } ...` ) ;
69+ }
70+ await Docker . getExecOutput ( [ 'login' , '--password-stdin' , '--username' , username , registry ] , {
4171 ignoreReturnCode : true ,
4272 silent : true ,
43- input : Buffer . from ( password )
73+ input : Buffer . from ( password ) ,
74+ env : envs
4475 } ) . then ( res => {
4576 if ( res . stderr . length > 0 && res . exitCode != 0 ) {
4677 throw new Error ( res . stderr . trim ( ) ) ;
4778 }
48- core . info ( ` Login Succeeded!` ) ;
79+ core . info ( ' Login Succeeded!' ) ;
4980 } ) ;
5081}
51-
52- export async function loginECR ( registry : string , username : string , password : string ) : Promise < void > {
53- core . info ( `Retrieving registries data through AWS SDK...` ) ;
54- const regDatas = await aws . getRegistriesData ( registry , username , password ) ;
55- for ( const regData of regDatas ) {
56- core . info ( `Logging into ${ regData . registry } ...` ) ;
57- await Docker . getExecOutput ( [ 'login' , '--password-stdin' , '--username' , regData . username , regData . registry ] , {
58- ignoreReturnCode : true ,
59- silent : true ,
60- input : Buffer . from ( regData . password )
61- } ) . then ( res => {
62- if ( res . stderr . length > 0 && res . exitCode != 0 ) {
63- throw new Error ( res . stderr . trim ( ) ) ;
64- }
65- core . info ( 'Login Succeeded!' ) ;
66- } ) ;
67- }
68- }
0 commit comments