0

私たちはCIセットアップにJenkins Job DSLを使用します。従来のJenkinsfile構文でのみ利用可能なspecial commandを使用しているため、pipeline jobを使用する必要があります。環境変数()がJenkinsジョブDSLおよびパイプラインジョブで機能しない

パイプラインジョブの中で私たちはGitからプロジェクトをチェックアウトします。複数のプロジェクトにパイプラインジョブを使用しているので、git urlをパイプラインスクリプトに挿入したいと考えています。

これは、パイプラインのジョブを生成する私たちのスクリプトの短いバージョンです:

def createPipelineJob(def jobName, def gitUrl) { 
    pipelineJob(jobName) { 
     environmentVariables(GIT_URL: gitUrl) 
     definition { 
      cps { 
       script(''' 
        node { 
         sh 'env | sort' 
        } 
       ''') 
       sandbox(true) 
      } 
     }  
    } 
} 

これは、次のXML設定ファイルを作成:

<flow-definition> 
    <actions/> 
    <description/> 
    <keepDependencies>false</keepDependencies> 
    <properties> 
     <EnvInjectJobProperty> 
      <info> 
       <propertiesContent>GIT_URL=my-git.url</propertiesContent> 
       <loadFilesFromMaster>false</loadFilesFromMaster> 
      </info> 
      <on>true</on> 
      <keepJenkinsSystemVariables>true</keepJenkinsSystemVariables> 
      <keepBuildVariables>true</keepBuildVariables> 
      <overrideBuildParameters>false</overrideBuildParameters> 
      <contributors/> 
     </EnvInjectJobProperty> 
    </properties> 
    <triggers/> 
    <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition"> 
     <script> 
      node { sh 'env | sort' } 
     </script> 
     <sandbox>true</sandbox> 
    </definition> 
</flow-definition> 

をしかし、私はこれを実行する場合、GIT_URL環境変数ではありません(他の環境変数があります)。しかし、代わりにこのsetupで手動でパイプラインジョブを作成すると、GIT_URL環境変数が正しく表示されます。我々はジェンキンスの新機能で、この問題は、今日のために私達を保持しているので、我々はかなり失われ

<flow-definition plugin="[email protected]"> 
    <actions> 
     <io.jenkins.blueocean.service.embedded.BlueOceanUrlAction plugin="[email protected]"> 
      <blueOceanUrlObject class="io.jenkins.blueocean.service.embedded.BlueOceanUrlObjectImpl"> 
       <mappedUrl>blue/organizations/jenkins/test-jobname</mappedUrl> 
       <modelObject class="flow-definition" reference="../../../.."/> 
      </blueOceanUrlObject> 
     </io.jenkins.blueocean.service.embedded.BlueOceanUrlAction> 
    </actions> 
    <description/> 
    <keepDependencies>false</keepDependencies> 
    <properties> 
     <com.sonyericsson.rebuild.RebuildSettings plugin="[email protected]"> 
      <autoRebuild>false</autoRebuild> 
      <rebuildDisabled>false</rebuildDisabled> 
     </com.sonyericsson.rebuild.RebuildSettings> 
     <EnvInjectJobProperty plugin="[email protected]"> 
      <info> 
       <propertiesContent>GIT_URL=my-git.url</propertiesContent> 
       <secureGroovyScript plugin="[email protected]"> 
       <script/> 
       <sandbox>false</sandbox> 
      </secureGroovyScript> 
      <loadFilesFromMaster>false</loadFilesFromMaster> 
     </info> 
     <on>true</on> 
     <keepJenkinsSystemVariables>true</keepJenkinsSystemVariables> 
     <keepBuildVariables>true</keepBuildVariables> 
     <overrideBuildParameters>false</overrideBuildParameters> 
    </EnvInjectJobProperty> 
     <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty> 
      <triggers/> 
     </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty> 
    </properties> 
    <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="[email protected]"> 
     <script> 
      node { sh 'env | sort' } 
     </script> 
     <sandbox>true</sandbox> 
    </definition> 
    <triggers/> 
    <disabled>false</disabled> 
</flow-definition> 

ジョブを作成すると、手動でほとんど同じXML構成を作成します。

編集:

  • ジョブがジェンキンスマスターノード上で生成が、実行されるスレーブノードに
  • ジェンキンス2.37.3
  • 環境インジェクタプラグイン2.1.5
  • パイプライン2.5

答えて

0

Environment Injector Pluginが正常にインストールされていないことが判明したため、スクリプトが正しく実行されませんでした。だから、私がやらなければならなかったことは、Jenkinsを再起動することだけでした。 Javier Garcésに感謝して、私のスクリプトが本当に正しいことを確認しました。

0

これは答えより多くのコメントですが、私はあなたのDSLコードを変更してテストしました。正常に動作します。

私は、スクリプトを使用してDSLジョブを作成:

def createPipelineJob(def jobName, def gitUrl) { 
    pipelineJob(jobName) { 
     environmentVariables(GIT_URL: gitUrl) 
     definition { 
      cps { 
       script(''' 
        node { 
         sh "echo $GIT_URL" 
        } 
       ''') 
       sandbox(true) 
      } 
     }  
    } 
} 
createPipelineJob('new-job-2','my-git.url') 

たパイプラインの仕事はあなたが投稿1(マイナスシェルスクリプト)と同じXMLを持っており、パイプラインジョブを構築するGIT_URLの値を出力します。

[new-job-1] Running shell script 
+ echo my-git.url 
my-git.url 

私の推薦:

  • あなたが投稿(または多分鉱山を試してみてください)ショートバージョンが動作しない場合、私はアップグレードジェンキンスやプラグインは、任意の違いをかどうかを確認しようとするだろう。
  • あなたが投稿した短いバージョンが動作する場合は、フルバージョンを投稿してください。おそらくそこにエラーがあります。
+0

あなたのコードを使ってジョブを生成しましたが、ジョブを開始するときに例外が発生します: 'groovy.lang.MissingPropertyException:そのようなプロパティはありません:class:groovy.langのGIT_URL。Binding' jenkinsインストールのバージョンと、元の質問にすべてのプラグインのバージョンが含まれます。しかし、限り、私は彼らがすべて最新のものだと言うことができます。また、スレーブ上で実行されている間にマスタノード上でジョブを生成しています。これは問題なのでしょうか? –

関連する問題