Jenkins Pipeline Groovy脚本:批处理脚本问题'%20'替换为' 0'

时间:2016-09-22 07:46:15

标签: git jenkins groovy jenkins-pipeline

我有一个使用git URL的管道脚本,其中包含空格https://username:password@project-teamprojects.visualstudio.com/defaultcollection/_git/Core - Stream

node ("master") {
  bat([script: "git push https://username:password@project-teamprojects.visualstudio.com/defaultcollection/_git/Core%20-%20Stream", encoding: "UTF-8" ])  
}

当执行此jenkins作业时,它会失败,因为'%20'已替换为'0'。记录说:

git push https://username:password@project-teamprojects.visualstudio.com/defaultcollection/_git/Core0-0Stream 
remote: TF401019: The Git repository with name or identifier Core0-0Stream does not exist or you do not have permissions for the operation you are attempting.
fatal: repository 'https://username:password@project-teamprojects.visualstudio.com/defaultcollection/_git/Core0-0Stream/' not found

如何使用jenkins管道脚本中的bat task正确编码git url

1 个答案:

答案 0 :(得分:1)

您的%20字符很可能被解释为变量。你有很多解决方案:

不允许变量替换

从bat脚本中删除插值。为此,请使用simple quotes代替double quotes

bat([script: 'git push https://username:password@project-teamprojects.visualstudio.com/defaultcollection/_git/Core%20-%20Stream', encoding: "UTF-8" ]) 

使用'+'char而不是'%20'

加号不应被解释为变量,并且应该在URL中作为空格有效:

bat([script: "git push https://username:password@project-teamprojects.visualstudio.com/defaultcollection/_git/Core+-+Stream", encoding: "UTF-8" ]) 

转义解释后的角色

我没有测试它,但很可能是你的蝙蝠脚本,它是被解释和替换的%。为避免这种情况,您可以尝试使用%%

来逃避它
bat([script: "git push https://username:password@project-teamprojects.visualstudio.com/defaultcollection/_git/Core%%20-%%20Stream", encoding: "UTF-8" ]) 

注意:根据this article%字符必须使用另一个百分比字符进行转义,因此:%%