将目录路径作为参数传递给批处理文件

huangapple go评论47阅读模式
英文:

How to pass directory path as argument to batch file

问题

我正在编写一个管道脚本来调用一个批处理文件,该文件将调用一个.wsf脚本。

我正在尝试执行一个带有一个参数的批处理文件。参数是一个文件路径。我可以让Jenkins脚本执行批处理文件,但无法成功将文件路径作为参数传递。

.wsf脚本(使用VBScript)使用输入目录构建完整的文件路径。它使用反斜杠线,所以我假设我传递给批处理文件的路径应该使用反斜杠线定义。

我尝试了许多不同的建议语法,但都没有成功。该脚本要么因为语法错误而无法运行,要么只是将变量名作为文字字符串传递,而不是我预期的分配值。

实际的.bat文件名中确实有一个和号,所以我以类似的方式命名了这个示例。

这是我目前尝试的内容:

environment {
    ROOT_DIR = 'http:\\\\myserver.mycity.mycompany.com\\svn\\PRODUCT\\trunk'
}
...
stage('Copy my files') {
    steps {
        bat script: "ABC_123\\Rock^&Roll42.bat", args: "\"${env.ROOT_DIR }\""
    }
}
英文:

I'm writing a pipeline script to call a batch file, which in turn will call a .wsf script.

I am trying to execute a batch file with one parameter. The parameter is a file path. I can get the Jenkins script to execute the batch file, but I cannot successfully pass the file path as an argument.

The .wsf script (which uses VBScript) builds out the full file path using the input directory. It uses backslashes, so I'm assuming that the path I pass to the batch file should be defined using backslashes.

I have tried many different suggested syntax, but nothing works. The script either won't run due to syntax error, or it just passes the variable name as literal string instead of the assigned value that I expected.

The name of the actual .bat file does have an ampersand in it, so I named this example similarly.

This is what I currently tried:

	environment {
        ROOT_DIR = 'http:\\\\myserver.mycity.mycompany.com\\svn\\PRODUCT\\trunk'
    }
...
        stage('Copy my files') {
            steps {
				bat script: "ABC_123\\Rock^&Roll42.bat", args: "\"${env.ROOT_DIR }\""
			}
        }

答案1

得分: 1

根据文档args参数不存在,因此您可能希望尝试:

bat脚本:"ABC_123\\Rock^&Roll42.bat ${env.ROOT_DIR}"

作为另一种选项,当变量在environment块内定义时,它不仅可以通过env.ROOT_DIR在流水线中使用,还会在您使用的shell中作为环境变量创建。因此,它可以通过以下方式直接在cmd/powershell/sh shell中访问(根据shell语法而定),以下是一些示例:

  1. cmd可以通过在变量名称的末尾和开头附加%来使用环境变量,所以它会是%ROOT_DIR%
  2. sh可以使用$来使用环境变量,所以它会是$ROOT_DIR

因此,我会尝试这样做:

bat脚本:"ABC_123\\Rock^&Roll42.bat %ROOT_DIR%"

在上面的示例中,变量替换不是在Groovy中进行的,而是由您执行的shell直接完成的。这是传递秘密给shell步骤的推荐做法。

英文:

According to the documentation, the args argument does not exist, so you may want to try:

bat script: "ABC_123\\Rock^&Roll42.bat ${env.ROOT_DIR}"

As another option, when a variable is defined inside the environment block it will not only be available inside the pipeline by using env.ROOT_DIR but it will also be created as an environment variable inside the shells that you use.
So it can be accessed directly in the cmd/powershell/sh shell by (depending on the shell syntax) using the variable as a normal shell variable, some examples:

  1. cmd can use environment variables by appending % at the end and the start of the variable name, so it would be %ROOT_DIR%.
  2. sh can use environment variables by using $ so it would be $ROOT_DIR.

So, I would try this:

bat script: "ABC_123\\Rock^&Roll42.bat %ROOT_DIR%"

In the above example, the variable substitution is not happening on Groovy, it is being done directly by the shell you are executing. This is recommended for passing secrets to shell steps.

huangapple
  • 本文由 发表于 2023年5月26日 01:04:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定