2016-04-05 5 views
36

私は複数のステージを持つJenkinsfileを持っていますが、そのうちの1つは事実、別のジョブ(展開するもの)であり、場合によっては失敗することがあります。Jenkinsパイプラインで失敗したステージの再試行オプションを実装するにはどうすればよいですか?

私はJenkinsfileを使ってプロンプトを出すことができますが、実際にこのジョブの再試行メカニズムを実装する方法はわかりません。

失敗したステージをクリックして再試行することができます。 jenkins-pipelines-with-stages

+1

この機能の要望は[JENKINS-33846](https://issues.jenkins-ci.org/browse/JENKINS-33846)です。それは(dissapointingly)[JENKINS-45455](https://issues.jenkins-ci.org/browse/JENKINS-45455)の宣言型パイプライン用にのみ選択されています。 – mkobit

答えて

22

あなたは誰も検証していない場合、それは終了したい場合は 何かその

stage('deploy-test') { 
    try { 
    build 'yourJob' 
    } catch(error) { 
    echo "First build failed, let's retry if accepted" 
    retry(2) { 
     input "Retry the job ?" 
     build 'yourJob' 
    } 
    } 
} 

ようにあなたも入力のタイムアウトを使用することができることを行うには、リトライ+入力を結合することができるはずです。 そこ役に立つかもしれないこともWaitUntilによりですが、私はまだそれを使用していない

編集:

stage('deploy-test') { 
    waitUntil { 
    try { 
     build 'yourJob' 
    } catch(error) { 
     input "Retry the job ?" 
     false 
    } 
    } 
} 
: WaitUntilにより、あなたはそれを少しそれのようなものがクリーンであるを果たすべき間違いなく最高のようです

ところで、ここにはすべての手順が書かれていますhttps://jenkins.io/doc/pipeline/steps

+0

リトライプロンプトを追加しますか?私は疑う。 – sorin

+0

あなたは正しいです。私はそれについて私の答えを更新します! – fchaillou

+1

リトライ部分に対してのみタイムアウトを有効にすることはできますか?私は仕事のタイムアウトを変えたいかもしれません。私は良い解決策としてブロッキング・ジョブを見つけられなかったので、答えをまだ受け入れていませんでした。理想的には、再試行オプションはジョブが完了した後でなければなりません。このジョブがPR上のGitHubフックによってトリガーされると想像してください。私は、エラーの場合には無回答ではなく、GitHub上の失敗を見たいと思う。 – sorin

2

この機能を実装しようとしているときに見つけた選択肢の1つです。 https://gist.github.com/beercan1989/b66b7643b48434f5bdf7e1c87094acb9

私のニーズに合わせて再試行または中止した共有ライブラリのメソッドに変更されました。また、max retriesを追加し、timeout変数を作成して、必要なジョブやステージに応じて変更できるようにしました。

package com.foo.bar.jenkins 

def class PipelineHelper { 
    def steps 

    PipelineHelper(steps) { 
     this.steps = steps 
    } 

    void retryOrAbort(final Closure<?> action, int maxAttempts, int timeoutSeconds, final int count = 0) { 
     steps.echo "Trying action, attempt count is: ${count}" 
     try { 
      action.call(); 
     } catch (final exception) { 
      steps.echo "${exception.toString()}" 
      steps.timeout(time: timeoutSeconds, unit: 'SECONDS') { 
       def userChoice = false 
       try { 
        userChoice = steps.input(message: 'Retry?', ok: 'Ok', parameters: [ 
          [$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Check to retry from failed stage']]) 
       } catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) { 
        userChoice = false 
       } 
       if (userChoice) { 
        if (count <= maxAttempts) { 
         steps.echo "Retrying from failed stage." 
         return retryOrAbort(action, maxAttempts, timeoutMinutes, count + 1) 
        } else { 
         steps.echo "Max attempts reached. Will not retry." 
         throw exception 
        } 
       } else { 
        steps.echo 'Aborting' 
        throw exception; 
       } 
      } 
     } 
    } 
} 

入力の60秒間を待機する最大2回の再試行を使用する使用例。

def pipelineHelper = new PipelineHelper(this) 

stage ('Retry Example'){ 
    pipelineHelper.retryOrAbort({ 
     node{ 
      echo 'Here is an example' 
      throw new RuntimeException('This example will fail.') 
     } 
    }, 2, 60) 
} 

だけ入力を待っていることはexecutorを妨げないように閉鎖の内部のノードを置くことを忘れないでください。

有償のJenkinsエンタープライズCloudbeesには、これをよりうまく処理できるCheckpointプラグインがありますが、オープンソースJenkins(JENKINS-33846)のリリース予定はありません。

関連する問題