๐Ÿ“ Configuring Jenkins Declarative Pipeline with AWS Cloud ๐Ÿš€

๐Ÿ“ Configuring Jenkins Declarative Pipeline with AWS Cloud ๐Ÿš€

ยท

5 min read

๐Ÿ“ Introduction:

Are you looking to configure Jenkins's declarative pipeline using AWS Cloud? ๐Ÿค” Look no further! In this blog, I will guide you through the step-by-step process with detailed instructions and commands. Let's get started! โš™๏ธ

๐Ÿ”น Step 1: Start an EC2 Server with Enabled Ports

To begin, launch an EC2 server and make sure to enable inbound rules for ports 22, 8080, and 8000. These ports are crucial for successful configuration.

๐Ÿ”น Step 2: Connect to the Server via SSH

Next, establish a connection to the server using the SSH command. This will allow you to interact with the server's command line.

๐Ÿ”น Step 3: Update the Server

Once connected, update the server by running the following commands:

sudo apt update
git clone https://github.com/sumanprasad007/django-notes-app-with-database.git
cd django-notes-app-with-database
sudo apt install git docker.io -y
sudo usermod -aG docker ubuntu
sudo reboot

After the server reboots, reconnect using SSH.

Verify that Ubuntu has permission to run Docker by executing docker images command.

๐Ÿ”น Dockerfile

Create a Dockerfile with the following content:

FROM python:3.9
WORKDIR /app/backend
COPY requirements.txt /app/backend
RUN pip install -r requirements.txt
COPY . /app/backend
EXPOSE 8000
CMD python /app/backend/manage.py runserver 0.0.0.0:8000

Build the Docker image by running:

docker build -t my-note-app .
docker images

๐Ÿ”น Step 4: Install Java JDK and Jenkins Using Shell Script

To install Java JDK and Jenkins, follow these steps:

  1. Make the Jenkins installation script executable:
chmod +x jenkins-installation.sh
  1. Run the Jenkins installation script:
./jenkins-installation.sh

Verify the Jenkins installation by checking its version:

jenkins --version

Grant Jenkins access to build and run Docker images:

sudo usermod -aG docker jenkins
sudo reboot

Access Jenkins by entering your EC2 instance's public IP followed by port 8080 in your browser. Obtain the initialAdminPassword by running:

cat /var/lib/jenkins/secrets/initialAdminPassword

Copy and paste the password in the provided tab and install all the necessary plugins.

๐Ÿ”น Step 5: Creating a Declarative Pipeline

Now, let's create a declarative pipeline in Jenkins:

  1. Click on "New Item" and name it "my-note-app-cicd-pipeline".

  2. Under the configuration settings, select the GitHub project and provide the repository URL:

https://github.com/sumanprasad007/django-notes-app-with-database.git

Set the name to "note-app".

  1. Enable the "Build Triggers" option by ticking the box next to "GitHub hook trigger for GITScm pooling".

  2. In the "Pipeline" section, choose "Pipeline Script" and add the following script:

pipeline {
    agent any 

    stages{
        stage("Clone Code"){
            steps {
                echo "Cloning the code"
                git url:"https://github.com/sumanprasad007/django-notes-app-with-database.git", branch: "main"
            }
        }
        stage("Build"){
            steps {
                echo "Building the image"
                sh "docker build -t my-note-app ."
            }
        }
        stage("Push to Docker Hub"){
            steps {
                echo "Pushing the image to Docker Hub"
                withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                sh "docker tag my-note-app ${env.dockerHubUser}/my-note-app:latest"
                sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                sh "docker push ${env.dockerHubUser}/my-note-app:latest"
                }
            }
        }
        stage("Deploy"){
            steps {
                echo "Deploying the container"
                sh "docker-compose down && docker-compose up -d"

            }
        }
    }
}

โœ” Before building, make sure to follow these additional steps:

  1. Add DockerHub credentials in the global credentials section with the ID as "dockerHub" along with the username and password.

  2. Create a Docker Compose file to enhance the deployment of Docker containers:

version: "3.3"
services:
  web:
    image: sumanprasad007/my-note-app:latest
    ports:
      - "8000:8000"
  1. Install Docker Compose before running the pipeline.

Once everything is set up, you can manually trigger the pipeline by clicking the build button in the Jenkins interface.

๐Ÿ”น Tadada: Our Note App is Successfully Deployed! ๐ŸŽ‰

Congratulations! Your Note App is now deployed and ready to use. Enjoy the benefits of automation and continuous integration.

๐Ÿ”น Bonus Step: Full Automation with Webhooks

To achieve full automation, configure webhooks so that any changes pushed to the GitHub repository will trigger the Jenkins pipeline and initiate the deployment process automatically.

๐Ÿ”น Further Simplification with "Pipeline Script from SCM"

To make your pipeline more effective and easier to modify, you can use "Pipeline Script from SCM." This approach allows you to provide a GitHub repository link containing the Jenkinsfile. By adopting this method, any developer in your team with proper permissions can review and modify the Jenkinsfile, simplifying the pipeline management process.

๐Ÿ“„ Jenkinsfile:

Auto-trigger Jenkins pipeline using webhook

New commit: changed the title "Hello TWS Community Builders"

By leveraging webhooks and the "Pipeline Script from SCM" approach, your team can work collaboratively on the Jenkinsfile and make changes with ease, eliminating the need to access the Jenkins dashboard.

That's it! You have now successfully configured a Jenkins declarative pipeline using AWS Cloud. ๐Ÿš€ Embrace automation and streamline your development workflow with this powerful setup. Happy coding! ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

๐Ÿ”’ Conclusion ๐Ÿ”‘

In this blog, we explored the step-by-step process of configuring a Jenkins declarative pipeline using AWS Cloud. By following the outlined instructions, you can automate your development workflow, enhance collaboration, and achieve continuous integration. Let's summarize the key points covered:

1๏ธโƒฃ We started by setting up an EC2 server with the necessary ports enabled, allowing us to establish a secure connection.

2๏ธโƒฃ After connecting to the server via SSH, we updated the server, installed Git, and Docker, and cloned the project repository.

3๏ธโƒฃ Dockerfile was created to define the Docker image configuration, and we built the image using the Docker build command.

4๏ธโƒฃ Java JDK and Jenkins were installed using a shell script, granting Jenkins access to build and run Docker images.

5๏ธโƒฃ We accessed Jenkins through the browser, installed the required plugins, and created a declarative pipeline to automate the CI/CD process.

6๏ธโƒฃ The pipeline consisted of stages such as cloning the code, building the image, pushing it to Docker Hub, and deploying the container using Docker Compose.

7๏ธโƒฃ We discussed additional steps, including adding DockerHub credentials, utilizing a Docker Compose file, and triggering the pipeline with webhooks.

8๏ธโƒฃ Lastly, we highlighted the benefits of using "Pipeline Script from SCM" to simplify pipeline modifications and encourage team collaboration.

By implementing this Jenkins declarative pipeline configuration, you can streamline your software development process, reduce manual efforts, and achieve faster and more reliable deployments. Embracing automation and cloud technologies allows your team to focus on innovation and delivering high-quality software products.

๐Ÿ” Checkout GitHub Repository for projects:

๐Ÿ”— github.com/sumanprasad007

#Jenkins #Automation #AWS #DevOps #ContinuousIntegration #Cloud

Did you find this article valuable?

Support Prasad Suman Mohan by becoming a sponsor. Any amount is appreciated!

ย