Jenkins Installation and Freestyle Project

Installation of Jenkins

  1. Login to your AWS account and create a New EC2 Instance, and Connect to your EC2 instance via SSH

  2. Refer to Jenkins Official Installation Docs for the installation steps:- https://www.jenkins.io/doc/book/installing/linux/

    1. Upgrade all installed packages and Install Java, as Jenkins requires the Java Runtime Environment (JRE)

      • Upgrade all installed packages to their latest available versions.
        sudo yum upgrade -y

      • Installing Java 11 on Amazon Linux [Add required dependencies for the Jenkins package]

        sudo yum install java-11-openjdk

        If the above did not work, check for the Java packages available using
        yum list java| grep java- | grep devel
        yum install java-11-amazon-corretto-devel.x86_64 -y

      • Verify the Java Version:-
        java -version

    2. Install Jenkins

      • Download the Repo file jenkins.repo at yum repo directory [/etc/yum.repos.d/]
        sudo wget -O /etc/yum.repos.d/jenkins.repohttps://pkg.jenkins.io/redhat-stable/jenkins.repo

      • Get the Repolist
        sudo yum repolist

      • Import the cryptographic key of a package repository. This key is used to verify the integrity and authenticity of the packages obtained from that repository.
        sudo rpm --importhttps://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key

      • Verify the cryptographic key gets imported.
        rpm -qa gpg-pubkey-[KEY]
        Get detailed information about a specific GPG key in the RPM keyring, using

        sudo rpm -qi gpg-pubkey-[KEY]
        sudo rpm -qa gpg-pubkey | tail -1 | xargs -I {} rpm -qi {}

      • Install Jenkins
        sudo yum install jenkins -y
        sudo systemctl daemon-reload

      • You can enable the Jenkins service to start at boot with the command:

        sudo systemctl enable jenkins

      • You can start the Jenkins service with the command:

        sudo systemctl start jenkins

      • You can check the status of the Jenkins service using the command:

        sudo systemctl status jenkins

      • Check for errors if any

        sudo journalctl -xeu jenkins.service

      • Add Jenkins user to the Docker Group
        sudo usermod -aG docker jenkins

    3. By default, Jenkins runs on port 8080, so you'll need to open this port in your security group to access the Jenkins Web Interface.

      Allow Incoming Traffic for Port 8080, in AWS - Security Groups -> Inbound Rules

      • Specific My IP in Source which will be able to access the Jenkins UI.
        You can also specify your Bastion Host IP or VPN IP Or if you are using a Dedicated IP shared by your ISP.
        Additionally, you can check your IP here: showmyip.com

  1. Browse the URL in the browser
    http://<IP>:8080
    i.e
    http://54.87.45.80:8080/

  2. You should see the Jenkins login screen. If this is your first time accessing Jenkins, you will need to retrieve the initial administrative password.
    To do this, SSH into your EC2 instance and run the following command to retrieve the password from the Jenkins log file:

    sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Copy and paste the password into the Jenkins login screen and click "Continue". You should now be on the Jenkins home screen and can start using Jenkins.

  1. The setup prompts to either Install suggested plugins or Select plugins to install. Go with suggested plugins:

  2. Create First Admin User for your Jenkins administrator,
    then click Save and Continue

  3. Set up the Instance Configuration. This is the preferred network address for this Jenkins installation. Confirm the address you want to use for your server. This is most likely the same address you used to get to this configuration page.

  4. Now we are into the Jenkins dashboard.

    Create a freestyle pipeline

    1. In the Jenkins dashboard, Click on the New Item Or Create a job

    2. Enter the Name as per your project name i.e django-cicd-project and Select FreeStyle Project

    3. Configuration your Jenkin Job/Project, you have different options(like Source Code Management, Build Triggers, Build Environment, Build Steps, and Post-build Actions) that help you manage your job.

      • Provide a Description: This job will build a Django application.

      • Select GitHub project

        • Project URL : https://github.com/ketangharateG/react_django_demo_app

          • Advanced -> Display name: React Django App

    4. For Source Code Management select Git
      Repository URL: https://github.com/ketangharateG/react_django_demo_app.git

    5. Build Steps -> Execute shell -> Click on the Save button

    6. After this, you need to click on the Build Now button.
      On Clicking on the Build Now button, Jenkins will Pull the code from GitHub on your Machine/System, and whatever Build steps we have mentioned will be executed one by one.

    7. Check for the Build History and check for the Console Output

Since we are getting errors we have to install Git on the Jenkins Server
sudo yum install git -y

  1. Let's further modify our Build Steps, which will require the below steps on the Jenkins Server.

    • Install Docker
      yum install docker -y

    • Install Docker Compose
      wget [https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname](github.com/docker/compose/releases/latest/d.. -s)-$(uname -m)

      sudo mv docker-compose-$(uname -s)-$(uname -m) /usr/local/bin/docker-compose

      sudo chmod -v +x /usr/local/bin/docker-compose

    • Add Jenkins user to the Docker Group
      sudo usermod -aG docker jenkins

    • Reboot the Server

    • Allow incoming traffic for 8001 Port [Django Application Running on this port] on AWS - Security Groups

      Or

Built Steps -> Execute shell:-

echo "I have started my DevOps journey"

echo "Hello this is $USER and running this job"

echo "I will build the code using docker"
docker build . -t react-django-app
echo "I will perform test if required"

echo "I will deploy the code"
docker run -d -p 8001:8001 react-django-app:latest

Or

  1. After the build is successful, you will see your Application running on Port 8001

  2. We can also use just the below commands for build steps instead of previous build steps.

    Built Steps -> Execute shell:-
    echo "I have started my DevOps journey"
    echo "Hello this is $USER and running this job"
    echo "I will build the code using docker"
    docker-compose down
    echo "I will deploy the code"
    docker-compose up -d

    Note: docker-compose uses yaml file present in the project directory, the yaml file is built based on the Docker file present in the directory.