Jenkins Declarative

Log in to AWS Account.

Launch an Ubuntu Instance:Jenkins Declarative

chmod 400 jenkins-d.pem(to give the permission)

ssh -i "jenkins-d.pem" ubuntu@ec2-54-209-145-41.compute-1.amazonaws.com

Clone the Code

installing Docker

sudo apt-get update

sudo apt-get install docker.io

sudo usermod -aG docker $USER(or ubuntu)--if permission denied

we are adding the user to the group called docker.-->user permission

sudo reboot

Creating Dockerfile

FROM python:3.9

WORKDIR /app/backend #current directory

COPY requirements.txt /app/backend (copy the requirements.txt to working dir)

RUN pip install -r requirements.txt

COPY . /app/backend

EXPOSE 8000

CMD python /app/backend/manage.py runserver 0.0.0.0:8000

requirement.txt contain all prerequisite(tools to run this project).

Docker Build

docker build -t notes-app .

Installing Jenkins

Update your system

sudo apt update

Install java

sudo apt install openjdk-11-jre

Validate Installation

java -version

It should look something like this

openjdk version "11.0.12" 2021-07-20 OpenJDK Runtime Environment (build 11.0.12+7-post-Debian-2) OpenJDK 64-Bit Server VM (build 11.0.12+7-post-Debian-2, mixed mode, sharing)

Step - 2 Install Jenkins

Just copy these commands and paste them onto your terminal.

curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee \   /usr/share/keyrings/jenkins-keyring.asc > /dev/null echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \   https://pkg.jenkins.io/debian binary/ | sudo tee \   /etc/apt/sources.list.d/jenkins.list > /dev/nullsudo apt-get update sudo apt-get install jenkins

Step -3 Start jenkins

sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins

Or Copy the below Code and Paste it.

sudo apt update
sudo apt install openjdk-11-jre

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins
service jenkins status

Browse to:public ip:8080

Go to Security group of your Instance and allow the port:8080

Jenkins Ready

CICD Pipeline

Creating Pipelines:

Groovy Syntax:

pipeline {
    agent any

    stages{
        stage("Code"){
            steps{
            echo"Cloning the code"
            }
        }
        stage("build"){
            steps{
            echo"Building the image"
            }
        }
        stage("Push to Docker Hub"){
            steps{
            echo"Pushing the image to docke hub"
            }
        }
        stage("Deploy"){
            steps{
            echo"Deploying the container"
            }
        }
    }
}
pipeline {
    agent any

    stages{
        stage("Code"){
            steps{
            echo"Cloning the code"
            sh "git clone https://github.com/devkrgoutam/django-notes-app.git"
            }
        }
        stage("build"){
            steps{
            echo"Building the image"
            }
        }
        stage("Push to Docker Hub"){
            steps{
            echo"Pushing the image to docke hub"
            }
        }
        stage("Deploy"){
            steps{
            echo"Deploying the container"
            }
        }
    }
}

Login to Docker Hub

sudo usermod -aG docker $USER
sudo reboot

To Create Enviornment:

Go to -->Manage Jenkins-->security -->Credentials-->System

Global Credentials:

pipeline {
    agent any

    stages{
        stage("Code"){
            steps{
            echo"Cloning the code"
            git url:"https://github.com/devkrgoutam/django-notes-app.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 docke hub"
            withCredentials([usernamePassword(credentialsID:"dockerhub",passwordVariable:"dockerhubpass",usernamVariable:"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 run -d -p 8000:8000 gouutam/my-note-app:latest"
            }
        }
    }
}

Installing Docker Compose

sudo apt-get install docker-compose

docker compose file
version : "3.3"
services : 
    web :
        build : .
        ports :
            - "8000:8000"
docker compose file
version : "3.3"
services : 
    web :
        image : gouutam/my-note-app:latest
        ports :
            - "8000:8000"

docker-compose up

docker-compose down

pipeline {
    agent any

    stages{
        stage("Code"){
            steps{
            echo"Cloning the code"
            git url:"https://github.com/devkrgoutam/django-notes-app.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 docke hub"
            withCredentials([usernamePassword(credentialsID:"dockerhub",passwordVariable:"dockerhubpass",usernamVariable:"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 run -d -p 8000:8000 gouutam/my-note-app:latest"
            }
        }
    }
}

Pipeline from SCM

Goto Github-->Create jenkinsfile-->Copy the above code to jekinsfile-->commit

Configure Github-Webhook for Automation