如何从Jenkins的多分支管道中提取分支名称?

时间:2019-08-14 16:23:40

标签: jenkins svn groovy

所以,我知道Jenkins有一个包含分支名称的环境变量

sync() {
    console.log('inside sync function');
    console.log(this.state.location);

    const { projid } = this.state;
     const { client } = this.state;
      const { contractor } = this.state;
       const { title } = this.state;
        const { location } = this.state;
         const { desc } = this.state;
          const { unit } = this.state;
           const { drill } = this.state;
            const { logger } = this.state;

     //alert(user_name, user_contact, user_address);

     if (projid) {
       if (client) {
         if (contractor) {
           if (title) {
             if (location) {
               if (desc) {
                 if (unit) {
                   if (drill) {
                     if (logger) {
           db.transaction(function(tx) {
             tx.executeSql(
               'INSERT INTO projInfo (projectId,client,contractor,title,location,description,unit,drillers,loggers) VALUES (?,?,?,?,?,?,?,?,?)',
               [projid, client, contractor, title,location,desc,unit,drill,logger],
               (tx, results) => {
                 console.log('Results', results.rowsAffected);
                 if (results.rowsAffected > 0) {
                   Alert.alert(
                     'Success',
                     'Inserted Successfully',
                     [
                       {
                         text: 'Ok'
                       },
                     ],
                     { cancelable: false }
                   );
                 } else {
                   alert('Insert Failed');
                 }
               }
             );
           });
          } else {
             alert('Please fill loggers');
          }
            } else {
             alert('Please fill drillers');
           }
             }else {
              alert('Please fill unit');
            }
              } else {
              alert('Please fill description');
            }
              } else {
              alert('Please fill location');
            }
               } else {
              alert('Please fill title');
            }
               } else {
              alert('Please fill contractor');
            }
                } else {
              alert('Please fill client');
            }
                } else {
               alert('Please fill project ID');
            }
        }

但是,由于我的管道是一个多分支项目,因此我在这两个分支/ *,tags / *中进行扫描,因此环境变量看起来像这样:

env.BRANCH_NAME

branches/foo

我需要的只是foo或bar部分,而不是tag /或branchs /部分。 我尝试了一些字符串方法来提取出来,例如:

tags/bar

但是,当我稍后在构建脚本中尝试使用此变量时,会出现此异常。

def branch_name = env.BRANCH_NAME.replaceAll("(branches/|tags/)","")

如果任何人对如何执行此操作有任何想法,将不胜感激!

1 个答案:

答案 0 :(得分:0)

尝试

def branch_name = env.BRANCH_NAME.toString()。split(“ /”)[1]