2017-12-06 13:03:06 +02:00
import com.cloudbees.groovy.cps.NonCPS
def call ( Map parameters = [ : ] , body ) {
2017-12-13 11:58:22 +02:00
2017-12-14 15:29:26 +02:00
def STEP_NAME = 'dockerExecute'
2017-12-13 11:58:22 +02:00
def PLUGIN_ID_DOCKER_WORKFLOW = 'docker-workflow'
2018-05-14 16:53:13 +02:00
handlePipelineStepErrors ( stepName: STEP_NAME , stepParameters: parameters ) {
2018-03-29 14:13:11 +02:00
def dockerImage = parameters . dockerImage ? : ''
Map dockerEnvVars = parameters . dockerEnvVars ? : [ : ]
def dockerOptions = parameters . dockerOptions ? : ''
Map dockerVolumeBind = parameters . dockerVolumeBind ? : [ : ]
2018-05-14 16:53:13 +02:00
def k8s = parameters . k8s ? : true
2018-05-14 17:11:27 +02:00
2018-05-14 16:53:13 +02:00
if ( k8s ) {
2018-05-31 15:49:23 +02:00
echo "redirecting to executeDocker"
executeDocker ( stepParameters ) {
2018-05-15 08:59:07 +02:00
body ( )
}
2018-05-14 16:53:13 +02:00
} else if ( dockerImage ) {
2017-12-06 13:03:06 +02:00
2018-05-14 16:53:13 +02:00
if ( ! isPluginActive ( PLUGIN_ID_DOCKER_WORKFLOW ) ) {
2017-12-14 15:29:26 +02:00
echo "[WARNING][${STEP_NAME}] Docker not supported. Plugin '${PLUGIN_ID_DOCKER_WORKFLOW}' is not installed or not active. Configured docker image '${dockerImage}' will not be used."
2017-12-13 11:58:22 +02:00
dockerImage = null
}
def returnCode = sh script: 'which docker > /dev/null' , returnStatus: true
2018-05-14 16:53:13 +02:00
if ( returnCode ! = 0 ) {
2017-12-14 15:29:26 +02:00
echo "[WARNING][${STEP_NAME}] No docker environment found (command 'which docker' did not return with '0'). Configured docker image '${dockerImage}' will not be used."
2017-12-13 11:58:22 +02:00
dockerImage = null
}
2018-01-15 14:42:57 +02:00
returnCode = sh script: 'docker ps -q > /dev/null' , returnStatus: true
2018-05-14 16:53:13 +02:00
if ( returnCode ! = 0 ) {
2018-01-15 14:42:57 +02:00
echo "[WARNING][$STEP_NAME] Cannot connect to docker daemon (command 'docker ps' did not return with '0'). Configured docker image '${dockerImage}' will not be used."
2018-01-15 14:45:16 +02:00
dockerImage = null
2018-01-15 14:42:57 +02:00
}
2017-12-06 13:03:06 +02:00
def image = docker . image ( dockerImage )
image . pull ( )
image . inside ( getDockerOptions ( dockerEnvVars , dockerVolumeBind , dockerOptions ) ) {
body ( )
}
}
2018-05-15 14:47:59 +02:00
if ( ! dockerImage ) {
echo "[INFO][${STEP_NAME}] Running on local environment."
body ( )
}
2017-12-06 13:03:06 +02:00
}
}
2018-04-20 12:59:17 +02:00
@NonCPS
2018-05-14 16:53:13 +02:00
private isPluginActive ( String pluginId ) {
2018-04-20 12:59:17 +02:00
return Jenkins . instance . pluginManager . plugins . find { p - > p . isActive ( ) & & p . getShortName ( ) = = pluginId }
}
2017-12-06 13:03:06 +02:00
/ * *
* Returns a string with docker options containing
* environment variables ( if set ) .
* Possible to extend with further options .
* @param dockerEnvVars Map with environment variables
* /
@NonCPS
private getDockerOptions ( Map dockerEnvVars , Map dockerVolumeBind , def dockerOptions ) {
2018-05-14 16:53:13 +02:00
def specialEnvironments = [ 'http_proxy' ,
'https_proxy' ,
'no_proxy' ,
'HTTP_PROXY' ,
'HTTPS_PROXY' ,
'NO_PROXY' ]
2018-03-29 14:13:11 +02:00
def options = [ ]
2017-12-06 13:03:06 +02:00
if ( dockerEnvVars ) {
for ( String k : dockerEnvVars . keySet ( ) ) {
2018-03-29 14:13:11 +02:00
options . add ( "--env ${k}=${dockerEnvVars[k].toString()}" )
2017-12-06 13:03:06 +02:00
}
}
for ( String envVar : specialEnvironments ) {
if ( dockerEnvVars = = null | | ! dockerEnvVars . containsKey ( envVar ) ) {
2018-03-29 14:13:11 +02:00
options . add ( "--env ${envVar}" )
2017-12-06 13:03:06 +02:00
}
}
if ( dockerVolumeBind ) {
for ( String k : dockerVolumeBind . keySet ( ) ) {
2018-03-29 14:13:11 +02:00
options . add ( "--volume ${k}:${dockerVolumeBind[k].toString()}" )
2017-12-06 13:03:06 +02:00
}
}
2018-03-29 14:13:11 +02:00
if ( dockerOptions instanceof CharSequence ) {
options . add ( dockerOptions . toString ( ) )
} else if ( dockerOptions instanceof List ) {
for ( String option : dockerOptions ) {
options . add "${option}"
}
} else {
throw new IllegalArgumentException ( "Unexpected type for dockerOptions. Expected was either a list or a string. Actual type was: '${dockerOptions.getClass()}'" )
2017-12-06 13:03:06 +02:00
}
2018-03-29 14:13:11 +02:00
return options . join ( ' ' )
2017-12-06 13:03:06 +02:00
}