前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >实践:使用JenkinsActive参数,让参数动起来~

实践:使用JenkinsActive参数,让参数动起来~

作者头像
DevOps云学堂
发布2021-09-07 16:17:10
1.2K0
发布2021-09-07 16:17:10
举报
文章被收录于专栏:DevOps持续集成DevOps持续集成
  • JenkinsActive参数概述
  • Postman调试GitLab接口
  • 实践:动态获取Git项目标签/分支
  • 实践: JenkinsCoreAPI获取凭据

参数化构建

在使用Pipeline项目时一般都是参数化构建作业,在Jenkins的构建时,可能需要使用参数类型有复选框,单选按钮,多选值等输入的情景。

ActiveChoice参数插件安装

转到→管理Jenkins→选择管理插件→选择可用选项卡,然后搜索主动选择插件。安装并重新启动Jenkins,以正确安装插件。我的已经安装好,因此在“已安装”标签中列出。

使用Groovy脚本,生成动态参数选项值列表。参数可以动态更新,呈现为组合框,复选框,单选按钮或丰富的HTMLUI窗口小部件。

这里的`return` 语句是什么意思呢? 选项参数的值其实是一个Array数组, 所以这里最终运行的函数返回要对应上。

当作业中已定义参数的值发生更改时,可以动态更新。这里可以使用IF进行条件判断,输出相关的值。

这里使用的是IF进行判断, 判断buildType这个参数的值是否匹配某个条件,然后返回对应的选项值。场景: 根据用户选择的构建工具不同,自动填充对应的构建命令。

调试GitLab接口

进入GitLab官方文档,找到API资源文档。 找到分支的接口;

可以使用curl或者postman进行调试

代码语言:javascript
复制
curl --request POST \
--header “PRIVATE-TOKEN: <your_access_token>” \  
"https://gitlab.example.com/api/v4/projects/5/repository/branches?branch=newbranch&ref=master"

实践: 动态获取GitLab项目分支和标签

未优化:

代码语言:javascript
复制
import groovy.json.JsonSlurper
JsonSlurper slurper = new JsonSlurper()

if (refType.equals("branch")){

  try {
    String apiServer = "https://gitlab.com/api/v4/"
    //String data = "branch=zeyang&ref=master";
    URL url = new URL(apiServer + "projects/18803707/repository/branches");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("PRIVATE-TOKEN", "DTyTRQ-ky1wZJ-zSSKRW");
    con.setDoOutput(true);
    // if (data != ""){
    //    con.getOutputStream().write(data.getBytes("UTF-8"));
    // }
    con.getInputStream();
    responseCode = con.getResponseCode()
    println(responseCode)
    response = slurper.parse(con.getInputStream())
    //println(slurper.parse(response))
    // return slurper.parse(response)
    refs = []
    //println(response.getClass())
    for(item in response) {
        refs.add(item.name)
     }
    return refs
  }
  catch(Exception e) {
    return []
  }
}


if (refType.equals("tag")){

  try {
    String apiServer = "https://gitlab.com/api/v4/"
    //String data = "branch=zeyang&ref=master";
    URL url = new URL(apiServer + "projects/18803707/repository/tags");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("PRIVATE-TOKEN", "DTyTRQ-ky1wZJ-zSSKRW");
    con.setDoOutput(true);
    // if (data != ""){
    //    con.getOutputStream().write(data.getBytes("UTF-8"));
    // }
    con.getInputStream();
    responseCode = con.getResponseCode()
    println(responseCode)
    response = slurper.parse(con.getInputStream())
    //println(slurper.parse(response))
    // return slurper.parse(response)
    refs = []
    //println(response.getClass())
    for(item in response) {
        refs.add(item.name)
     }
    return refs
  }
  catch(Exception e) {
    return []
  }
}

优化后:

  • 将凭据存储在Jenkins,并使用API获取;
  • 封装一个统一的HTTP请求函数;
代码语言:javascript
复制
import groovy.json.JsonSlurper
import java.util.ArrayList
import jenkins.*
import jenkins.model.* 
import hudson.*
import hudson.model.*


// 获取Jenkins凭据
def GetJenkinsCredentials(id, type){
   def jenkinsCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
        com.cloudbees.plugins.credentials.Credentials.class,
        Jenkins.instance,
        null,
        null
   );
   for (creds in jenkinsCredentials) {
      if(creds.id == id){
         if (type == "secret"){
            return creds.secret
         } 
      }
      
   }
}


// 发送请求
def HttpReq(data, method, apiUrl ){
   JsonSlurper slurper = new JsonSlurper()
   String gitlabToken = GetJenkinsCredentials("bba72a35-6857-4be6-9e71-cb6eed4db403/update", "secret")
   try {
      String apiServer = "https://gitlab.com/api/v4/"
      //String data = "branch=zeyang&ref=master";
      URL url = new URL(apiServer + apiUrl);
      HttpURLConnection con = (HttpURLConnection) url.openConnection();
      con.setRequestMethod(method);
      con.setRequestProperty("PRIVATE-TOKEN", gitlabToken);
      con.setDoOutput(true);
      if (data != ""){
         con.getOutputStream().write(data.getBytes("UTF-8"));
      }
      con.getInputStream();
      responseCode = con.getResponseCode()
      //println(responseCode)
      response = con.getInputStream()
      //println(slurper.parse(response))
      return slurper.parse(response)
   }
   catch(Exception e) {
      return []
   }
}
// 获取项目的标签列表
def GetTagList(){
   refs = []
   ArrayList response = HttpReq("", "GET", "projects/18803707/repository/tags" )
   //println(response.getClass())
   for(item in response) {
      refs.add(item.name)
   }
   return refs
}

// 获取项目的分支列表
def GetBranchList(type){
   refs = []
   ArrayList response = HttpReq("", "GET", "projects/18803707/repository/branches" )
   //println(response.getClass())
   for(item in response) {
      refs.add(item.name)
   }
   return refs
}

if (refType.equals("tag")){
   refs = GetTagList()
   return refs
}


if (refType.equals("branch")){
   refs = GetBranchList()
   return refs
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-08-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DevOps云学堂 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档