Run JAR Files From Jenkins To EC2 A Comprehensive Guide

by ADMIN 56 views
Iklan Headers

In today's software development landscape, continuous integration and continuous deployment (CI/CD) have become essential practices for streamlining the software delivery process. Jenkins, a leading open-source automation server, plays a crucial role in CI/CD pipelines, automating tasks such as building, testing, and deploying applications. Amazon EC2, a cloud computing service provided by Amazon Web Services (AWS), offers scalable virtual servers in the cloud, making it an ideal platform for hosting applications. This article delves into the process of running JAR files from a Jenkins server to a remote EC2 instance, providing a comprehensive guide for developers and system administrators seeking to automate their deployment workflows. We'll explore the necessary configurations, security considerations, and troubleshooting tips to ensure a smooth and efficient deployment process.

Before embarking on the journey of running JAR files from Jenkins to EC2, it's essential to ensure that you have the following prerequisites in place:

  • A running Jenkins server: You'll need a Jenkins server up and running. If you don't have one already, you can download and install Jenkins from the official website (https://www.jenkins.io/).
  • An active AWS account: To utilize EC2 instances, you'll need an active AWS account. If you don't have one, you can sign up for a free AWS account.
  • An EC2 instance: You'll need an EC2 instance running in your AWS account. This instance will serve as the target server for deploying your JAR file. Make sure the instance has Java installed.
  • A JAR file: You'll need the JAR file that you want to deploy to the EC2 instance.
  • SSH access to the EC2 instance: Jenkins will need SSH access to the EC2 instance to execute commands remotely. This involves setting up key-based authentication.
  • Basic understanding of Linux commands: Familiarity with Linux commands will be beneficial for navigating the EC2 instance and troubleshooting any issues.

To enable Jenkins to securely access the EC2 instance, we'll set up SSH key-based authentication. This method eliminates the need to store passwords directly in Jenkins, enhancing security.

  1. Generate an SSH key pair on the Jenkins server:

    Open a terminal on the Jenkins server and execute the following command:

    ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa
    

    This command generates a private key (id_rsa) and a public key (id_rsa.pub) in the .ssh directory within the Jenkins user's home directory. You'll be prompted to enter a passphrase for the key pair. While a passphrase adds an extra layer of security, it's important to note that Jenkins will need to access the key without manual intervention, so you may choose to leave the passphrase empty.

  2. Copy the public key to the EC2 instance:

    Copy the contents of the id_rsa.pub file to the ~/.ssh/authorized_keys file on the EC2 instance. You can use the ssh-copy-id command or manually copy and paste the key. If you don't have the .ssh directory or authorized_keys file, you will need to generate them before pasting the key, which can be done using the following commands:

    mkdir ~/.ssh
    chmod 700 ~/.ssh
    touch ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
    

    Using ssh-copy-id:

    ssh-copy-id -i ~/.ssh/id_rsa.pub [username]@[ec2_instance_ip]
    

    Replace [username] with the username for your EC2 instance (e.g., ec2-user for Amazon Linux) and [ec2_instance_ip] with the public IP address of your EC2 instance. This command will prompt you for the user's password on the EC2 instance.

    Manually copying the key:

    cat ~/.ssh/id_rsa.pub
    

    Copy the output of this command. Then, SSH into your EC2 instance:

    ssh [username]@[ec2_instance_ip]
    

    And then run the following commands:

    mkdir ~/.ssh
    chmod 700 ~/.ssh
    touch ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
    vi ~/.ssh/authorized_keys
    

    Paste the copied public key into the authorized_keys file. Save and exit the file.

  3. Verify SSH access:

    From the Jenkins server, try SSHing into the EC2 instance using the private key:

    ssh -i ~/.ssh/id_rsa [username]@[ec2_instance_ip]
    

    If the connection is successful without prompting for a password, SSH key-based authentication is set up correctly.

With SSH key-based authentication in place, we can now configure Jenkins to run JAR files on the EC2 instance.

  1. Install the SSH plugin:

    If you haven't already, install the SSH plugin in Jenkins. Navigate to Jenkins > Manage Jenkins > Manage Plugins > Available and search for