jenkins pipeline封装api请求调用gitlab
来源:原创
时间:2021-03-02
作者:脚本小站
分类:Jenkins
先设置gitlab的token全局变量:
gitlab中:
右上角用户名 > Settings > Access Tokens > 填写名称勾选Scopes,创建,然后在页面上面可以看到token。
jenkins中:
Jenkins > 凭据 > 系统 > 全局凭据 (unrestricted) > 添加凭据 > 类型(Secret text)> 粘贴token,填写名称,创建即可。
封装好的gitlab api请求方法:
package org.devops //封装HTTP请求 def HttpReq(reqType,reqUrl,reqBody){ def gitServer = "http://192.168.0.26/api/v4" withCredentials([string(credentialsId: 'gitlab-token', variable: 'gitlabToken')]) { result = httpRequest customHeaders: [[maskValue: true, name: 'PRIVATE-TOKEN', value: "${gitlabToken}"]], httpMode: reqType, contentType: "APPLICATION_JSON", consoleLogResponseBody: true, ignoreSslErrors: true, requestBody: reqBody, url: "${gitServer}/${reqUrl}" //quiet: true } return result } //更新文件内容 def UpdateRepoFile(projectId,filePath,fileContent){ apiUrl = "projects/${projectId}/repository/files/${filePath}" reqBody = """{"branch": "master","encoding":"base64", "content": "${fileContent}", "commit_message": "update a new file"}""" response = HttpReq('PUT',apiUrl,reqBody) println(response) } //获取文件内容,ProjectId可以在项目页面看到,页面搜索"Project ID" def GetRepoFile(projectId,filePath){ apiUrl = "projects/${projectId}/repository/files/${filePath}/raw?ref=master" response = HttpReq('GET',apiUrl,'') return response.content } //创建仓库文件 def CreateRepoFile(projectId,filePath,fileContent){ apiUrl = "projects/${projectId}/repository/files/${filePath}" reqBody = """{"branch": "master","encoding":"base64", "content": "${fileContent}", "commit_message": "create a new file"}""" response = HttpReq('POST',apiUrl,reqBody) println(response) } //更改提交状态 def ChangeCommitStatus(projectId,commitSha,status){ commitApi = "projects/${projectId}/statuses/${commitSha}?state=${status}" response = HttpReq('POST',commitApi,'') println(response) return response } //获取项目ID def GetProjectID(repoName='',projectName){ projectApi = "projects?search=${projectName}" response = HttpReq('GET',projectApi,'') def result = readJSON text: """${response.content}""" for (repo in result){ // println(repo['path_with_namespace']) if (repo['path'] == "${projectName}"){ repoId = repo['id'] println(repoId) } } return repoId } //删除分支 def DeleteBranch(projectId,branchName){ apiUrl = "/projects/${projectId}/repository/branches/${branchName}" response = HttpReq("DELETE",apiUrl,'').content println(response) } //创建分支 def CreateBranch(projectId,refBranch,newBranch){ try { branchApi = "projects/${projectId}/repository/branches?branch=${newBranch}&ref=${refBranch}" response = HttpReq("POST",branchApi,'').content branchInfo = readJSON text: """${response}""" } catch(e){ println(e) } //println(branchInfo) } //创建合并请求 def CreateMr(projectId,sourceBranch,targetBranch,title,assigneeUser=""){ try { def mrUrl = "projects/${projectId}/merge_requests" def reqBody = """{"source_branch":"${sourceBranch}", "target_branch": "${targetBranch}","title":"${title}","assignee_id":"${assigneeUser}"}""" response = HttpReq("POST",mrUrl,reqBody).content return response } catch(e){ println(e) } } //搜索分支 def SearchProjectBranches(projectId,searchKey){ def branchUrl = "projects/${projectId}/repository/branches?search=${searchKey}" response = HttpReq("GET",branchUrl,'').content def branchInfo = readJSON text: """${response}""" def branches = [:] branches[projectId] = [] if(branchInfo.size() ==0){ return branches } else { for (branch in branchInfo){ //println(branch) branches[projectId] += ["branchName":branch["name"], "commitMes":branch["commit"]["message"], "commitId":branch["commit"]["id"], "merged": branch["merged"], "createTime": branch["commit"]["created_at"]] } return branches } } //允许合并 def AcceptMr(projectId,mergeId){ def apiUrl = "projects/${projectId}/merge_requests/${mergeId}/merge" HttpReq('PUT',apiUrl,'') }
pipeline:
stage("deployment"){ steps{ script{ // 获取文件 content = gitlab.GetRepoFile("5","Dockerfile") println(content) response = k8s.GetDeployment("kube-system", "coredns") response = response.content base64Content = response.bytes.encodeBase64().toString() // 上传文件 content = gitlab.CreateRepoFile(2,"yaml%2fcoredns.yaml",base64Content) } } }
Project ID:
gitlab官方文档:
https://docs.gitlab.com/ee/api/repository_files.html#create-new-file-in-repository