2017-06-12 6 views
0

をキャプチャ:Jenkinsfile宣言パイプライン - 私のような始まるJenkinsfileを書いて、すべてのチェックアウトSCM出力

pipeline { 
    agent { 
     label 'ec2-slave' 
    } 
    stages { 
     stage('Checkout') { 
      steps { 
       checkout scm 
      } 
     } 
... 

私はgitの関連を使用する必要があるので、私はcheckout scmコールの出力をキャプチャしたいと思います次のステップの情報。

もちろん、このステップは、このような出力を生成し、見事に動作します:

[Pipeline] { 
[Pipeline] stage 
[Pipeline] { (Declarative: Checkout SCM) 
[Pipeline] checkout 
> git rev-parse --is-inside-work-tree # timeout=10 
Fetching changes from the remote Git repository 
> git config remote.origin.url xxxxxxxxx # timeout=10 
Fetching upstream changes from xxxxxxxxx 
> git --version # timeout=10 
using GIT_SSH to set credentials jenkins ssh key 
> git fetch --tags --progress xxxxxxxxx +refs/heads/*:refs/remotes/origin/* 
Seen branch in repository origin/master 
Seen 2 remote branches 
> git tag -l # timeout=10 
Checking out Revision 10214b72d4c1f2c69444cc79a1af7ecc7f033349 (origin/master) 
> git config core.sparsecheckout # timeout=10 
> git checkout -f 10214b72d4c1f2c69444cc79a1af7ecc7f033349 
> git rev-list 10214b72d4c1f2c69444cc79a1af7ecc7f033349 # timeout=10 
[Pipeline] } 
[Pipeline] // stage 
[Pipeline] stage 
[Pipeline] { (Checkout) 
[Pipeline] checkout 
> git rev-parse --is-inside-work-tree # timeout=10 
Fetching changes from the remote Git repository 
> git config remote.origin.url xxxxxxxxx # timeout=10 
Fetching upstream changes from xxxxxxxxx 
> git --version # timeout=10 
using GIT_SSH to set credentials jenkins ssh key 
> git fetch --tags --progress xxxxxxxxx +refs/heads/*:refs/remotes/origin/* 
Seen branch in repository origin/master 
Seen 2 remote branches 
> git tag -l # timeout=10 
Checking out Revision 10214b72d4c1f2c69444cc79a1af7ecc7f033349 (origin/master) 
> git config core.sparsecheckout # timeout=10 
> git checkout -f 10214b72d4c1f2c69444cc79a1af7ecc7f033349 

は、キャプチャおよび/または前のパイプラインステップを参照の意図する方法はありますか?

+0

私はこのJenkinsfileをフリースタイルの構文から変換しています。私は、変数に 'sh'呼び出しを保存することで、前のステップを"参照 "することができました。 'checkout scm'を使って同じことを達成する方法がわかりません...単純な解決策だと確信していますが、GroovyとJenkinsfilesについてまだよく学んでいます – asdoylejr

+0

私は' script {} 'ブロックでこれを試してみましたが、これは奇妙な出力を生成しています: 'checkout scm' ' echo "$ {scm.dump()}" ' これは私のためにあまり役に立たない' null'値の束を持つマップを生成します。 – asdoylejr

答えて

1

あなたは、スクリプトステップを使用するOSにインストールのgitでチェックアウトを実行し、出力をキャプチャすることができます

script { 
    GIT_CLONE = sh (
    script: 'git clone xxxxxxxxx', 
    returnStdout: true 
).trim() 
    echo "git clone output: ${GIT_CLONE}" 
} 
+0

これは私の唯一の選択肢かもしれません。私はJenkinsfileの他の部分のスクリプトブロックに頼らざるを得ませんでしたが、このインスタンスでそうしなければならない方法があると思っていました。 – asdoylejr

1

だけでステージへの追加スクリプトステップを追加しました私がやってしまいました私はこれらのgit変数が必要でした。

 steps { 
      script { 
       GIT_COMMIT = sh (
        script: 'git rev-parse HEAD', 
        returnStdout: true 
       ).trim() 

       GIT_URL = sh (
        script: 'git config --get remote.origin.url', 
        returnStdout: true 
       ).trim() 
      } 
     } 
+0

URLについては、Jenkinsにチェックアウトする場所を教えてください。 jenkinsfileの先頭に環境変数を設定して、チェックアウトのステップでそれを使用し、必要な場所を他の場所で使用できるように思えます。 – 8bittree