Jenkins Pipeline: Deploying and Running JAR file via SCP & Nohup
Published: 2023-03-14 06:34:50

Deploying a JAR file via SCP and starting it with nohup is a common approach for deploying Java applications to remote servers. Here are some reasons why this approach is often used:

1. Convenience: This approach allows you to quickly and easily deploy a JAR file to a remote server without requiring any additional software or tools. All you need is a terminal with SSH access to the remote server.
2. Portability: Because the JAR file contains all of the dependencies and resources needed to run the application, you can deploy it to any server with a compatible Java runtime environment.
3. Scalability: Deploying the JAR file to a remote server allows you to run the application on a dedicated server, which can help to improve performance and scalability.
4. Reliability: Using nohup to start the application ensures that it continues running even if the SSH session is terminated or the server is restarted.

## Example

```
pipeline {
  agent any
  
  stages {
    stage('Deploy JAR via SCP') {
      steps {
        sh """
          sshpass -p '<REMOTE_SERVER_PASSWORD>' scp '<PATH_TO_JAR_FILE>' '<REMOTE_SERVER_USERNAME>@<REMOTE_SERVER_IP>:<REMOTE_DIRECTORY_PATH>'
        """
      }
    }
    
    stage('Start JAR with nohup') {
      steps {
        sh """
          sshpass -p '<REMOTE_SERVER_PASSWORD>' ssh '<REMOTE_SERVER_USERNAME>@<REMOTE_SERVER_IP>' 'cd <REMOTE_DIRECTORY_PATH> && nohup java -jar <JAR_FILE_NAME> &'
        """
      }
    }
  }
}

```
In this example, we are using the sshpass command to pass the password of the remote server user. Note that this approach is not as secure as using SSH keys, so it's important to take appropriate security measures to protect the password.

Replace REMOTE_SERVER_PASSWORD with the password of the remote server user, PATH_TO_JAR_FILE with the path to the JAR file you want to deploy, REMOTE_SERVER_USERNAME with the username of the remote server user, REMOTE_SERVER_IP with the IP address or hostname of the remote server, and REMOTE_DIRECTORY_PATH with the remote directory path where you want to deploy the JAR file.

Make sure that the remote server user has write access to the remote directory path where you want to deploy the JAR file. Also, make sure that sshpass is installed on the machine where the Jenkins pipeline is running.

Replace REMOTE_SERVER_PASSWORD with the password of the remote server user, PATH_TO_JAR_FILE with the path to the JAR file you want to deploy, REMOTE_SERVER_USERNAME with the username of the remote server user, REMOTE_SERVER_IP with the IP address or hostname of the remote server, REMOTE_DIRECTORY_PATH with the remote directory path where you want to deploy the JAR file, and JAR_FILE_NAME with the name of the JAR file.

The nohup command is used to start the JAR file in the background so that the process continues running even after the SSH session is terminated. The & at the end of the command tells the shell to run the command in the background. Note that you may need to adjust the command based on the specific requirements of your application.

## Conclusion

Overall, deploying a JAR file via SCP and starting it with nohup is a simple and effective way to deploy Java applications to remote servers. However, it's important to ensure that appropriate security measures are in place to protect the remote server and the data stored on it. This may include using secure SSH authentication methods, such as SSH keys, and configuring firewalls to restrict access to the server.