Skip to main content

Command Palette

Search for a command to run...

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

Published
β€’5 min read
πŸ“ Configuring Jenkins Declarative Pipeline with AWS Cloud πŸš€
P

πŸ‘‹ Hello! I'm passionate about DevOps and have over 1+ years of experience in the field. I'm proficient in a variety of cutting-edge technologies and always motivated to expand my knowledge and skills. Let's connect and grow together!

SKILLS:

πŸ”Ή Languages & Runtimes: Python, Shell Scripting, HCL, YAML πŸ”Ή Cloud Technologies: AWS, Microsoft Azure, GCP πŸ”Ή Infrastructure Tools: Docker, Terraform, AWS CloudFormation πŸ”Ή Other Tools: Linux, Git and GitHub Actions, Jenkins, Jira, GitLab (beginner), Docker, AWS DevOps πŸ”Ή Web Development: HTML, CSS, Bootstrap, Python, SQL

Job & Responsibilities:

πŸš€ Improved development efficiency by implementing CI/CD pipelines, resulting in a 30% reduction in deployment time on the test server. πŸ”’ Strengthened deployment and testing reliability by utilizing Docker containers and optimizing Dockerfile, reducing development issues on the test server by 20%. βš™οΈ Automated S3 bucket log creation with Shell scripting, eliminating 100% of manual search and saving 2 hours per week. πŸ“… Scheduled EC2 instance start/stop using Lambda functions and Event Bridge, leading to a 25% decrease in infrastructure costs. πŸ”§ Utilized AWS, Linux, Python, Docker, Shell scripting, Terraform, Jenkins Pipelines, and automation to streamline workflows and improve overall system performance.

I'm very detail-oriented and possess strong written and verbal communication skills. As a high performer with a possibility mindset, I strive to solve problems using efficient approaches.

Let's Connect & Grow:

If you find my profile suitable for the role you are searching for, please feel free to reach out to me at sumanprasad9766@gmail.com.

πŸ“ 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

More from this blog

D

DeployToCloud

405 posts

πŸ‘‹ Welcome to my Hashnode blog! I'm a DevOps Engineer with 2+ years of experience. Join ~5k followers and explore 320+ blogs on Python, AWS, Docker, Jenkins, Linux, and more. Let's connect & grow πŸš€