JENKINS Tutorials

 




1. Introduction to Jenkins

Jenkins is a widely used open-source automation server that helps you build, test, and deploy applications. It provides hundreds of plugins to support building, deploying, and automating your projects.


2. Prerequisites

  • Operating System: Linux (Ubuntu/Debian commands are used here)
  • Java: Jenkins requires Java (preferably Java 11 or later).
  • Internet Connection: For downloading packages, plugins, and source code.

3. Installing Jenkins

3.1 Update Your System and Install Java

First, update your package lists and install Java. Jenkins typically runs on Java 11.

bash
sudo apt-get update sudo apt-get upgrade -y sudo apt-get install -y openjdk-11-jdk

Check the Java version to confirm installation:

bash
java -version

3.2 Add Jenkins Repository and Key

  1. Add the Jenkins Debian repository key:

    bash
    curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \ /usr/share/keyrings/jenkins-keyring.asc > /dev/null
  2. Add the Jenkins repository:

    bash
    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
  3. Update the package database:

    bash
    sudo apt-get update

3.3 Install Jenkins

Now install Jenkins with:

bash
sudo apt-get install -y jenkins

3.4 Start and Enable Jenkins Service

Start Jenkins immediately and enable it to start on boot:

bash
sudo systemctl start jenkins sudo systemctl enable jenkins

3.5 Adjust the Firewall (Optional)

If you’re using UFW (Uncomplicated Firewall), allow traffic on port 8080 (default for Jenkins):

bash
sudo ufw allow 8080 sudo ufw status

4. Accessing and Initial Setup

4.1 Open the Jenkins Web Interface

Open a browser and navigate to:

cpp
http://<your-server-ip>:8080

4.2 Unlock Jenkins

The first time you access Jenkins, it will ask for an initial admin password. Retrieve it using:

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

Copy the password from the terminal and paste it into the browser prompt.

4.3 Customize Jenkins

After unlocking, you can:

  • Install Suggested Plugins: This installs a set of common plugins.
  • Create the First Admin User: Set up your username, password, and full name.
  • Instance Configuration: Confirm the URL (e.g., http://<your-server-ip>:8080).

5. Configuring Jenkins

5.1 Install Additional Plugins

You can install more plugins as needed:

  • Go to Manage Jenkins > Manage Plugins.
  • Browse the Available tab to install plugins like Git, Pipeline, Blue Ocean, etc.

5.2 Global Tool Configuration

Under Manage Jenkins > Global Tool Configuration, configure:

  • JDK: Point to your installed Java (if not auto-detected).
  • Git: Ensure Jenkins knows where Git is installed (often /usr/bin/git).

6. Creating Your First Job

6.1 Creating a Freestyle Project

  1. Create New Job:

    • Click New Item.
    • Enter a name (e.g., MyFirstJob) and select Freestyle project.
    • Click OK.
  2. Configure the Job:

    • Source Code Management: If using Git, select Git and enter your repository URL.
    • Build Triggers: Optionally set up triggers like Poll SCM.
    • Build Steps: Add a build step (e.g., “Execute shell”) and enter a command:
      bash
      echo "Hello, Jenkins!"
    • Post-build Actions: Optionally add actions like archiving artifacts.
  3. Save and Run:

    • Click Save.
    • On the job’s page, click Build Now.
    • Monitor progress by clicking on the build number and then Console Output.

7. Using Jenkins Pipeline

7.1 Creating a Pipeline Job

  1. Create a New Pipeline:

    • Click New Item, enter a name (e.g., MyPipeline), and select Pipeline.
    • Click OK.
  2. Define Your Pipeline Script:

    • In the Pipeline section, choose Pipeline script.
    • Enter a simple pipeline script:
      groovy
      pipeline { agent any stages { stage('Build') { steps { echo 'Building...' } } stage('Test') { steps { echo 'Testing...' } } stage('Deploy') { steps { echo 'Deploying...' } } } }
  3. Save and Run:

    • Click Save.
    • Click Build Now.
    • Monitor the pipeline progress under the Blue Ocean view (if installed) or by clicking the build and viewing its logs.

8. Integrating Jenkins with Git

8.1 Configure Git Integration

  1. Install the Git Plugin:

    • In Manage Jenkins > Manage Plugins, ensure the Git plugin is installed.
  2. Create a Job with Git SCM:

    • When configuring a new Freestyle or Pipeline job, go to Source Code Management.
    • Select Git and enter your repository URL.
    • If needed, add credentials by clicking Add next to the Credentials field.
  3. Using a Jenkinsfile from a Repository:

    • Create a Pipeline job and select Pipeline script from SCM.
    • Choose Git as the SCM, enter the repository URL, and specify the branch.
    • Set the Script Path (default is Jenkinsfile).

9. Advanced Jenkins Configurations

9.1 Managing Plugins and Updates

  • Regularly update plugins via Manage Jenkins > Manage Plugins > Updates.
  • Explore additional plugins to support various build, test, and deployment strategies.

9.2 Distributed Builds with Jenkins Agents

  1. Set Up an Agent:

    • In Manage Jenkins > Manage Nodes and Clouds, click New Node.
    • Configure the node details (remote machine’s name, remote root directory, etc.).
  2. Launch Agent:

    • Follow the instructions provided by Jenkins to launch the agent (often via a Java Web Start or SSH).

9.3 Backup and Maintenance

  • Backup Jenkins Configuration:
    • Regularly back up the /var/lib/jenkins directory.
  • Upgrade Jenkins:
    • Update Jenkins via your package manager:
      bash
      sudo apt-get update sudo apt-get upgrade jenkins

10. Final Thoughts

  • Explore Further:
    Jenkins is highly extensible. Look into creating shared libraries for pipelines, using Blue Ocean for a modern UI, and automating complex workflows.

  • Documentation and Community:
    Refer to the official Jenkins documentation and community forums for advanced topics and troubleshooting.

This tutorial should help you get started with Jenkins—from installation and initial configuration to creating jobs and setting up pipelines for continuous integration and continuous delivery (CI/CD). Enjoy automating your builds and deployments!

Comments