const jenkinsApi1 = axios.create({ baseURL: '/view', // Jenkins的地址 headers: { method: "GET", 'Content-Type': 'application/json' }, }); const jenkinsApi2 = axios.create({ baseURL: '/view', headers: { method: "POST", 'Content-Type': 'application/json', Authorization: `Basic ${btoa('jiancheng5.huang:11fa4770b79980ba20eecaa288fa15ce34')}` }, }); export default { getJobInfo(jobName: string) { return jenkinsApi1.get(`/view/yanting1.lin/job/${jobName}/2/api/json?pretty=true&tree=url,displayName,actions[parameters[name,value]],previousBuild[url]`); }, getJobInfo1() { return jenkinsApi1.get(`/view/yanting1.lin/job/build_test/api/json?pretty=true&tree=property[parameterDefinitions[defaultParameterValue[name,value]]]`); }, buildWithParameters(jobName: string, parameters: any) { return jenkinsApi2.post(`/view/yanting1.lin/job/${jobName}/buildWithParameters`, parameters) } };const names2 = ref([]) const values2 = ref([]); const fetchJobInfo2 = async () => { try { const response = await jenkinsApi1.getJobInfo1(); const parameterDefinitions = response.data.property[5].parameterDefinitions const defaultParameterValue = parameterDefinitions.map(param => param.defaultParameterValue) names2.value = defaultParameterValue.map(param => param.name) values2.value = defaultParameterValue.map(param => param.value) console.log(defaultParameterValue); console.log(names2); } catch (error) { console.log(error); } }; const buildJob = async () => { const parameters = {}; names2.value.forEach((name, index) => { parameters[name] = values2.value[index]; console.log(JSON.stringify(parameters)) }); // try { // await jenkinsApi2.buildWithParameters('build_test', JSON.stringify(parameters)); // console.log('构建已启动'); // } catch (error) { // console.log(error); // } }; fetchJobInfo2(); <el-form label-width="120px"> <el-form-item v-for="(name, index) in names2" :key="index" :label="name"> <el-input v-model="values2[index]" /> </el-form-item> <el-button type="primary" @click="buildJob"> 确认 </el-button> </el-form>在这段代码中怎么实现将在el-input输入的参数传给Jenkins并根据这些参数启动构建
时间: 2024-03-12 10:47:26 浏览: 161
在这段代码中,可以通过将el-input中的参数值保存到values2数组中,然后将这些参数值作为参数传递给jenkinsApi2的buildWithParameters方法来实现将参数传递给Jenkins并根据这些参数启动构建。以下是一个示例代码:
```
const names2 = ref([]);
const values2 = ref([]);
const buildJob = async () => {
const parameters = {};
names2.value.forEach((name, index) => {
parameters[name] = values2.value[index];
});
try {
await jenkinsApi2.buildWithParameters('build_test', parameters);
console.log('构建已启动');
} catch (error) {
console.log(error);
}
};
<el-form label-width="120px">
<el-form-item v-for="(name, index) in names2" :key="index" :label="name">
<el-input v-model="values2[index]" />
</el-form-item>
<el-button type="primary" @click="buildJob">
确认
</el-button>
</el-form>
```
在上面的示例代码中,我们遍历names2数组,将每个参数名称和对应的参数值存储在parameters对象中,然后将这个参数对象传递给jenkinsApi2的buildWithParameters方法来触发Jenkins的构建。在el-input中输入的参数值会被保存在values2数组中。
阅读全文