2016-08-17 10 views
2

私はパイプライン上で作成したので、その中に1つのSQLクエリを実行したい。私は次のステートメント(コードの2行、インポート/クラスなどなし)を書いており、実行中にエラーをスローします。JenkinsのパイプラインスクリプトでSQL文を実行する方法は?

import groovy.sql.Sql 
def sql = Sql.newInstance("jdbc:mysql://myIP:3306/dbName", "uname","password", "com.mysql.jdbc.Driver") 
sql.execute "select count(*) from TableName" 

私はこのエラーに

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod groovy.sql.Sql newInstance java.lang.String java.lang.String java.lang.String java.lang.String 
    at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectStaticMethod(StaticWhitelist.java:174) 
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onStaticCall(SandboxInterceptor.java:142) 
    at org.kohsuke.groovy.sandbox.impl.Checker$2.call(Checker.java:180) 
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedStaticCall(Checker.java:177) 
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:91) 
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:15) 
    at WorkflowScript.run(WorkflowScript:3) 
    at ___cps.transform___(Native Method) 
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:55) 
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:106) 
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:79) 
    at sun.reflect.GeneratedMethodAccessor841.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72) 
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21) 
    at com.cloudbees.groovy.cps.Next.step(Next.java:58) 
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154) 
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:19) 
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33) 
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30) 
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108) 
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30) 
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:164) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:277) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$000(CpsThreadGroup.java:77) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:186) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:184) 
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:47) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112) 
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:745) 
Finished: FAILURE 

Plsのヘルプを取得しています。前もって感謝します。スクリプトで実行することができますどのような制限がたくさんある。その場合には、サンドボックスモードでスクリプトを実行しているように見えるエラーから

import groovy.sql.Sql 


def sql = Sql.newInstance("jdbc:mysql://myIP:3306/dbName", "uname","password", "com.mysql.jdbc.Driver") 
sql.execute "select count(*) from TableName" 

答えて

0

は、SQLだけのパッケージをインポートします。たとえば、次の文句を言わない作業でもない:

echo binding.hasVariable("test") 

あなたができること:あなたはジェンキンスをホスティングていない場合は、パイプラインのスクリプト自体から多くを行うことはできません自分

  • 、あなたがする必要はあり別のプロセスを作成してselectを実行し、その結果をパイプラインスクリプトに渡します。これは、スレーブにGroovyスクリプトを追加し、パイプラインから実行することで可能です。

以下のすべてのシナリオでは、ジェンキンスをホースしていることを前提と自分

  • あなたはサンドボックスモードでスクリプトないを実行するオプションを持っているPipeline script定義を使用している場合(ジョブ設定でUse Groovy Sandboxのチェックを外しますページ)。この場合、の場合はの管理者になります。あなたは管理者ていない場合は、Pipeline script from SCM定義を使用している場合

  • 下のアドバイスに従う、スクリプトは​​モードで実行され、あなたはまた、あなたが投稿エラーが発生します。上記は、ジェンキンスバージョンでテストされました:その後、管理者は

注意を(ちょうどApproveボタンを押してください)ジェンキンス»インプロセススクリプトの承認を管理に移動し、拒否されたメソッド呼び出しを承認する必要があります2.7 .1

+0

エラーとインポートステートメントで私の質問が更新されました。 Groovy.sql.sqlのインポートは動作しませんでした:-( – Peter

2

+1

Gergelyに感謝しますが、私はJenkinsにはそれほど親しみがありません。私はPipelineで作成しました。> 'Configure'ページに移動します> (Build Now)を使用して実行している間に、ジョブのコンフィグレーションページのGroovy Sandboxオプションがチェックされていません。このエラー java.lang.ClassNotFoundException:com.mysql.jdbc.Driver – Peter

関連する問題