@@ -38,19 +38,25 @@ export function startBuildProcess(buildId: number, jobId: number,
3838 } , [ ] ) ;
3939 commands = commands . filter ( cmd => ! cmd . startsWith ( 'export' ) ) ;
4040
41- startContainer ( name , image , vars )
41+ const sub = startContainer ( name , image , vars )
4242 . concat ( ssh ? executeInContainer ( name , 'sudo /etc/init.d/ssh start' ) : Observable . empty ( ) )
4343 . concat ( ssh ? getContainerExposedPort ( name , 22 ) : Observable . empty ( ) )
4444 . concat ( ...commands . map ( command => executeInContainer ( name , command ) ) )
4545 . subscribe ( ( event : ProcessOutput ) => {
4646 observer . next ( event ) ;
4747 } , err => {
48- observer . next ( { type : 'data' , data : err } ) ;
48+ sub . unsubscribe ( ) ;
4949 observer . error ( err ) ;
50- stopContainer ( name ) . subscribe ( ( event : ProcessOutput ) => observer . next ( event ) ) ;
50+ stopContainer ( name ) . subscribe ( ( event : ProcessOutput ) => {
51+ observer . next ( event ) ;
52+ observer . next ( { type : 'data' , data : err } ) ;
53+ } ) ;
5154 } , ( ) => {
55+ sub . unsubscribe ( ) ;
5256 observer . complete ( ) ;
53- stopContainer ( name ) . subscribe ( ( event : ProcessOutput ) => observer . next ( event ) ) ;
57+ stopContainer ( name ) . subscribe ( ( event : ProcessOutput ) => {
58+ observer . next ( event ) ;
59+ } ) ;
5460 } ) ;
5561 } ) ;
5662}
@@ -61,7 +67,7 @@ function executeInContainer(name: string, command: string): Observable<ProcessOu
6167
6268 start . on ( 'exit' , startCode => {
6369 if ( startCode !== 0 ) {
64- observer . error ( bold ( red ( 'Container errored with exit code ' + startCode ) ) ) ;
70+ observer . error ( red ( 'Container errored with exit code ' + startCode ) ) ;
6571 }
6672
6773
@@ -80,7 +86,7 @@ function executeInContainer(name: string, command: string): Observable<ProcessOu
8086 attach . on ( 'data' , data => {
8187 if ( ! executed ) {
8288 attach . write ( command + ' && echo EXECOK || echo EXECNOK\r' ) ;
83- observer . next ( { type : 'data' , data : bold ( yellow ( '==> ' + command ) ) + '\r' } ) ;
89+ observer . next ( { type : 'data' , data : yellow ( '==> ' + command ) + '\r' } ) ;
8490 executed = true ;
8591 } else if ( data . includes ( 'EXECOK' ) ) {
8692 exitCode = 0 ;
@@ -95,8 +101,9 @@ function executeInContainer(name: string, command: string): Observable<ProcessOu
95101 } ) ;
96102
97103 attach . on ( 'exit' , code => {
104+ code = ( detachKey === 'D' ) ? exitCode : code ;
98105 if ( exitCode !== 0 ) {
99- observer . error ( bold ( `Executed command returned exit code ${ red ( exitCode . toString ( ) ) } ` ) ) ;
106+ observer . error ( red ( `Executed command returned exit code ${ bold ( exitCode . toString ( ) ) } ` ) ) ;
100107 } else {
101108 observer . next ( { type : 'exit' , data : exitCode . toString ( ) } ) ;
102109 observer . complete ( ) ;
0 commit comments