4
4
using System . Linq ;
5
5
using System . Text . RegularExpressions ;
6
6
using System . Threading ;
7
+ using System . Threading . Channels ;
7
8
using System . Threading . Tasks ;
8
9
using GitHub . Runner . Common ;
9
10
using GitHub . Runner . Sdk ;
@@ -17,6 +18,7 @@ public interface IDockerCommandManager : IRunnerService
17
18
string DockerInstanceLabel { get ; }
18
19
Task < DockerVersion > DockerVersion ( IExecutionContext context ) ;
19
20
Task < int > DockerPull ( IExecutionContext context , string image ) ;
21
+ Task < int > DockerPull ( IExecutionContext context , string image , string configFileDirectory ) ;
20
22
Task < int > DockerBuild ( IExecutionContext context , string workingDirectory , string dockerFile , string dockerContext , string tag ) ;
21
23
Task < string > DockerCreate ( IExecutionContext context , ContainerInfo container ) ;
22
24
Task < int > DockerRun ( IExecutionContext context , ContainerInfo container , EventHandler < ProcessDataReceivedEventArgs > stdoutDataReceived , EventHandler < ProcessDataReceivedEventArgs > stderrDataReceived ) ;
@@ -31,6 +33,7 @@ public interface IDockerCommandManager : IRunnerService
31
33
Task < int > DockerExec ( IExecutionContext context , string containerId , string options , string command , List < string > outputs ) ;
32
34
Task < List < string > > DockerInspect ( IExecutionContext context , string dockerObject , string options ) ;
33
35
Task < List < PortMapping > > DockerPort ( IExecutionContext context , string containerId ) ;
36
+ Task < int > DockerLogin ( IExecutionContext context , string configFileDirectory , string registry , string username , string password ) ;
34
37
}
35
38
36
39
public class DockerCommandManager : RunnerService , IDockerCommandManager
@@ -82,9 +85,18 @@ public async Task<DockerVersion> DockerVersion(IExecutionContext context)
82
85
return new DockerVersion ( serverVersion , clientVersion ) ;
83
86
}
84
87
85
- public async Task < int > DockerPull ( IExecutionContext context , string image )
88
+ public Task < int > DockerPull ( IExecutionContext context , string image )
86
89
{
87
- return await ExecuteDockerCommandAsync ( context , "pull" , image , context . CancellationToken ) ;
90
+ return DockerPull ( context , image , null ) ;
91
+ }
92
+
93
+ public async Task < int > DockerPull ( IExecutionContext context , string image , string configFileDirectory )
94
+ {
95
+ if ( string . IsNullOrEmpty ( configFileDirectory ) )
96
+ {
97
+ return await ExecuteDockerCommandAsync ( context , $ "pull", image , context . CancellationToken ) ;
98
+ }
99
+ return await ExecuteDockerCommandAsync ( context , $ "--config { configFileDirectory } pull", image , context . CancellationToken ) ;
88
100
}
89
101
90
102
public async Task < int > DockerBuild ( IExecutionContext context , string workingDirectory , string dockerFile , string dockerContext , string tag )
@@ -346,6 +358,28 @@ public async Task<List<PortMapping>> DockerPort(IExecutionContext context, strin
346
358
return DockerUtil . ParseDockerPort ( portMappingLines ) ;
347
359
}
348
360
361
+ public Task < int > DockerLogin ( IExecutionContext context , string configFileDirectory , string registry , string username , string password )
362
+ {
363
+ string args = $ "--config { configFileDirectory } login { registry } -u { username } --password-stdin";
364
+ context . Command ( $ "{ DockerPath } { args } ") ;
365
+
366
+ var input = Channel . CreateBounded < string > ( new BoundedChannelOptions ( 1 ) { SingleReader = true , SingleWriter = true } ) ;
367
+ input . Writer . TryWrite ( password ) ;
368
+
369
+ var processInvoker = HostContext . CreateService < IProcessInvoker > ( ) ;
370
+
371
+ return processInvoker . ExecuteAsync (
372
+ workingDirectory : context . GetGitHubContext ( "workspace" ) ,
373
+ fileName : DockerPath ,
374
+ arguments : args ,
375
+ environment : null ,
376
+ requireExitCodeZero : false ,
377
+ outputEncoding : null ,
378
+ killProcessOnCancel : false ,
379
+ redirectStandardIn : input ,
380
+ cancellationToken : context . CancellationToken ) ;
381
+ }
382
+
349
383
private Task < int > ExecuteDockerCommandAsync ( IExecutionContext context , string command , string options , CancellationToken cancellationToken = default ( CancellationToken ) )
350
384
{
351
385
return ExecuteDockerCommandAsync ( context , command , options , null , cancellationToken ) ;
0 commit comments