masaki / jenkins-travis-yml-plugin

go to https://github.com/jenkinsci/travis-yml-plugin
MIT License
21 stars 16 forks source link

Broken by slightly sophisticated shell code in .travis.yml #9

Closed seschwar closed 8 years ago

seschwar commented 9 years ago

This .travis.yml:


---
script: |
    if true; then
        true
    fi
...

produces the following error:

/var/lib/jenkins/workspace/JAVIS/hudson.20141201122107.920.sh: line 39: syntax error near unexpected token `then'
/var/lib/jenkins/workspace/JAVIS/hudson.20141201122107.920.sh: line 39: `echo $ if true; then'
ERROR:  (Java::Hudson::AbortException)
sun.reflect.GeneratedConstructorAccessor127.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:526)
org.jruby.javasupport.JavaConstructor.newInstanceDirect(JavaConstructor.java:275)
org.jruby.java.invokers.ConstructorInvoker.call(ConstructorInvoker.java:91)
org.jruby.java.invokers.ConstructorInvoker.call(ConstructorInvoker.java:178)
org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:336)
org.jruby.runtime.callsite.CachingCallSite.callBlock(CachingCallSite.java:179)
org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:183)
org.jruby.java.proxies.ConcreteJavaProxy$2.call(ConcreteJavaProxy.java:52)
org.jruby.runtime.callsite.CachingCallSite.callBlock(CachingCallSite.java:177)
org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:183)
org.jruby.RubyClass.newInstance(RubyClass.java:855)
org.jruby.RubyClass$INVOKER$i$newInstance.call(RubyClass$INVOKER$i$newInstance.gen)
org.jruby.internal.runtime.methods.JavaMethod$JavaMethodZeroOrOneOrNBlock.call(JavaMethod.java:278)
org.jruby.java.proxies.ConcreteJavaProxy$3.call(ConcreteJavaProxy.java:148)
org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:168)
org.jruby.ast.CallOneArgNode.interpret(CallOneArgNode.java:57)
org.jruby.ast.FCallOneArgNode.interpret(FCallOneArgNode.java:36)
org.jruby.ast.NewlineNode.interpret(NewlineNode.java:105)
org.jruby.evaluator.ASTInterpreter.INTERPRET_METHOD(ASTInterpreter.java:75)
org.jruby.internal.runtime.methods.InterpretedMethod.call(InterpretedMethod.java:139)
org.jruby.internal.runtime.methods.DefaultMethod.call(DefaultMethod.java:172)
rubyjit.TravisYmlBuilder$$perform_4A06EDC1D08D2E7DE9267BC005E417035E798D8A1695017752.__file__(/var/lib/jenkins/plugins/travis-yml/WEB-INF/classes/models/travis_yml_builder.rb:34)
java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:599)
org.jruby.runtime.invokedynamic.InvocationLinker.invocationFallback(InvocationLinker.java:183)
rubyjit.Jenkins::Tasks::BuildStepProxy$$perform_5A3B002C7CA27A9DAFD502D73F0A034D55A627421695017752.block_0$RUBY$__file__(/var/lib/jenkins/plugins/travis-yml/WEB-INF/classes/vendor/gems/gems/jenkins-plugin-runtime-0.2.3/lib/jenkins/tasks/build_step_proxy.rb:14)
rubyjit$Jenkins::Tasks::BuildStepProxy$$perform_5A3B002C7CA27A9DAFD502D73F0A034D55A627421695017752$block_0$RUBY$__file__.call(rubyjit$Jenkins::Tasks::BuildStepProxy$$perform_5A3B002C7CA27A9DAFD502D73F0A034D55A627421695017752$block_0$RUBY$__file__)
org.jruby.runtime.CompiledBlock19.yieldSpecificInternal(CompiledBlock19.java:121)
org.jruby.runtime.CompiledBlock19.yieldSpecific(CompiledBlock19.java:96)
org.jruby.runtime.Block.yieldSpecific(Block.java:99)
org.jruby.runtime.invokedynamic.InvocationLinker.yieldSpecificFallback(InvocationLinker.java:404)
rubyjit.Jenkins::Tasks::BuildStepProxy$$boolean_result_D074B6AB5339B2940C00AF05958432465EDE52701695017752.chained_0_rescue_1$RUBY$SYNTHETIC__file__(/var/lib/jenkins/plugins/travis-yml/WEB-INF/classes/vendor/gems/gems/jenkins-plugin-runtime-0.2.3/lib/jenkins/tasks/build_step_proxy.rb:22)
rubyjit.Jenkins::Tasks::BuildStepProxy$$boolean_result_D074B6AB5339B2940C00AF05958432465EDE52701695017752.__file__(/var/lib/jenkins/plugins/travis-yml/WEB-INF/classes/vendor/gems/gems/jenkins-plugin-runtime-0.2.3/lib/jenkins/tasks/build_step_proxy.rb:21)
rubyjit.Jenkins::Tasks::BuildStepProxy$$perform_5A3B002C7CA27A9DAFD502D73F0A034D55A627421695017752.__file__(/var/lib/jenkins/plugins/travis-yml/WEB-INF/classes/vendor/gems/gems/jenkins-plugin-runtime-0.2.3/lib/jenkins/tasks/build_step_proxy.rb:13)
rubyjit.Jenkins::Tasks::BuildStepProxy$$perform_5A3B002C7CA27A9DAFD502D73F0A034D55A627421695017752.__file__(/var/lib/jenkins/plugins/travis-yml/WEB-INF/classes/vendor/gems/gems/jenkins-plugin-runtime-0.2.3/lib/jenkins/tasks/build_step_proxy.rb)
rubyjit.Jenkins::Tasks::BuildStepProxy$$perform_5A3B002C7CA27A9DAFD502D73F0A034D55A627421695017752.__file__(/var/lib/jenkins/plugins/travis-yml/WEB-INF/classes/vendor/gems/gems/jenkins-plugin-runtime-0.2.3/lib/jenkins/tasks/build_step_proxy.rb)
org.jruby.internal.runtime.methods.JittedMethod.call(JittedMethod.java:121)
org.jruby.javasupport.proxy.JavaProxyConstructor$2.invoke(JavaProxyConstructor.java:224)
org.jruby.proxy.hudson.tasks.Builder$Proxy1.perform(Unknown Source)
hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:770)
hudson.model.Build$BuildExecution.build(Build.java:199)
hudson.model.Build$BuildExecution.doRun(Build.java:160)
hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:533)
hudson.model.Run.execute(Run.java:1759)
hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
hudson.model.ResourceController.execute(ResourceController.java:89)
hudson.model.Executor.run(Executor.java:240)

Here is the generated build script:

#!/usr/bin/env bash

capture_result() {
  local result=$1
  export TRAVIS_JENKINS_RESULT=$(( ${TRAVIS_JENKINS_RESULT:-0} | $(($result != 0)) ))
}

export CI=1
export CONTINUOUS_INTEGRATION=1
export TRAVIS_JENKINS_RESULT=0

if [ $TRAVIS_JENKINS_RESULT -eq 0 ]; then
echo $ if true; then
    true
fi

if test true; then
    true
fi

fi
exit $TRAVIS_JENKINS_RESULT

There we can clearly see that the bug is caused by not quoting and escaping script from .travis.yml when it is used as an argument to echo. Without that the generated lines are treated as a shell commands. Which is bad in itself, but in this case the commands even have an invalid syntax. Which ultimately leads to failed builds.

alvarolorentedev commented 8 years ago

solved in latest pull request