Answers: <p>Check your SFTP folder permission on where you would put the file to make sure it has enough permission to do so.</p>
  <p>Consider using scp for shorter command.</p>
  
Answers: CC BY-SA 4.0
Answers: 2024-10-21T08:26:56.697Z
Answers: 2024-10-21T08:26:56.697Z
Answers: Hiếu Vũ
Body: <p><strong>Hello,
  I have some files that I want to download from Sonatype Nexus repo and upload it to my SFTP server using a Jenkins pipeline. The repo and the server are all running internally (on the local network).</strong></p>
  <p>My Jenkins is running inside a Docker container. Below is my current pipeline script.</p>
  <pre><code>    stage('Pull the file off Nexus') {
          withCredentials([usernameColonPassword(credentialsId: 'REDACTED ID', variable: 'NEXUS_CREDENTIALS')]) {
              sh script: '''
              curl -u ${NEXUS_CREDENTIALS} -o maven-metadata.xml &quot;PATH TO FILE in NEXUS&quot;
              '''
          }
      }
  stage('Upload file(s) to server') {
              withCredentials([usernameColonPassword(credentialsId: 'REDACTED ID', variable: 'FTP_CREDENTIALS')]) {
              sh script: '''
              # Extract username and password from FTP_CREDENTIALS
              USERNAME=$(echo ${FTP_CREDENTIALS} | cut -d: -f1)
              PASSWORD=$(echo ${FTP_CREDENTIALS} | cut -d: -f2)
              SERVER=IP_ADDRESS
              REMOTE_DIR=/home/peter/deploy/
              FILE=maven-metadata.xml
  
              # Print extracted values for debugging
              echo &quot;USERNAME=${USERNAME}&quot;
              echo &quot;PASSWORD=${PASSWORD}&quot;
              
              # Use sftp with a batch mode
              echo &quot;Uploading $FILE to $SERVER&quot;
              sftp -oBatchMode=no -oStrictHostKeyChecking=no -oPasswordAuthentication=yes ${USERNAME}@${SERVER} &lt;&lt; EOF
              cd $REMOTE_DIR
              put $FILE
              bye
              EOF
              '''
          }
  
      }
  }
  
  </code></pre>
  <p><strong>Downloading the file from Nexus to Jenkins works as expected</strong> but the second portion continues to have an error. Below is the error message:</p>
  <pre><code>Started by user admin
  [Pipeline] Start of Pipeline
  [Pipeline] node
  Running on Jenkins in /var/jenkins_home/workspace/nexus_test
  [Pipeline] {
  [Pipeline] stage
  [Pipeline] { (Pull the file off Nexus)
  [Pipeline] withCredentials
  Masking supported pattern matches of $NEXUS_CREDENTIALS
  [Pipeline] {
  [Pipeline] sh
  + curl -u **** -o maven-metadata.xml PATH_TO_FILE_ON_NEXUS
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                   Dload  Upload   Total   Spent    Left  Speed
  
    0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  100   284  100   284    0     0  30017      0 --:--:-- --:--:-- --:--:-- 31555
  [Pipeline] }
  [Pipeline] // withCredentials
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] stage
  [Pipeline] { (Upload file(s) to server)
  [Pipeline] withCredentials
  Masking supported pattern matches of $FTP_CREDENTIALS
  [Pipeline] {
  [Pipeline] sh
  + echo ****
  + cut -d: -f1
  + USERNAME=REDACTED
  + echo ****
  + cut -d: -f2
  + PASSWORD=REDACTED
  + SERVER=IP_ADDRESS
  + REMOTE_DIR=/home/peter/deploy/
  + FILE=maven-metadata.xml
  + echo USERNAME=REDACTED
  USERNAME=REDACTED
  + echo PASSWORD=REDACTED
  PASSWORD=REDACTED
  + echo Uploading maven-metadata.xml to IP_ADDRESS
  Uploading maven-metadata.xml to IP_ADDRESS
  + sftp -oBatchMode=no -oStrictHostKeyChecking=no -oPasswordAuthentication=yes REDACTED@IP_ADDRESS
  Warning: Permanently added 'IP_ADDRESS' (ED25519) to the list of known hosts.
  Permission denied, please try again.
  Permission denied, please try again.
  REDACTED@IP_ADDRESS: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
  
  Connection closed.  
  Connection closed
  [Pipeline] }
  [Pipeline] // withCredentials
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] End of Pipeline
  ERROR: script returned exit code 255
  Finished: FAILURE
  </code></pre>
  <p>As this is my first time with shell scripting and working with Jenkins, any help will be appreciated.</p>
  <p>Thank you</p>
  
Tags: <jenkins><jenkins-pipeline><shell-script><nexus>
Title: Downloading File from Sonatype Nexus and Uploading it to SFTP server using Jenkins Pipeline
UserDisplayName: pikklefish
ThreadText: # Downloading File from Sonatype Nexus and Uploading it to SFTP server using Jenkins Pipeline
  
  (Asked by: username_0 on 2024-08-27)
  
  **Hello,
  I have some files that I want to download from Sonatype Nexus repo and upload it to my SFTP server using a Jenkins pipeline. The repo and the server are all running internally (on the local network).**
  
  My Jenkins is running inside a Docker container. Below is my current pipeline script.
  
  ```
      stage('Pull the file off Nexus') {
          withCredentials([usernameColonPassword(credentialsId: 'REDACTED ID', variable: 'NEXUS_CREDENTIALS')]) {
              sh script: '''
              curl -u ${NEXUS_CREDENTIALS} -o maven-metadata.xml "PATH TO FILE in NEXUS"
              '''
          }
      }
  stage('Upload file(s) to server') {
              withCredentials([usernameColonPassword(credentialsId: 'REDACTED ID', variable: 'FTP_CREDENTIALS')]) {
              sh script: '''
              # Extract username and password from FTP_CREDENTIALS
              USERNAME=$(echo ${FTP_CREDENTIALS} | cut -d: -f1)
              PASSWORD=$(echo ${FTP_CREDENTIALS} | cut -d: -f2)
              SERVER=IP_ADDRESS
              REMOTE_DIR=/home/peter/deploy/
              FILE=maven-metadata.xml
  
              # Print extracted values for debugging
              echo "USERNAME=${USERNAME}"
              echo "PASSWORD=${PASSWORD}"
              
              # Use sftp with a batch mode
              echo "Uploading $FILE to $SERVER"
              sftp -oBatchMode=no -oStrictHostKeyChecking=no -oPasswordAuthentication=yes ${USERNAME}@${SERVER} << EOF
              cd $REMOTE_DIR
              put $FILE
              bye
              EOF
              '''
          }
  
      }
  }
  
  ```
  
  **Downloading the file from Nexus to Jenkins works as expected** but the second portion continues to have an error. Below is the error message:
  
  ```
  Started by user admin
  [Pipeline] Start of Pipeline
  [Pipeline] node
  Running on Jenkins in /var/jenkins_home/workspace/nexus_test
  [Pipeline] {
  [Pipeline] stage
  [Pipeline] { (Pull the file off Nexus)
  [Pipeline] withCredentials
  Masking supported pattern matches of $NEXUS_CREDENTIALS
  [Pipeline] {
  [Pipeline] sh
  + curl -u **** -o maven-metadata.xml PATH_TO_FILE_ON_NEXUS
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                   Dload  Upload   Total   Spent    Left  Speed
  
    0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  100   284  100   284    0     0  30017      0 --:--:-- --:--:-- --:--:-- 31555
  [Pipeline] }
  [Pipeline] // withCredentials
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] stage
  [Pipeline] { (Upload file(s) to server)
  [Pipeline] withCredentials
  Masking supported pattern matches of $FTP_CREDENTIALS
  [Pipeline] {
  [Pipeline] sh
  + echo ****
  + cut -d: -f1
  + USERNAME=REDACTED
  + echo ****
  + cut -d: -f2
  + PASSWORD=REDACTED
  + SERVER=IP_ADDRESS
  + REMOTE_DIR=/home/peter/deploy/
  + FILE=maven-metadata.xml
  + echo USERNAME=REDACTED
  USERNAME=REDACTED
  + echo PASSWORD=REDACTED
  PASSWORD=REDACTED
  + echo Uploading maven-metadata.xml to IP_ADDRESS
  Uploading maven-metadata.xml to IP_ADDRESS
  + sftp -oBatchMode=no -oStrictHostKeyChecking=no -oPasswordAuthentication=yes REDACTED@IP_ADDRESS
  Warning: Permanently added 'IP_ADDRESS' (ED25519) to the list of known hosts.
  Permission denied, please try again.
  Permission denied, please try again.
  REDACTED@IP_ADDRESS: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
  
  Connection closed.  
  Connection closed
  [Pipeline] }
  [Pipeline] // withCredentials
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] End of Pipeline
  ERROR: script returned exit code 255
  Finished: FAILURE
  
  ```
  
  As this is my first time with shell scripting and working with Jenkins, any help will be appreciated.
  
  Thank you
  
  **Tags:** jenkins, jenkins-pipeline, shell-script, nexus
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-10-21. Score: 0
  
  Check your SFTP folder permission on where you would put the file to make sure it has enough permission to do so.
  
  Consider using scp for shorter command.
  
  
  ---
  
  