一、gitlab的token类型
一种是使用项目的trigger token,另一种是使用个人访问令牌。
1. trigger token
1 2 3 4 5 6 7 8 9 10 11 12 13 |
如果是使用trigger token,可能需要在项目的CI/CD设置里添加一个触发器,然后获取到token。这时候API端点可能不同,比如是/projects/{project_id}/trigger/pipeline,而参数包括token和ref。 比如,使用trigger token的话,curl命令可能像这样: curl -X POST \ -F token=TOKEN \ -F ref=master \ "https://gitlab.example.com/api/v4/projects/{project_id}/trigger/pipeline" 这时候不需要PRIVATE-TOKEN头,而是通过表单数据传递token。这时候可能需要先在项目的Settings -> CI/CD -> Pipeline triggers 那里创建一个触发器,然后拿到token。 |

2. 个人令牌
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
使用个人访问令牌的话,可能需要有api作用域的权限,然后在请求头里带上PRIVATE-TOKEN或者Authorization Bearer。这时候的端点是直接创建流水线,比如POST /projects/:id/pipeline,参数包括ref和其他变量。例如: curl -X POST \ -H "PRIVATE-TOKEN: <your_access_token>" \ -H "Content-Type: application/json" \ -d '{"ref": "master", "variables": [{"key": "ENV_VAR", "value": "value"}]}' \ "https://gitlab.example.com/api/v4/projects/{project_id}/pipeline" 这时候需要注意,可能参数需要用JSON格式,并且设置Content-Type为application/json。或者,使用表单数据的话,可能需要不同的参数传递方式。不过,这里可能存在混淆,因为GitLab API有两种触发方式:一种是通过Trigger Token,另一种是通过API使用个人访问令牌。需要确认这两种方式的区别和正确的调用方法。 比如,根据GitLab文档,使用触发器的方式需要在项目的CI/CD设置中生成一个触发器,然后使用该token来触发流水线。这时候的端点是POST /projects/:id/trigger/pipeline,参数是token和ref,以及其他变量。而使用个人访问令牌的话,端点可能是POST /projects/:id/pipeline,并且需要在头中提供认证信息,参数包括ref和variables。 这时候,我需要确定用户想要哪种方式。可能两种方法都可以,但步骤不同。比如,使用触发器的方式更适用于外部系统触发,而个人访问令牌需要用户有足够的权限。 |

二、使用token触发流水线(根据自己需求选择所需要的令牌)
python脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import requests import os # 配置参数(根据实际情况修改) GITLAB_URL = "https://gitlab.example.com" PROJECT_ID = "123" # 项目ID或URL编码的项目路径(如"my%2Fproject") BRANCH_NAME = "main" # 触发流水线的分支或Tag # ------------------------- 方法一:使用触发器Token ------------------------- def trigger_pipeline_with_trigger_token(token, variables=None): """ 使用触发器Token触发流水线 """ url = f"{GITLAB_URL}/api/v4/projects/{PROJECT_ID}/trigger/pipeline" data = { "token": token, "ref": BRANCH_NAME } # 添加自定义变量(可选) if variables: for key, value in variables.items(): data[f"variables[{key}]"] = value try: response = requests.post(url, data=data) response.raise_for_status() # 检查HTTP错误 return response.json() except requests.exceptions.RequestException as e: print(f"触发失败: {e}") return None # ------------------------- 方法二:使用个人访问令牌 ------------------------- def trigger_pipeline_with_personal_token(access_token, variables=None): """ 使用个人访问令牌触发流水线 """ url = f"{GITLAB_URL}/api/v4/projects/{PROJECT_ID}/pipeline" headers = { "PRIVATE-TOKEN": access_token, "Content-Type": "application/json" } payload = { "ref": BRANCH_NAME } # 添加自定义变量(可选) if variables: payload["variables"] = variables try: response = requests.post(url, headers=headers, json=payload) response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: print(f"触发失败: {e}") return None # ------------------------- 使用示例 ------------------------- if __name__ == "__main__": # 示例1:使用触发器Token trigger_token = "YOUR_TRIGGER_TOKEN" # 替换为你的触发器Token variables = { "ENV": "production", "VERSION": "1.0.0" } result = trigger_pipeline_with_trigger_token(trigger_token, variables) print("触发器Token结果:", result) # 示例2:使用个人访问令牌 personal_token = "YOUR_PERSONAL_TOKEN" # 替换为你的个人Token variables_json = { "ENV": "staging", "VERSION": "2.0.0" } result = trigger_pipeline_with_personal_token(personal_token, variables_json) print("个人Token结果:", result) |
- 本文固定链接: https://www.yoyoask.com/?p=12701
- 转载请注明: shooter 于 SHOOTER 发表