首页 > 解决方案 > 如何从 Jenkins 传入字符串并在 JavaScript 中转换为 Int

问题描述

一个 JS 新手 ~ 只是想将 Jenkins 的值传递到管道脚本中,然后将其转换为 Int。

管道片段

 parameters {
        string(name: 'TIMES_TO_LOOP', defaultValue: ['1'], description: 'Number of times to loop')

javascript 片段*

let timesToLoopThrough = 1; //set a default value

timesToLoopThrough = timesToLoopThrough.parseInt(agreementsToGenerate); //reset to value passed in

//use timesToLoopThrough in subsequent code

错误

TypeError: timesToLoopThrough.parseInt is not a function

标签: javascriptjenkins

解决方案


您想在类上调用 parseInt,而不是在实例上:

timesToLoopThrough = Number.parseInt(agreementsToGenerate);

或者干脆

timesToLoopThrough = parseInt(agreementsToGenerate);

如果 parseInt() 在全局范围内

文档在这里


推荐阅读