参数化构建
在使用Pipeline项目时一般都是参数化构建作业,在Jenkins的构建时,可能需要使用参数类型有复选框,单选按钮,多选值等输入的情景。
ActiveChoice参数插件安装
转到→管理Jenkins→选择管理插件→选择可用选项卡,然后搜索主动选择插件。安装并重新启动Jenkins,以正确安装插件。我的已经安装好,因此在“已安装”标签中列出。
使用Groovy脚本,生成动态参数选项值列表。参数可以动态更新,呈现为组合框,复选框,单选按钮或丰富的HTMLUI窗口小部件。
这里的`return` 语句是什么意思呢? 选项参数的值其实是一个Array数组, 所以这里最终运行的函数返回要对应上。
当作业中已定义参数的值发生更改时,可以动态更新。这里可以使用IF进行条件判断,输出相关的值。
这里使用的是IF进行判断, 判断buildType这个参数的值是否匹配某个条件,然后返回对应的选项值。场景: 根据用户选择的构建工具不同,自动填充对应的构建命令。
调试GitLab接口
进入GitLab官方文档,找到API资源文档。 找到分支的接口;
可以使用curl或者postman进行调试
curl --request POST \
--header “PRIVATE-TOKEN: <your_access_token>” \
"https://gitlab.example.com/api/v4/projects/5/repository/branches?branch=newbranch&ref=master"
实践: 动态获取GitLab项目分支和标签
未优化:
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 []
}
}
优化后:
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
}