In this exercise, you will learn how to set up a basic CI/CD pipeline using Jenkins. This will involve creating a simple project, configuring Jenkins, and running your first build.

Objectives

  • Understand the basic components of a CI/CD pipeline.
  • Set up Jenkins on your local machine or a cloud service.
  • Create a simple Jenkins job to automate the build process.
  • Run the pipeline and verify the results.

Prerequisites

  • Basic understanding of CI/CD concepts.
  • Jenkins installed on your local machine or access to a Jenkins server.
  • A version control system (e.g., Git) installed and configured.

Step-by-Step Guide

Step 1: Install Jenkins

  1. Download Jenkins:

  2. Install Jenkins:

    • Follow the installation instructions for your operating system. For example, on Windows, run the installer and follow the prompts. On macOS, you can use Homebrew:
      brew install jenkins-lts
      
  3. Start Jenkins:

    • After installation, start Jenkins. On Windows, this might be done automatically. On macOS, you can start Jenkins with:
      brew services start jenkins-lts
      
  4. Access Jenkins:

    • Open a web browser and navigate to http://localhost:8080. You should see the Jenkins setup screen.

Step 2: Configure Jenkins

  1. Unlock Jenkins:

    • During the initial setup, Jenkins will ask for an unlock key. This key can be found in the initialAdminPassword file located in the Jenkins home directory. Copy the key and paste it into the setup screen.
  2. Install Suggested Plugins:

    • Jenkins will prompt you to install plugins. Choose "Install suggested plugins" to get started quickly.
  3. Create an Admin User:

    • Create an admin user by providing a username, password, and email address.
  4. Configure Jenkins URL:

    • Set the Jenkins URL to http://localhost:8080 or the appropriate URL if you are using a cloud service.

Step 3: Create a Simple Jenkins Job

  1. Create a New Job:

    • From the Jenkins dashboard, click on "New Item."
    • Enter a name for your job (e.g., "BasicPipeline") and select "Freestyle project." Click "OK."
  2. Configure Source Code Management:

    • In the job configuration screen, scroll down to the "Source Code Management" section.
    • Select "Git" and enter the repository URL of your project. For example, you can use a sample repository:
      https://github.com/your-username/sample-repo.git
      
    • If your repository is private, provide the necessary credentials.
  3. Add Build Steps:

    • Scroll down to the "Build" section and click "Add build step."
    • Select "Execute shell" (or "Execute Windows batch command" on Windows).
    • Enter a simple build command. For example, if you are using a Node.js project, you might use:
      npm install
      npm test
      
  4. Save the Job:

    • Click "Save" to save your job configuration.

Step 4: Run the Pipeline

  1. Build the Job:

    • From the job's main page, click "Build Now."
    • Jenkins will start the build process. You can monitor the progress in the "Build History" section.
  2. Verify the Results:

    • Once the build is complete, click on the build number (e.g., "#1") in the "Build History" section.
    • Click on "Console Output" to see the details of the build process.
    • Verify that the build steps were executed successfully.

Common Mistakes and Tips

  • Incorrect Repository URL: Ensure that the Git repository URL is correct and accessible.
  • Missing Dependencies: Make sure all necessary dependencies (e.g., Node.js, npm) are installed on the Jenkins server.
  • Permissions Issues: If using a private repository, ensure that Jenkins has the correct credentials to access it.

Conclusion

In this exercise, you set up a basic CI/CD pipeline using Jenkins. You learned how to install and configure Jenkins, create a simple job, and run a build. This foundational knowledge will help you as you progress to more advanced CI/CD practices.

Next, you will integrate automated tests into your pipeline to ensure code quality and reliability.

© Copyright 2024. All rights reserved