Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-38972

fileExists doesn't take variables

    XMLWordPrintable

Details

    Description

      I created a pipeline job with a Jenkins files that has the following content.. but I can't get controlFile to work correctly as it is a variable and fileExists doesnt seem to resolve it.

      node {
      def controlFile = '/path/somefile.txt'
      if (fileExists(controlFile)) {
      echo '${controlFile} exists.'
      } else {
      echo '${controlFile} missing. Will try to mount'
      sh 'mount /path/server/extFs'
      if (fileExists(controlFile)) {
      echo '${controlFile} exists after mount. Continue...'
      } else {
      error: '${controlFile} still missing. Will now fail the job.'
      }
      }
      }

      Attachments

        Activity

          rachel Rachel M. added a comment -

          In order to replace a variable in a string, you might use double quotes. E.g:

          ...
          echo "${controlFile} exists."
          ...
          
          rachel Rachel M. added a comment - In order to replace a variable in a string, you might use double quotes. E.g: ... echo "${controlFile} exists." ...
          gunfus angel vera added a comment -

          The issue is not with the echo command the issue is with the fileExists routine.

          gunfus angel vera added a comment - The issue is not with the echo command the issue is with the fileExists routine.
          rachel Rachel M. added a comment -

          Sorry for the misunderstanding.

          In that case, please, check permissions. User with which you started Jenkins might have permissions to access the file.

          rachel Rachel M. added a comment - Sorry for the misunderstanding. In that case, please, check permissions. User with which you started Jenkins might have permissions to access the file.
          gunfus angel vera added a comment -

          Can you try the example attached..? the problems is related to the fact that I can't get fileExist to work with the variable, it simple doesn't recognize that is a variable.

          gunfus angel vera added a comment - Can you try the example attached..? the problems is related to the fact that I can't get fileExist to work with the variable, it simple doesn't recognize that is a variable.
          rachel Rachel M. added a comment -

          Hi gunfus,

          Of course, it's the first thing I did:

          Output:

          [Pipeline] {
          [Pipeline] fileExists
          [Pipeline] echo
          /home/rachel/examples/file.txt exists.
          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          Finished: SUCCESS
          

          The only problem I found in your code was the use of single quotes in echo strings.

          rachel Rachel M. added a comment - Hi gunfus , Of course, it's the first thing I did: Output: [Pipeline] { [Pipeline] fileExists [Pipeline] echo /home/rachel/examples/file.txt exists. [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS The only problem I found in your code was the use of single quotes in echo strings.
          rachel Rachel M. added a comment -

          And the section:

          } else {
               error: '${controlFile} still missing. Will now fail the job.'
          }
          

          It should be:

          } else {
               error("${controlFile} still missing. Will now fail the job.")
          }
          
          rachel Rachel M. added a comment - And the section: } else { error: '${controlFile} still missing. Will now fail the job.' } It should be: } else { error( "${controlFile} still missing. Will now fail the job." ) }

          People

            rachel Rachel M.
            gunfus angel vera
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: