Skip to content

Instantly share code, notes, and snippets.

@davidkarlsen
Last active April 13, 2017 19:40
Show Gist options
  • Save davidkarlsen/882af8fa46859134aa3502fd5921cb79 to your computer and use it in GitHub Desktop.
Save davidkarlsen/882af8fa46859134aa3502fd5921cb79 to your computer and use it in GitHub Desktop.
with nonCPS
workflow library script:
def call(composeFile, String services, boolean doDebug = false, Closure body) {
try {
withEnv( dockerComposeUp( composeFile, services ) ) {
debug(doDebug)
body()
}
}
finally {
dockerComposeDown( composeFile )
}
}
static def String splitComposeArgs( composeFiles ) {
String arg = "";
for( String composeFile : composeFiles.tokenize(" \t\n\r\f,;") ) {
arg += "-f $composeFile "
}
return arg
}
def dockerComposeUp( composeFiles, String services ) {
composeFile = splitComposeArgs( composeFiles )
sh "export GOSU_UID=`id -u` GOSU_GID=`id -g` ; docker-compose $composeFile pull --ignore-pull-failures $services &&\
docker-compose $composeFile up -d $services &&\
docker-compose $composeFile ps"
script = "SERVICES=\"$services\"\n" +
'for SERVICE in $SERVICES; do \n' +
" CONTAINERID=`docker-compose $composeFile ps -q \$SERVICE`\n" +
' IP=`docker inspect --format \'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}\' $CONTAINERID`\n' +
' PORTS=`docker inspect --format=\'{{range $p, $conf := .NetworkSettings.Ports}} {{$p}}{{end}}\' $CONTAINERID`\n' +
' for PORTPROTO in $PORTS; do\n' + ' IFS=\'/ \' read -r -a array <<< "$PORTPROTO"\n' +
' PORT="${array[0]}"\n' +
' PROTO="${array[1]}"\n' +
' echo "${SERVICE^^}_PORT_${PORT}_${PROTO^^}_PORT=$PORT"\n' +
' echo "${SERVICE^^}_PORT_${PORT}_${PROTO^^}_ADDR=${IP}"\n' +
' done\n' +
'done'
stdout = sh( returnStdout: true, script: "$script" )
//stdout = sh( returnStdout: true, script: "for container in $services; do docker-compose run --rm -T -e TERMINAL=xterm \$container env|grep PORT;done" )
return stdout.tokenize("\r?\n")
}
def dockerComposeDown( composeFiles ) {
composeFile = splitComposeArgs( composeFiles )
sh "export GOSU_UID=`id -u` GOSU_GID=`id -g` ; docker-compose $composeFile down -v"
}
~
~
~
without @NonCps (which works fine in a normal pipeline, e.g. a non-declarative one:
ava.lang.RuntimeException: Failed to serialize org.jenkinsci.plugins.pipeline.modeldefinition.when.impl.ExpressionConditional#block for class org.jenkinsci.plugins.pipeline.modeldefinition.when.impl.ExpressionConditional
at hudson.util.RobustReflectionConverter$2.writeField(RobustReflectionConverter.java:256)
at hudson.util.RobustReflectionConverter$2.visit(RobustReflectionConverter.java:224)
at com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider.visitSerializableFields(PureJavaReflectionProvider.java:138)
at hudson.util.RobustReflectionConverter.doMarshal(RobustReflectionConverter.java:209)
at hudson.util.RobustReflectionConverter.marshal(RobustReflectionConverter.java:150)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:43)
at com.thoughtworks.xstream.core.TreeMarshaller.s
with @NonCPS on dockerComposeUp:
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.EnvStep.overrides expects java.util.List<java.lang.String> but received class java.lang.String
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:384)
at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:313)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:257)
at org.jenkinsci.plugins.workflow.steps.StepDescriptor.newInstance(StepDescriptor.java:194)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:181)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:126)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:18)
at dockerCompose.call(/data/jenkins/jobs/tac/jobs/jfr/jobs/bitbucketTacJfrTeamFolder/jobs/tac-jfr-server/branches/feature%2FTACJFRSR-167/builds/29/libs/coejava/vars/dockerCompose.groovy:8)
at WorkflowScript.run(WorkflowScript:82)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
at sun.reflect.GeneratedMethodAcce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment