Farah Sultana
Farah Sultana

Reputation: 1

How can I check for the file that is present in the other directory and not in the workspace from a Jenkinsfile

I have a tar/zip file in a git repo. This tar/zip file needs to be put up in the directory named as 'chart_directory'. Using my Jenkinsfile, I want to implement the logic that it would download this tar/zip file from the git repo only if 'chart_directory' does not already contain a copy of the tar/zip file present in the git repo. eg - There is a file named 'Charts.tgz' in a git repo named 'testing.git' I need to download this 'Charts.tgz' file in 'chart_directory' only if 'chart_directory' does not already contain the file 'Charts.tgz'. If Charts.tgz is already present in 'chart_directory', then it should skip to download the file.

Upvotes: 0

Views: 485

Answers (1)

Shane Bishop
Shane Bishop

Reputation: 4750

Note: I am assuming your Jenkins server is running a Unix OS, since you don't specify the OS in your question.

You can check if the Charts.tgz file exists locally in chart_directory with the find command:

find chart_directory -type f -name Charts.tgz

-type f tells find to search for regular files, and -name Charts.tgz tells find to search specifically for Charts.tgz.

find will have an exit code of zero if it finds nothing. To work around this, you can pipe to read to get a non-zero exit code if find finds nothing. (read has a non-zero exit code if it encounters an EOF, which is exactly what will happen if find doesn't find anything.)

In your code, you can download Charts.tgz only if it is not present in chart_directory like this:

find chart_directory -type f -name Charts.tgz | read || {
    # Your code here (between the '{' and '}') to clone the git repo
    # to retrieve Charts.tgz
}

What the || operator does is the expression on the left side is only evaluated if the expression on the right side has a non-zero exit code. In this case, this means the code between the { and } will only be evaluated if find doesn't find anything.

Upvotes: 1

Related Questions