Jenkins的pipeline写groovy脚本-如何判断文件是否存在

判断在工作目录下是否存在**/target/failsafe-reports/TEST-*.xml的文件,如果存在就执行操作。

stage('Scan'') {
    dir("${WORKSPACE}"){
        script {
            try{
				out = sh(script: "[ -f **/target/failsafe-reports/TEST-*.xml ]  && echo 'true' || echo 'false' ", returnStdout: true)
				println out
				if(out == "true") {
                junit '**/target/failsafe-reports/TEST-*.xml'
				echo "failsafe-reports exist."
				}
            } catch(Exception e) {
				println e
				echo("failsafe-reports does not exist.")
			}
		}
    }
}

然后我又发现了更棒的,更加符合pipeline语法的写法,用fileExists方法

dir("${WORKSPACE}"){
                     script {
                         if(fileExists("**/target/failsafe-reports/TEST-*.xml")){
                            echo "failsafe-reports exist."
                            junit '**/target/failsafe-reports/TEST-*.xml'
                         }else{
                             echo("failsafe-reports does not exist.")
					        }
                         }
				        }

 


附加:

Jenkins如何扫描到IT测试类,并在UI上显示统计结果?

用Junit插件就可以,Junit既可以扫描到Unit Test ,也可以扫描到IT Test。

在需要的脚本上加上以下命令就行,扫描的路径根据自己个人项目修改。

junit '**/target/failsafe-reports/TEST-*.xml'

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值