Gitlab CI:api 使用变量时使用 axios 调用的触发器不起作用
Gitlab CI: api trigger with axios call not working when using variables
没有变量,服务器调用工作,gitlab 正在启动管道。
但是当我向那个调用添加变量时,它出错了:"variables needs to be a map of key-valued strings".
这是我的代码:
axios
.post(`https://gitlab.myurl.com/api/v4/projects/${projectId}/trigger/pipeline`, {
ref: branch,
token: token,
variables: { STAGING_AREA: 'testing1', NOTIFY_STATUS: true, SLACK_USER_ID: 'xxxxx' }
})
.then(res => {
console.log('pipeline started:', res.data.web_url);
})
.catch(error => {
console.error('errorMessage', error);
});
传递变量的正确语法是什么?
根据docs,可变参数的形式应该是variables[key]=value
。
并且该请求是多部分请求,因此您需要使用 FormData
。
试试 运行 这个代码。
const pipelineTriggerBody = new FormData();
pipelineTriggerBody.append('ref', 'master'); // branch name
pipelineTriggerBody.append('token', 'CI_TOKEN');
pipelineTriggerBody.append('variables[STAGING_AREA]', 'testing1');
pipelineTriggerBody.append('variables[NOTIFY_STATUS]', true);
pipelineTriggerBody.append('variables[SLACK_USER_ID]', 'xxxxx');
axios
.post(
`https://gitlab.myurl.com/api/v4/projects/${projectId}/trigger/pipeline`,
pipelineTriggerBody
)
.then(res => {
console.log('pipeline started:', res.data.web_url);
})
.catch(error => {
console.error('errorMessage', error);
});
我做错了一件事。
NOTIFY_STATUS: true
似乎true只能作为字符串传递:
NOTIFY_STATUS: 'true'
修改后我的代码工作正常。
没有变量,服务器调用工作,gitlab 正在启动管道。
但是当我向那个调用添加变量时,它出错了:"variables needs to be a map of key-valued strings".
这是我的代码:
axios
.post(`https://gitlab.myurl.com/api/v4/projects/${projectId}/trigger/pipeline`, {
ref: branch,
token: token,
variables: { STAGING_AREA: 'testing1', NOTIFY_STATUS: true, SLACK_USER_ID: 'xxxxx' }
})
.then(res => {
console.log('pipeline started:', res.data.web_url);
})
.catch(error => {
console.error('errorMessage', error);
});
传递变量的正确语法是什么?
根据docs,可变参数的形式应该是variables[key]=value
。
并且该请求是多部分请求,因此您需要使用 FormData
。
试试 运行 这个代码。
const pipelineTriggerBody = new FormData();
pipelineTriggerBody.append('ref', 'master'); // branch name
pipelineTriggerBody.append('token', 'CI_TOKEN');
pipelineTriggerBody.append('variables[STAGING_AREA]', 'testing1');
pipelineTriggerBody.append('variables[NOTIFY_STATUS]', true);
pipelineTriggerBody.append('variables[SLACK_USER_ID]', 'xxxxx');
axios
.post(
`https://gitlab.myurl.com/api/v4/projects/${projectId}/trigger/pipeline`,
pipelineTriggerBody
)
.then(res => {
console.log('pipeline started:', res.data.web_url);
})
.catch(error => {
console.error('errorMessage', error);
});
我做错了一件事。
NOTIFY_STATUS: true
似乎true只能作为字符串传递:
NOTIFY_STATUS: 'true'
修改后我的代码工作正常。