参数化构建这里可以添加选项参数,一些字符串的参数,非常的多。这些参数在流水线执行的时候,它会加入到流水线里面去,最终在流水线运行的时候调用这些参数,选完参数之后再去构建。
写一段groovy脚本,然后返回一个列表,其实也就是数组,之后就可以展示出来了。
所以上面就两种类型,一种是普通类型,一种是带有动作的。最终可以看到参数是需要去写脚本的。
这里面有三种类型,使用前面两种就足够了,都是动态类型的选项参数,都支持去运行一组脚本。
这里面写怎么样复杂的groovy脚本都是可以的,最终你将选项返回出来就可以了。
上面就是普通类型的,还有动态类型的。 还支持是否多选,单选。
动态类型还支持选择参数值,这里可以写关联参数的名称。在这里定义好了之后就可以在代码里面去引用它了。(动态类型根据普通类型的值变化而变化)
如果选ant gradle都没有反应,如果选maven就动态选择了。
if (buildTools .equals("maven")){
return ["mvn clean"]
}else if (buildTools.equals("ant")){
return ["ant"]
}else{
return ["gradle"]
}
实践
Branches API | GitLab
下面要完成实践,比如要选分支还是选tag,然后自动的将gitlab上面的标签拿下来。
其实最后的效果就是选择tag,那么就列出该项目下面所有的tag,选择branch就列出该项目下面的所有的branch。
接下来要调用gitlab的api接口了。
现在就是准备调用接口拿到项目的分支信息。
要调用api,那么第一步就需要去获取token。
拿到token之后,然后去gitlab看看接口如何调用。
Branches API | GitLab 找到api的分支,这里还有project_id
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/repository/branches"
[root@jenkins ~]# curl --header "PRIVATE-TOKEN: gL8YqsRqzmjfnZ34N7UR" "http://192.168.11.129/api/v4/projects/2/repository/branches"
[{"name":"master","commit":{"id":"45adcfde1668ea88ec76390b5298122bb175b5d5","short_id":"45adcfde","created_at":"2022-10-21T12:11:38.000+08:00","parent_ids":null,"title":"Update src/org/devops/test.groovy","message":"Update src/org/devops/test.groovy","author_name":"Administrator","author_email":"admin@example.com","authored_date":"2022-10-21T12:11:38.000+08:00","committer_name":"Administrator","committer_email":"admin@example.com","committed_date":"2022-10-21T12:11:38.000+08:00","web_url":"http://gitlab.devops.com/root/devops-library/-/commit/45adcfde1668ea88ec76390b5298122bb175b5d5"},"merged":false,"protected":true,"developers_can_push":false,"developers_can_merge":false,"can_push":true,"default":true,"web_url":"http://gitlab.devops.com/root/devops-library/-/tree/master"}][root@jenkins ~]#
上面就是通过curl或者postman去获取了项目的所有分支信息。后面就是用groovy写一个HTTP请求,然后放到Jenkins作业里面。
header里面要有key和value,所以在postman里面要加上header的信息。
这里使用url去发请求,发请求这里基本上就是设置method,选择请求的类型。之后设置请求头,也就是header里面的信息。最后将请求的信息返回。
返回的是json的map,最后就是去过滤数据就行了。
[
{
"name": "master",
"commit": {
"id": "45adcfde1668ea88ec76390b5298122bb175b5d5",
"short_id": "45adcfde",
"created_at": "2022-10-21T12:11:38.000+08:00",
"parent_ids": null,
"title": "Update src/org/devops/test.groovy",
"message": "Update src/org/devops/test.groovy",
"author_name": "Administrator",
"author_email": "admin@example.com",
"authored_date": "2022-10-21T12:11:38.000+08:00",
"committer_name": "Administrator",
"committer_email": "admin@example.com",
"committed_date": "2022-10-21T12:11:38.000+08:00",
"web_url": "http://gitlab.devops.com/root/devops-library/-/commit/45adcfde1668ea88ec76390b5298122bb175b5d5"
},
"merged": false,
"protected": true,
"developers_can_push": false,
"developers_can_merge": false,
"can_push": true,
"default": true,
"web_url": "http://gitlab.devops.com/root/devops-library/-/tree/master"
},
{
"name": "release-1.1.1",
"commit": {
"id": "45adcfde1668ea88ec76390b5298122bb175b5d5",
"short_id": "45adcfde",
"created_at": "2022-10-21T12:11:38.000+08:00",
"parent_ids": null,
"title": "Update src/org/devops/test.groovy",
"message": "Update src/org/devops/test.groovy",
"author_name": "Administrator",
"author_email": "admin@example.com",
"authored_date": "2022-10-21T12:11:38.000+08:00",
"committer_name": "Administrator",
"committer_email": "admin@example.com",
"committed_date": "2022-10-21T12:11:38.000+08:00",
"web_url": "http://gitlab.devops.com/root/devops-library/-/commit/45adcfde1668ea88ec76390b5298122bb175b5d5"
},
"merged": false,
"protected": false,
"developers_can_push": false,
"developers_can_merge": false,
"can_push": true,
"default": false,
"web_url": "http://gitlab.devops.com/root/devops-library/-/tree/release-1.1.1"
}
]
response.name是一个list对象。
pipeline {
agent any
stages {
stage('Hello') {
steps {
script{
response = sh returnStdout: true,
script: """
curl --location \
--request GET \
"http://192.168.11.129/api/v4/projects/2/repository/branches" \
--header "PRIVATE-TOKEN: gL8YqsRqzmjfnZ34N7UR"
"""
println(response)
response = readJSON text: "${response}"
println(response.name)
}
}
}
}
}
+ curl --location --request GET http://192.168.11.129/api/v4/projects/2/repository/branches --header 'PRIVATE-TOKEN: gL8YqsRqzmjfnZ34N7UR'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 1583 100 1583 0 0 21052 0 --:--:-- --:--:-- --:--:-- 21391
[Pipeline] echo
[{"name":"master","commit":{"id":"45adcfde1668ea88ec76390b5298122bb175b5d5","short_id":"45adcfde","created_at":"2022-10-21T12:11:38.000+08:00","parent_ids":null,"title":"Update src/org/devops/test.groovy","message":"Update src/org/devops/test.groovy","author_name":"Administrator","author_email":"admin@example.com","authored_date":"2022-10-21T12:11:38.000+08:00","committer_name":"Administrator","committer_email":"admin@example.com","committed_date":"2022-10-21T12:11:38.000+08:00","web_url":"http://gitlab.devops.com/root/devops-library/-/commit/45adcfde1668ea88ec76390b5298122bb175b5d5"},"merged":false,"protected":true,"developers_can_push":false,"developers_can_merge":false,"can_push":true,"default":true,"web_url":"http://gitlab.devops.com/root/devops-library/-/tree/master"},{"name":"release-1.1.1","commit":{"id":"45adcfde1668ea88ec76390b5298122bb175b5d5","short_id":"45adcfde","created_at":"2022-10-21T12:11:38.000+08:00","parent_ids":null,"title":"Update src/org/devops/test.groovy","message":"Update src/org/devops/test.groovy","author_name":"Administrator","author_email":"admin@example.com","authored_date":"2022-10-21T12:11:38.000+08:00","committer_name":"Administrator","committer_email":"admin@example.com","committed_date":"2022-10-21T12:11:38.000+08:00","web_url":"http://gitlab.devops.com/root/devops-library/-/commit/45adcfde1668ea88ec76390b5298122bb175b5d5"},"merged":false,"protected":false,"developers_can_push":false,"developers_can_merge":false,"can_push":true,"default":false,"web_url":"http://gitlab.devops.com/root/devops-library/-/tree/release-1.1.1"}]
[Pipeline] echo
------------------------------
[Pipeline] readJSON
[Pipeline] echo
[master, release-1.1.1]
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
最后或者你这样写也行,导入类,写成groovy脚本。
这样就可以将分支拿过去了,tag同理。
Jenkins其实本身就是Java包,里面也可以调用其现成的方法。token的值,怎么保存在Jenkins里面以及如何使用如下:
pipeline {
agent any
stages {
stage('Hello') {
steps {
script{
withCredentials([string(credentialsId: '81605426-8e30-4220-b421-0d2bfdfbe880', variable: 'token')]) {
response = sh returnStdout: true,
script: """
curl --location \
--request GET \
"http://192.168.11.129/api/v4/projects/2/repository/branches" \
--header "PRIVATE-TOKEN: ${token}"
"""
}
response = readJSON text: "${response}"
println(response.name)
}
}
}
}
}