在 Jenkins 管道中执行 npm 脚本(带变量)

Execute an npm-script (with variables) in a Jenkins pipeline

詹金斯 2.107.3

我有一个管道 jenkins 作业来使用 npm 运行

执行下面的脚本

脚本

{
  "name": "jenkins-postman-automation",
  "version": "1.0.0",
  "description": "test project",
  "directories": {
    "test": "01-TNR-CLIMPLUS-NEWMAN"
  },
  "scripts": {
    "api-tests": "newman run 01-TNR-NEWMAN/01-TNR-CORE.postman_collection.json -e 99-Environnements/${Environnement} "
  },
  "author": "Reda",
  "dependencies": {
    "newman": "^3.5.2"
  }
}

管道:

node(){
  stage("testing"){
    deleteDir()

    echo 'git clone'
    git branch: 'master', url: 'git@git.ci/postman.git'
    sh "npm install"
    try {
      sh "npm run api-tests" 
    } catch (Exception err) {
      currentBuild.result = 'UNSTABLE'
    }
  }
}

两种情况:

  1. 案例成功:当我声明 ${Environnement} 为作业参数时
  2. 案例失败:当我在工作def Environnement = "DEV"中声明${Environnement}时,出现以下错误:
unable to read data from file "-e"
ENOENT: no such file or directory, open '-e'
  1. 我的问题:我想在我的工作中使用一个变量,所以我该怎么做,请帮忙!

它仅在我使用声明性管道时有效,但现在无效。

def Environnement = "DEV" 更改为 env.Environnement="DEV"。 env.xxx 会将值设置到 shell 上下文的环境变量中,因此 npm run api-tests 可以从 shell 上下文中获取值。