在Jenkins中运行groovy脚本

时间:2017-12-05 11:12:37

标签: jenkins groovy

我正在尝试在jenkins中运行groovy脚本。但我得到的错误。

            import groovy.sql.Sql
            def output = []
            def sql = Sql.newInstance('jdbc:mysql://localhost:3306/logging_schema',     'root', '****', 'com.mysql.jdbc.Driver')
            String sqlString = "SELECT * debug DESC Limit 10;"
            sql.eachRow(sqlString){ row ->  
                output.push(row[0])`enter code here`
            }
            return output


                FATAL: command execution failed
            java.io.IOException: Cannot run program "groovy" (in directory "/var/lib/jenkins/workspace/TestNexusDeploy"): error=2, No such file or directory
                at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
                at hudson.Proc$LocalProc.<init>(Proc.java:245)
                at hudson.Proc$LocalProc.<init>(Proc.java:214)
                at hudson.Launcher$LocalLauncher.launch(Launcher.java:846)
                at hudson.Launcher$ProcStarter.start(Launcher.java:384)
                at hudson.Launcher$ProcStarter.join(Launcher.java:395)
                at hudson.plugins.groovy.Groovy.perform(Groovy.java:102)
                at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
                at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
                at hudson.model.Build$BuildExecution.build(Build.java:205)
                at hudson.model.Build$BuildExecution.doRun(Build.java:162)
                at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:534)
                at hudson.model.Run.execute(Run.java:1728)
                at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
                at hudson.model.ResourceController.execute(ResourceController.java:98)
                at hudson.model.Executor.run(Executor.java:404)
            Caused by: java.io.IOException: error=2, No such file or directory
                at java.lang.UNIXProcess.forkAndExec(Native Method)
                at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
                at java.lang.ProcessImpl.start(ProcessImpl.java:134)
                at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
                ... 15 more
            Build step 'Execute Groovy script' marked build as failure
            [WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done
            Finished: FAILURE

如果我需要做一些全局配置或任何其他解决方案,请帮助我。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我们必须添加相应的jar文件来运行sql实例。 Jar文件可以从类路径或工作空间加载,在这种情况下你必须首先创建classloader对象并尝试加载jar文件,如下所述。

import groovy.sql.Sql;

def classLoader = ClassLoader.systemClassLoader
while (classLoader.parent) {
    classLoader = classLoader.parent
}
classLoader.addURL(new File("/var/lib/jenkins/workspace/egsdloen-
logging/mysql/mysql.jar").toURL())
def sql = 
Sql.newInstance('jdbc:mysql://server/dbname', 
'user', 'password', 'com.mysql.jdbc.Driver')
sql.execute 'SELECT * FROM tablename limit 10'
sql.close()