@@ -45,7 +45,7 @@ const getOptionDesc = function (cmdOpt, optName) {
4545var argv = yargs . usage ( 'simplify-pipeline create|list [stage] [options]' )
4646 . string ( 'help' ) . describe ( 'help' , 'display help for a specific command' )
4747 . string ( 'project' ) . alias ( 'p' , 'project' ) . describe ( 'project' , getOptionDesc ( 'create' , 'project' ) )
48- . string ( 'file' ) . alias ( 'f' , 'file' ) . describe ( 'file' , getOptionDesc ( 'file ' , 'file' ) )
48+ . string ( 'file' ) . alias ( 'f' , 'file' ) . describe ( 'file' , getOptionDesc ( 'list ' , 'file' ) )
4949 . demandCommand ( 1 ) . argv ;
5050
5151showBoxBanner ( )
@@ -73,7 +73,7 @@ if (cmdOPS == 'CREATE') {
7373 if ( index >= 0 ) {
7474 const executedStage = yamlObject . stages [ index ]
7575 let dockerComposeContent = { version : '3.8' , services : { } , volumes : { } }
76- dockerComposeContent . volumes [ `${ projectName } ` ] = {
76+ dockerComposeContent . volumes [ `shared ` ] = {
7777 "driver" : "local" ,
7878 "driver_opts" : {
7979 "type" : "none" ,
@@ -85,7 +85,7 @@ if (cmdOPS == 'CREATE') {
8585 let dockerCacheVolumes = [ ]
8686 let dockerBaseContent = [ `FROM ${ yamlObject . image } ` , 'WORKDIR /source' , 'VOLUME /source' , 'COPY . /source' ]
8787 let dockerBasePath = `${ projectName } /Dockerfile`
88- const baseImage = `base-${ projectName . replace ( / \. / g , '' ) } `
88+ const baseImage = `base-${ yamlObject . image . split ( ':' ) [ 0 ] } `
8989 const baseDirName = path . dirname ( path . resolve ( dockerBasePath ) )
9090 if ( ! fs . existsSync ( baseDirName ) ) {
9191 fs . mkdirSync ( baseDirName , { recursive : true } )
@@ -100,10 +100,26 @@ if (cmdOPS == 'CREATE') {
100100 Object . keys ( yamlObject ) . map ( ( key , idx ) => {
101101 if ( yamlObject [ key ] . stage === executedStage ) {
102102 const stageName = `${ idx } -${ executedStage } .${ key } `
103- dockerComposeContent . services [ key ] = { build : { context : `../` , dockerfile : `${ projectName } /${ stageName } .Dockerfile` , args : { } } , volumes : [ `${ projectName } :/source` ] }
103+ dockerComposeContent . services [ key ] = { build : { context : `../` , dockerfile : `${ projectName } /${ stageName } .Dockerfile` , args : { } } , volumes : [ `shared :/source` ] }
104104 stageExecutionChains . push ( `${ stageName } ` )
105105 dockerfileContent = [ `FROM ${ projectName . replace ( / \. / g, '' ) } _${ baseImage } ` , 'WORKDIR /source' ]
106106 let dockerCommands = [ ]
107+ let dockerBeforeCommands = [ ]
108+
109+ simplify . getContentArgs ( yamlObject [ key ] . before_script , { ...process . env } , { ...variables } ) . map ( script => {
110+ let scriptContent = script
111+ if ( script . startsWith ( 'export ' ) ) {
112+ let dockerOpts = 'ENV'
113+ scriptContent = script . replace ( 'export ' , '' )
114+ const argKeyValue = scriptContent . split ( '=' )
115+ dockerComposeContent . services [ key ] . build . args [ `${ argKeyValue [ 0 ] . trim ( ) } ` ] = `${ argKeyValue [ 1 ] . trim ( ) } `
116+ scriptContent = `${ argKeyValue [ 0 ] . trim ( ) } ="${ argKeyValue [ 1 ] . trim ( ) } "`
117+ dockerfileContent . push ( `${ dockerOpts } ${ scriptContent } ` )
118+ } else {
119+ dockerBeforeCommands . push ( `${ scriptContent } ` )
120+ }
121+ } )
122+
107123 simplify . getContentArgs ( yamlObject [ key ] . script , { ...process . env } , { ...variables } ) . map ( script => {
108124 let scriptContent = script
109125 if ( script . startsWith ( 'export ' ) ) {
@@ -114,10 +130,11 @@ if (cmdOPS == 'CREATE') {
114130 scriptContent = `${ argKeyValue [ 0 ] . trim ( ) } ="${ argKeyValue [ 1 ] . trim ( ) } "`
115131 dockerfileContent . push ( `${ dockerOpts } ${ scriptContent } ` )
116132 } else {
117- dockerCommands . push ( `RUN ${ scriptContent } ` )
133+ dockerCommands . push ( `${ scriptContent } ` )
118134 }
119135 } )
120- dockerfileContent . push ( `${ dockerCommands . join ( '\n' ) } ` )
136+ dockerfileContent . push ( `RUN ${ dockerBeforeCommands . join ( ' && ' ) } ` )
137+ dockerfileContent . push ( `RUN ${ dockerCommands . join ( ' && ' ) } ` )
121138 let dockerfilePath = `${ projectName } /${ stageName } .Dockerfile`
122139 const pathDirName = path . dirname ( path . resolve ( dockerfilePath ) )
123140 if ( ! fs . existsSync ( pathDirName ) ) {
@@ -132,7 +149,8 @@ if (cmdOPS == 'CREATE') {
132149 fs . writeFileSync ( `pipeline.bash` , [
133150 '#!/bin/bash' ,
134151 `cd ${ projectName } ` ,
135- `docker-compose -f docker-compose.$1.yml up --force-recreate`
152+ `docker volume rm ${ projectName . replace ( / \. / g, '' ) } _shared > /dev/null` ,
153+ `docker-compose -f docker-compose.$1.yml --project-name ${ projectName } up --force-recreate`
136154 ] . join ( '\n' ) )
137155 }
138156} else if ( cmdOPS == 'LIST' ) {
0 commit comments