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'

猜你喜欢

转载自blog.csdn.net/fenger_c/article/details/110916106