当我试图创建一个Jenkins项目时,我观察到了一种奇怪的行为,该项目有Ant步骤,接受Ant要使用的任何字符串参数。因此,Ant似乎以某种方式接收了一些格式错误的内容作为参数。
首先,我登录了Jenkins安装的同一个系统,并编写了一个简单的ant构建文件:
<?xml version="1.0"?>
<!--DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "/usr/src/ant/project.dtd"-->
<project name="many-params" default="run">
<target name="run">
<echo message="first Param: ${firstparam}"/>
<echo message="second Param: ${secondparam}"/>
</target>
</project>然后用参数集运行ant并得到预期的输出:
$ ant -Dfirstparam=A -Dsecondparam=B
Buildfile: /tmp/build-dir/build.xml
run:
[echo] first Param: A
[echo] second Param: B
BUILD SUCCESSFUL
Total time: 0 seconds接下来,我在詹金斯做了一个同样的项目。我的Jenkins系统规格如下:
我已经设置了8个连续步骤的任务,4个准备外壳脚本片段,用稍微不同的参数名称重写build.xml (没有分隔符、破折号分隔符、点分隔符),以及调用ant的4个Ant构建步骤,试图传递相应的参数。
作业目录中的就绪config.xml如下所示:
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description>A simple task to test many parameter setting for jenkins calling ant tasks</description>
<logRotator class="hudson.tasks.LogRotator">
<daysToKeep>-1</daysToKeep>
<numToKeep>10</numToKeep>
<artifactDaysToKeep>-1</artifactDaysToKeep>
<artifactNumToKeep>-1</artifactNumToKeep>
</logRotator>
<keepDependencies>false</keepDependencies>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<jdk>Oracle Java 1.6.45 32-bit JRE</jdk>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>echo "STEP 1: Pass parameters with no separators in names"
cat > build.xml << EOF
<?xml version="1.0"?>
<!--DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "/usr/src/ant/project.dtd"-->
<project name="many-params" default="run">
<target name="run">
<echo message="first Param: ${paramfirst}"/>
<echo message="second Param: ${paramsecond}"/>
</target>
</project>
EOF</command>
</hudson.tasks.Shell>
<hudson.tasks.Ant plugin="ant@1.2">
<targets>run</targets>
<antName>Ant 1.8.2</antName>
<properties>paramfirst=A
paramsecond=B</properties>
</hudson.tasks.Ant>
<hudson.tasks.Shell>
<command>rm build.xml
echo
echo "STEP 2: Pass parameters with separated by dashes in names"
cat > build.xml << EOF
<?xml version="1.0"?>
<!--DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "/usr/src/ant/project.dtd"-->
<project name="many-params" default="run">
<target name="run">
<echo message="first Param: ${param-first}"/>
<echo message="second Param: ${param-second}"/>
</target>
</project>
EOF</command>
</hudson.tasks.Shell>
<hudson.tasks.Ant plugin="ant@1.2">
<targets>run</targets>
<antName>Ant 1.8.2</antName>
<properties>param-first=A
param-second=B</properties>
</hudson.tasks.Ant>
<hudson.tasks.Shell>
<command>echo
echo "STEP 3: Same, but with parameters on the same line in Ant Plugin step setup"</command>
</hudson.tasks.Shell>
<hudson.tasks.Ant plugin="ant@1.2">
<targets>run</targets>
<antName>Ant 1.8.2</antName>
<properties>param-first=A param-second=B</properties>
</hudson.tasks.Ant>
<hudson.tasks.Shell>
<command>rm build.xml
echo
echo "STEP 4: Pass parameters with separated by dots in names"
cat > build.xml << EOF
<?xml version="1.0"?>
<!--DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "/usr/src/ant/project.dtd"-->
<project name="many-params" default="run">
<target name="run">
<echo message="first Param: ${param.first}"/>
<echo message="second Param: ${param.second}"/>
</target>
</project>
EOF</command>
</hudson.tasks.Shell>
<hudson.tasks.Ant plugin="ant@1.2">
<targets>run</targets>
<antName>Ant 1.8.2</antName>
<properties>param.first=A
param.second=B</properties>
</hudson.tasks.Ant>
</builders>
<publishers/>
<buildWrappers>
<hudson.plugins.ws__cleanup.PreBuildCleanup plugin="ws-cleanup@0.26">
<deleteDirs>false</deleteDirs>
<cleanupParameter></cleanupParameter>
<externalDelete></externalDelete>
</hudson.plugins.ws__cleanup.PreBuildCleanup>
</buildWrappers>
</project>构建该任务给了我以下输出:
Started by user Alexander Krasnyansky
[EnvInject] - Loading node environment variables.
Building remotely on j03_uk1ps41 (buildslaves) in workspace /var/lib/jenkins03/workspace/akrasnyansky/ManyParams/ant-param-test
Deleting project workspace... done
[ant-param-test] $ /bin/sh -xe /tmp/hudson2203137136163442442.sh
+ echo 'STEP 1: Pass parameters with no separators in names'
STEP 1: Pass parameters with no separators in names
+ cat
[ant-param-test] $ /usr/local/apache-ant-1.8.2/bin/ant -Dparamfirst=A -Dparamsecond=B run
Buildfile: /var/lib/jenkins03/workspace/akrasnyansky/ManyParams/ant-param-test/build.xml
run:
[echo] first Param:
[echo] second Param:
BUILD SUCCESSFUL
Total time: 0 seconds
[ant-param-test] $ /bin/sh -xe /tmp/hudson5869719507232196626.sh
+ rm build.xml
+ echo
+ echo 'STEP 2: Pass parameters with separated by dashes in names'
STEP 2: Pass parameters with separated by dashes in names
+ cat
[ant-param-test] $ /usr/local/apache-ant-1.8.2/bin/ant -Dparam-first=A -Dparam-second=B run
Buildfile: /var/lib/jenkins03/workspace/akrasnyansky/ManyParams/ant-param-test/build.xml
run:
[echo] first Param: first
[echo] second Param: second
BUILD SUCCESSFUL
Total time: 0 seconds
[ant-param-test] $ /bin/sh -xe /tmp/hudson1052316660761572548.sh
+ echo
+ echo 'STEP 3: Same, but with parameters on the same line in Ant Plugin step setup'
STEP 3: Same, but with parameters on the same line in Ant Plugin step setup
[ant-param-test] $ /usr/local/apache-ant-1.8.2/bin/ant "-Dparam-first=A param-second=B" run
Buildfile: /var/lib/jenkins03/workspace/akrasnyansky/ManyParams/ant-param-test/build.xml
run:
[echo] first Param: first
[echo] second Param: second
BUILD SUCCESSFUL
Total time: 0 seconds
[ant-param-test] $ /bin/sh -xe /tmp/hudson4427994032931026249.sh
+ rm build.xml
+ echo
+ echo 'STEP 4: Pass parameters with separated by dots in names'
STEP 4: Pass parameters with separated by dots in names
+ cat
/tmp/hudson4427994032931026249.sh: line 5: <?xml version="1.0"?>
<!--DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "/usr/src/ant/project.dtd"-->
<project name="many-params" default="run">
<target name="run">
<echo message="first Param: ${param.first}"/>
<echo message="second Param: ${param.second}"/>
</target>
</project>
: bad substitution
Build step 'Execute shell' marked build as failure
Started calculate disk usage of build
Finished Calculation of disk usage of build in 0 seconds
Started calculate disk usage of workspace
Finished Calculation of disk usage of workspace in 0 seconds
Finished: FAILURE因此,这就产生了许多问题:
非常感谢就这些问题提出的任何建议。
发布于 2015-08-25 16:29:04
你的问题是重写build.xml。我真的不明白你为什么要这么做。但让我们跳进去吧。
tl;dr:您混淆了Shell和Ant变量的行为:如何转义它们,以及当它们未定义时会发生什么。
您有一个"Execute“构建步骤,并在这里使用cat将新内容放入文件中。
停在那儿。要么删除其他构建步骤,要么简单地在cat命令之后放置一个巨大的cat。现在查看您的工作区中生成的build.xml。你会看到:
<echo message="first Param: "/>
正如你所看到的,在回声线中实际上没有参数。因此,当它运行时,它不会打印任何东西,除了文本之外,这并不奇怪。为什么会发生这种事?
1.您的cat命令在Shell中运行。你不能逃避$在first Param: ${paramfirst}中的美元签名。因此,在运行时,Shell将${paramfirst}视为变量,并试图找到它的值。没有一个,所以Shell没有用任何东西替换它(如果是Ant,Ant就会逐字打印未定义的变量)
您需要从Shell中转义变量,因此编写first Param: \${paramfirst}。这将生成正确的build.xml,所有这些都将从那里工作。
2. Allowed characters in linux environment variable names或Can shell variable include - character?
连字符-不是允许的Shell变量名。
在Shell看到变量定义${之后,它会向前看连字符- (原因连字符不能是变量名),并在那里拆分字符串。因此,在${param-first}中,Shell认为${param是可变的,-first}是格式错误的文本。第一个被替换为零,其余的被打印出来(由于Shell的特性,}被消耗)。
3. -- "Invoke Ant“构建步骤--期望以key=value的方式进行params,每行一个。然后,它将取每一行,以-D作为前缀,并将多行字符串放在一起,形成最终的命令行。
所以这个:
param-first=A
param-second=B变成这样:
-Dparam-first=A
-Dparam-second=B然后串在一起,如:-Dparam-first=A -Dparam-second=B
当你这么做的时候
param-first=A param-second=B它变成了(记住:每行一个):
-Dparam-first=A param-second=B注意,每行只有一个-D,因为它是一个param
4. Shell不允许变量名中的句点为.。句号。(双关意)。见1.和2.的答案。不过,您可以在Ant变量名中使用句点和连字符,但出于一致性原因,我建议您不要这样做。
https://stackoverflow.com/questions/32187134
复制相似问题