如何从Jenkins管道获取拉请求ID

时间:2017-01-17 11:08:01

标签: github jenkins-pipeline github-organizations

我正在尝试使用Jenkins管道使用Sonar分析我的源代码。要让Sonar通知Github结果,我需要指定Pull Request ID。

如何从Jenkins Pipelines获取此Pull Request ID?

我们正在使用GitHub Organization Folder Plugin来构建拉取请求,而不是GitHub pull request builder plugin。这就是$ghprbPullId不适合我的原因。有任何想法如何以不同的方式获取拉请求ID?

3 个答案:

答案 0 :(得分:20)

Jenkins公开了一个名为CHANGE_ID的全局变量:

  

对于某种类型的多分支项目   更改请求,这将设置为更改ID,例如拉   请求编号。

此变量仅为拉取请求构建填充,因此您必须禁用分支构建并在分支源的管道配置中启用PR构建:

enter image description here

我的管道步骤如下:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

struct TrieNode
{
char key; //value in node
struct TrieNode* child; //stores child of nodes
struct TrieNode* sibling; //stores sibling
};

struct TrieNode* TrieNodeConstructor(char key)
{
    struct TrieNode* node; //creates pointer for trie
    node = malloc(sizeof(struct TrieNode)); //dynamically assigns memory to trie

node->key = key; //sets value to input

//child and sibling are initially NULL
node->child = NULL;
node->sibling = NULL;

return node;
}


void TrieDestructor(struct TrieNode* node) //deletes node
{
    if (node->sibling != NULL) //checks if sibling node is empty
    {
        TrieDestructor(node->sibling); //frees sibling node
    }

    if (node->child != NULL) //checks if child node is empty
    {
        TrieDestructor(node->child); //frees child node
    }

    free(node); //frees node
}

void insert(struct TrieNode* node, char* str, int value)
{
    if(str[value]=='\0') //if input string is empty
    {
        node = TrieNodeConstructor('#'); //root node
        return;
    }

    if(node==NULL) //if node is empty
    {
        node = TrieNodeConstructor(str[value]); //recursion
        insert(node->child, str, value+1); //increments value and inserts
    }

    if(node->key == str[value]) //if the key matches the value
    {
        insert(node->child, str, value+1); //insert child below node
    }
    else //if the character is not already in the trie
    {
        insert(node->sibling, str, value+1); //it becomes a sibling
    }

}

int search(struct TrieNode* node, char* str, int value)
{
    if(node==NULL) //node is empty
    {
        return 0;
    }

    if(node->key=='#' && str[value]=='\0') //root node
    {
        return 1;
    }

    if((node->key!='#' &&  str[value]=='\0') || (node->key=='#' && str[value]!='\0')) //Empty
    {
        return 0;
    }

    if(node->key == str[value]) //if key matches with input
    {
        return search(node->child,str,value+1); //search next letter in word
    }
    else
    {
        return search(node->sibling,str,value); //searches through next letter
    }

    return 0;
}

答案 1 :(得分:2)

您可以通过例如env.BRANCH_NAME获取PR编号。

if (env.BRANCH_NAME.startsWith('PR-')) {
    def prNum = env.BRANCH_NAME.replace(/^PR-/, '')
    ...
}

答案 2 :(得分:2)

如果Thomas的回答不起作用或适用于您,您也可以(可能)使用分支名称通过查询Github REST API来获取Pull Request号。您只需要一个API令牌和分支名称,按日期更新DESC的顺序查找拉取请求,并找到与您的分支名称匹配的第一个PR。这将有拉动请求编号。

仅当每个拉取请求都有唯一的分支名称(例如JIRA签发票号)时,此方法才有效。