Jnekins Active动态参数 集成Gitlab实践

参数化构建这里可以添加选项参数,一些字符串的参数,非常的多。这些参数在流水线执行的时候,它会加入到流水线里面去,最终在流水线运行的时候调用这些参数,选完参数之后再去构建。

写一段groovy脚本,然后返回一个列表,其实也就是数组,之后就可以展示出来了。

所以上面就两种类型,一种是普通类型,一种是带有动作的。最终可以看到参数是需要去写脚本的。

这里面有三种类型,使用前面两种就足够了,都是动态类型的选项参数,都支持去运行一组脚本。

这里面写怎么样复杂的groovy脚本都是可以的,最终你将选项返回出来就可以了。

上面就是普通类型的,还有动态类型的。 还支持是否多选,单选。

动态类型还支持选择参数值,这里可以写关联参数的名称。在这里定义好了之后就可以在代码里面去引用它了。(动态类型根据普通类型的值变化而变化

如果选ant gradle都没有反应,如果选maven就动态选择了。

if (buildTools .equals("maven")){
  return ["mvn clean"]
}else if (buildTools.equals("ant")){
  return ["ant"]
}else{
 return ["gradle"]
}

认证鉴权(补充知识点 和文章无关)


调用接口有如下两种认证方式,您可以选择其中一种进行认证鉴权。

  • Token认证:通过Token认证调用请求。

  • AK/SK认证:通过AK(Access Key ID)/SK(Secret Access Key)加密调用请求。推荐使用AK/SK认证,其安全性比Token认证要高。

Token认证

说明:

Token在计算机系统中代表令牌(临时)的意思,拥有Token就代表拥有某种权限。Token认证就是在调用API的时候将Token加到请求消息头,从而通过身份认证,获得操作API的权限。

获取Token后,再调用其他接口时,您需要在请求消息头中添加“X-Auth-Token”,其值即为Token。例如Token值为“ABCDEFJ....”,则调用接口时将“X-Auth-Token: ABCDEFJ....”加到请求消息头即可,如下所示。

POST https://apig.cn-north-1.myhuaweicloud.com/v2/{project_id}/apigw/instances/{instance_id}/api-groups
Content-Type: application/json
X-Auth-Token: ABCDEFJ....

AK/SK认证

说明:

AK/SK签名认证方式仅支持消息体大小12MB以内,12MB以上的请求请使用Token认证。

AK/SK认证就是使用AK/SK对请求进行签名,在请求时将签名信息添加到消息头,从而通过身份认证。

  • AK(Access Key ID):访问密钥ID。与私有访问密钥关联的唯一标识符;访问密钥ID和私有访问密钥一起使用,对请求进行加密签名。
  • SK(Secret Access Key):与访问密钥ID结合使用的密钥,对请求进行加密签名,可标识发送方,并防止请求被修改。

调用API(补充知识点 和文章无关)


使用接口测试工具配置调用信息。

  1. 获取API请求信息。

    为简单起见,此处通过线下传递方式获取API及文档。API调用者可以从中获取API认证方式,请求方法,请求路径等信息。

  2. 增加Header参数名称:X-Apig-AppCode,参数值填​​​​​​​已生成的AppCode
  3. 增加Header参数名称:x-stage,参数值为运行环境。若API发布到RELEASE环境不需执行此步骤。
  4. 单击“Send”发送请求。

    调用成功后,显示“200 OK”。

实践


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":"[email protected]","authored_date":"2022-10-21T12:11:38.000+08:00","committer_name":"Administrator","committer_email":"[email protected]","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": "[email protected]",
            "authored_date": "2022-10-21T12:11:38.000+08:00",
            "committer_name": "Administrator",
            "committer_email": "[email protected]",
            "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": "[email protected]",
            "authored_date": "2022-10-21T12:11:38.000+08:00",
            "committer_name": "Administrator",
            "committer_email": "[email protected]",
            "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":"[email protected]","authored_date":"2022-10-21T12:11:38.000+08:00","committer_name":"Administrator","committer_email":"[email protected]","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":"[email protected]","authored_date":"2022-10-21T12:11:38.000+08:00","committer_name":"Administrator","committer_email":"[email protected]","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)
                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34556414/article/details/127945473
今日推荐