Step-by-Step Instructions for Establishing Jenkins with Agents for Effortless CI/CD ๐Ÿš€

ยท

3 min read

Step-by-Step Instructions for Establishing Jenkins with Agents for Effortless CI/CD ๐Ÿš€

Introduction:

In the dynamic world of DevOps, continuous integration and continuous deployment (CI/CD) are indispensable. Jenkins, a powerful automation server, plays a pivotal role in facilitating these processes. To enhance its capabilities, setting up Jenkins with agents becomes essential. In this blog post, we'll guide you through the process of creating a Jenkins master server and connecting it with agents on separate EC2 instances.

Prerequisites:

Before we dive into the setup, ensure you have two EC2 instances โ€“ one for the Jenkins master server and another for the agent. Here are the prerequisites for each:

  1. Jenkins Master Server:

    • Java: Ensure Java is installed on the Jenkins master server.

    • Jenkins: Install Jenkins on the master server by following the official installation instructions.

  2. Agent Server:

    • Java: Install Java on the agent server.

    • Additional Tools: Install any project-specific tools required for the agent on the server, such as Docker, Docker Compose, Trivy, etc.

SSH Connection Setup:

  1. Generate an Ed25519 SSH key pair on the master server using ssh-keygen -t ed25519. Copy the public key.

  2. On the agent server, navigate to ~/.ssh/ and edit the authorized_keys file using sudo vim authorized_keys. Paste the copied public key.

  3. Create SSH credentials in Jenkins:

    • Navigate to Dashboard > Manage Jenkins > Credentials > System > Global credentials (unrestricted).

    • Add new credentials of type "SSH Username with private key."

    • Enter the username (e.g., ubuntu) and add the private key content.

Creating Jenkins Node:

  1. Go to Dashboard > Manage Jenkins > Nodes > New Node.

  2. Set the Node Name (e.g., dev-server) and choose "Permanent Agent."

  3. Provide a description, which helps identify the purpose or characteristics of the node.

  4. Set the remote root directory (e.g., /home/ubuntu/dev-workspace), indicating the location where the agent thinks it will store its workspace files.

  5. Labels:

    • Labels are crucial for identifying and categorizing agents. They act as tags that help Jenkins understand which agent to assign a specific job or pipeline.

    • For example, you might label an agent as 'dev-agent' to signify that it's suitable for development tasks.

  6. Choose the option to "Only build jobs with label expressions matching this node."

  7. For Launch Method, select "Launch agents via SSH."

  8. Enter the public IP of the agent server and choose "Non-verifying Verification Strategy" for the Host Key Verification Strategy.

  9. Keep other configurations as default and save the node.

  10. Launch the agent, and monitor the console output for any errors or success messages.

Sample Pipeline Code:

pipeline {
    agent {label: 'dev-agent'}
    stages {
        stage("Code") {
            steps {
                echo 'Something here'
            }
        }
    }
}

Special Thanks: ๐ŸŒŸ
A big shoutout and heartfelt thanks to Train With Shubham (TWS) or DevOps wale bhaiya for their invaluable contributions. ๐Ÿ™Œ

Conclusion:

Setting up Jenkins with agents establishes a robust CI/CD foundation. This ensures efficient and scalable automation, crucial for modern software development. ๐ŸŒโœจ

Did you find this article valuable?

Support SagarOps by becoming a sponsor. Any amount is appreciated!

ย