从 jenkinsfile groovy 脚本中的 bash 访问字符串变量
Access string variable from bash in jenkinsfile groovy script
我正在使用 gradle 和 bash 脚本在 docker 图像中构建多个 android 应用程序。该脚本由 jenkins 触发,运行 是 docker 图像。
在 bash 脚本中,我收集有关构建成功的信息。我想将该信息传递给 jenkinsfile 的 groovy 脚本。
我试图在 docker 容器中创建一个 txt 文件,但是 jenkinsfile 中的 groovy 脚本找不到该文件。
这是我的 jenkinsfile 的 groovy 脚本:
script {
try {
sh script:'''
#!/bin/bash
./jenkins.sh
'''
} catch(e){
currentBuild.result = "FAILURE"
} finally {
String buildResults = null
try {
def pathToBuildResults="[...]/buildResults.txt"
buildResults = readFile "${pathToBuildResults}"
} catch(e) {
buildResults = "error receiving build results. Error: " + e.toString()
}
}
}
在我的 jenkins.sh bash 脚本中,我执行以下操作:
[...]
buildResults+=" $appName: Build Failed!" //this is done for several apps
echo "$buildResults" | cat > $pathToBuildResults //this works I checked, if the file is created
[...]
文件已创建,但 groovy 找不到。我认为原因是,jenkins 脚本没有 运行 在 docker 容器内。
如何在我的 groovy jenkins 脚本中访问 bash 脚本的字符串 buildResults?
为了避免需要读取结果文件,您可以选择修改 jenkins.sh
脚本以将结果打印到输出而不是将它们写入文件,然后使用 sh
捕获该输出并使用它而不是文件的步骤。
类似于:
script {
try {
String buildResults = sh returnStdout: true, script:'''
#!/bin/bash
./jenkins.sh
'''
// You now have the output of jenkins.sh inside the buildResults parameter
} catch(e){
currentBuild.result = "FAILURE"
}
}
这样您就无需处理输出文件并直接获得您需要的结果,然后您可以根据需要解析和使用这些结果。
我正在使用 gradle 和 bash 脚本在 docker 图像中构建多个 android 应用程序。该脚本由 jenkins 触发,运行 是 docker 图像。 在 bash 脚本中,我收集有关构建成功的信息。我想将该信息传递给 jenkinsfile 的 groovy 脚本。 我试图在 docker 容器中创建一个 txt 文件,但是 jenkinsfile 中的 groovy 脚本找不到该文件。 这是我的 jenkinsfile 的 groovy 脚本:
script {
try {
sh script:'''
#!/bin/bash
./jenkins.sh
'''
} catch(e){
currentBuild.result = "FAILURE"
} finally {
String buildResults = null
try {
def pathToBuildResults="[...]/buildResults.txt"
buildResults = readFile "${pathToBuildResults}"
} catch(e) {
buildResults = "error receiving build results. Error: " + e.toString()
}
}
}
在我的 jenkins.sh bash 脚本中,我执行以下操作:
[...]
buildResults+=" $appName: Build Failed!" //this is done for several apps
echo "$buildResults" | cat > $pathToBuildResults //this works I checked, if the file is created
[...]
文件已创建,但 groovy 找不到。我认为原因是,jenkins 脚本没有 运行 在 docker 容器内。
如何在我的 groovy jenkins 脚本中访问 bash 脚本的字符串 buildResults?
为了避免需要读取结果文件,您可以选择修改 jenkins.sh
脚本以将结果打印到输出而不是将它们写入文件,然后使用 sh
捕获该输出并使用它而不是文件的步骤。
类似于:
script {
try {
String buildResults = sh returnStdout: true, script:'''
#!/bin/bash
./jenkins.sh
'''
// You now have the output of jenkins.sh inside the buildResults parameter
} catch(e){
currentBuild.result = "FAILURE"
}
}
这样您就无需处理输出文件并直接获得您需要的结果,然后您可以根据需要解析和使用这些结果。