You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins凭证含'=$'符号传递至Cypress测试丢失字符的解决方法

问题根源

你的凭证中包含$Za,在Jenkins的sh步骤(三重双引号块)中,Shell会将$Za识别为未定义的环境变量,直接替换为空字符串,导致这部分字符丢失。

修复方案

以下两种方法均可解决问题,任选其一即可:

方法一:通过Shell变量中转,用单引号保护原凭证值

先将Jenkins凭证变量赋值给Shell变量,用单引号包裹避免Shell解析其中的$,再传递给Cypress:

stage('StageName') {
  steps {
    withCredentials([
      string(credentialsId: 'Client_Secret_D4_Backoffice_Admin', variable: 'Client_Secret_D4_Backoffice_Admin')
    ]) {
      sh """
        # 用单引号包裹变量值,阻止Shell解析$字符
        export ADMIN_SECRET='${Client_Secret_D4_Backoffice_Admin//\'/\\\'}'
        npx cypress-tags run -e TAGS='@release',clientId=.......,backofficeAdminPassword=\"\$ADMIN_SECRET\" --browser chrome --config-file D4-CDS-stage-cypress.config.ts
      """
    }
  }
}
  • ${Client_Secret_D4_Backoffice_Admin//\'/\\\'}是Jenkins字符串替换语法,将凭证内的单引号转义为\',防止Shell单引号被提前闭合。
  • 传递给Cypress时用\"\$ADMIN_SECRET\",确保Shell将变量值作为字面量传递。

方法二:直接转义特殊字符,用Shell单引号包裹参数值

直接在Cypress参数中用单引号包裹凭证变量,同时转义凭证内的单引号:

stage('StageName') {
  steps {
    withCredentials([
      string(credentialsId: 'Client_Secret_D4_Backoffice_Admin', variable: 'Client_Secret_D4_Backoffice_Admin')
    ]) {
      sh """
        npx cypress-tags run -e TAGS='@release',clientId=.......,backofficeAdminPassword='${Client_Secret_D4_Backoffice_Admin//\'/\\\'}' --browser chrome --config-file D4-CDS-stage-cypress.config.ts
      """
    }
  }
}
  • 核心是通过Jenkins字符串替换处理凭证中的单引号,同时用Shell单引号包裹整个参数值,彻底阻止Shell解析$等特殊字符。

内容的提问来源于stack exchange,提问作者shruthi Muralidhara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 00:37:29