Check if a file exists in jenkins pipeline

2019-02-11 09:48发布

I am trying to run block if a directory exists in my jenkins workspace and the pipeline step "fileExists: Verify file exists" in workspace doesn't seem to work correctly.

I'm using Jenkins v 1.642 and Pipeline v 2.1. and trying to have a condition like

if ( fileExists 'test1' ) {
  //Some block
}

What are the other alternatives I have within pipeline?

1条回答
迷人小祖宗
2楼-- · 2019-02-11 10:25

You need to use brackets when using the fileExists step in an if condition or assign the returned value to a variable

Using variable:

def exists = fileExists 'file'

if (exists) {
    echo 'Yes'
} else {
    echo 'No'
}

Using brackets:

if (fileExists('file')) {
    echo 'Yes'
} else {
    echo 'No'
}
查看更多
登录 后发表回答