【原】Gradle调用shell脚本和python脚本并传参
最近由于项目自动化构建的需要,研究了下gradle调用脚本并传参的用法,在此作个总结。
Pre
build.gradle中定义了$jenkinsJobName $jenkinsBuild两个Jenkins变量,意图将gradle中的这两个值传到shell/python脚本中去用
def jenkinsBuild = System.getenv("BUILD_NUMBER") ?: "0"
println "jenkinsBuild is set to $jenkinsBuild"
def jenkinsJobName = System.getenv("JOB_NAME") ?: "defaultJobName"
println "jenkinsJobName is set to $jenkinsJobName"
Gradle调用shell脚本带参数
gradle task
task upload(type: Exec) { executable 'sh' args "-c","../shelldemo.sh $jenkinsJobName $jenkinsBuild" }
shelldemo.sh脚本中
#!/usr/bin/env bash #从build.gradle中传入的两个参数 jenkinsJobName=$1 jenkinsBuild=$2 echo "jenkinsJobName=$jenkinsJobName" echo "jenkinsBuild=$jenkinsBuild" ...
Gradle调用python脚本带参数
gradle task
task uploadpython(type: Exec) { workingDir './' commandLine "python","pythondemo.py", "$jenkinsJobName", "$jenkinsBuild" }
pythondemo.py脚本
#encoding:utf8 import sys jenkinsJobName=sys.argv[1] jenkinsBuild=sys.argv[2] print jenkinsJobName print jenkinsBuild
...
Gradle Exec官方文档的说明
注意双引号
Groovy中只为双引号的字符进行插值,所以如果写了单引号,是不work的。
http://stackoverflow.com/questions/14900883/trying-to-undersand-how-i-can-pass-arguments-from-gradle-to-my-shell-script
Groovy only performs String interpolation for double-quoted Strings. (That's one of the reasons why I use double quotes by default.) Try: ... args "-c", "source dosomething.sh $arg"