2017-12-25 13 views
1

Jenkinsスクリプトコンソールからパイプラインプラグイン操作をシミュレートすることができるのだろうかと思います。たとえば、コマンドでパイプラインの仕事を経由して通知を送信するために使用スラックプラグイン:Jenkinsスクリプトコンソールからスラックプラグインにアクセスする

slackSend(色:カラーコード、メッセージ:要約)

私が遊んで試してみて、オブジェクトとプロパティを見てみたいです。私はかなりJenkinsのスクリプトコンソールからgroovyで可能だと確信しています。

答えて

1

私は通常、ソースコードを見る必要があることを発見しました。この場合、ソースはGitHubのjenkinsci/slack-pluginにあります。ここ

私はどうなるステップである。"slackSend"ステップを記述するために使用される

で行われるsome properties that are injected

  • コードを見てから主要部I通知はStandardSlackServiceステップに提供し、おそらくグローバルジェンキンス構成からされたパラメータを使用して作成されることです。

    このコードは、スクリプトコンソールから実行する場合は、パイプラインパスと同じ方法で実行するのが難しいため、少しずつ調整して調整することができます。ここで

    は、私が参照するために、関連するコードと考えるものです:

    SlackService slackService = getSlackService(baseUrl, team, token, tokenCredentialId, botUser, channel); 
    boolean publishSuccess; 
    if(step.attachments != null){ 
        JsonSlurper jsonSlurper = new JsonSlurper(); 
        JSON json = null; 
        try { 
         json = jsonSlurper.parseText(step.attachments); 
        } catch (JSONException e) { 
         listener.error(Messages.NotificationFailedWithException(e)); 
         return null; 
        } 
        if(!(json instanceof JSONArray)){ 
         listener.error(Messages.NotificationFailedWithException(new IllegalArgumentException("Attachments must be JSONArray"))); 
         return null; 
        } 
        JSONArray jsonArray = (JSONArray) json; 
        for (int i = 0; i < jsonArray.size(); i++) { 
         Object object = jsonArray.get(i); 
         if(object instanceof JSONObject){ 
          JSONObject jsonNode = ((JSONObject) object); 
          if (!jsonNode.has("fallback")) { 
           jsonNode.put("fallback", step.message); 
          } 
         } 
        } 
        publishSuccess = slackService.publish(jsonArray, color); 
    }else{ 
        publishSuccess = slackService.publish(step.message, color); 
    } 
    if (!publishSuccess && step.failOnError) { 
        throw new AbortException(Messages.NotificationFailed()); 
    } else if (!publishSuccess) { 
        listener.error(Messages.NotificationFailed()); 
    } 
    // rest of method... 
    
    // ... 
    
    //streamline unit testing 
    SlackService getSlackService(String baseUrl, String team, String token, String tokenCredentialId, boolean botUser, String channel) { 
        return new StandardSlackService(baseUrl, team, token, tokenCredentialId, botUser, channel); 
    } 
    
  • 関連する問題