DMCA.com Protection Status Trending Topics About Devops

Tuesday, 15 July 2025

How to Use and Configure Jenkins

 

1. Install Latest JDK (OpenJDK 17+ recommended)

bash
sudo apt update sudo apt install openjdk-17-jdk -y java -version
  • Installs OpenJDK 17.

  • java -version confirms the installation.


2. Add Jenkins Repository and Key

bash
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add - sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list' sudo apt update
  • Adds Jenkins official repo and key.


3. Install Jenkins

bash
sudo apt install jenkins -y

4. Start and Enable Jenkins Service

bash
sudo systemctl start jenkins sudo systemctl enable jenkins sudo systemctl status jenkins
  • Starts Jenkins and enables it to run on boot.

  • status checks if Jenkins is running.


5. Access Jenkins Web UI

  • Open browser: http://<VM-IP>:8080

  • Get initial admin password:

bash
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
  • Use this password to unlock Jenkins in the browser and continue setup.


6. Jenkins CLI Commands (using jenkins-cli.jar)

a) Download Jenkins CLI

bash
wget http://<VM-IP>:8080/jnlpJars/jenkins-cli.jar

b) List all jobs

bash
java -jar jenkins-cli.jar -s http://<VM-IP>:8080 list-jobs

c) Trigger a build for a job

bash
java -jar jenkins-cli.jar -s http://<VM-IP>:8080 build <job-name>

d) Get job configuration XML

bash
java -jar jenkins-cli.jar -s http://<VM-IP>:8080 get-job <job-name> > config.xml

e) Create a new job from XML config

bash
java -jar jenkins-cli.jar -s http://<VM-IP>:8080 create-job <new-job-name> < config.xml

f) Enable/Disable a job

bash
java -jar jenkins-cli.jar -s http://<VM-IP>:8080 disable-job <job-name> java -jar jenkins-cli.jar -s http://<VM-IP>:8080 enable-job <job-name>

g) Get build logs for a job

bash
java -jar jenkins-cli.jar -s http://<VM-IP>:8080 console <job-name> <build-number>




1. Configure System

Step 1: Open Jenkins dashboard

  • Go to http://<your-jenkins-ip>:8080

  • Login with admin user.

Step 2: Navigate to Manage Jenkins > Configure System

  • Click Manage Jenkins from the left menu

  • Click Configure System

Step 3: Set Jenkins URL

  • Under Jenkins Location → set Jenkins URL to your Jenkins server URL (e.g., http://your-jenkins-server:8080)

  • This URL is used in notifications and links.

Step 4: Add JDK installation

  • Scroll to JDK installations section

  • Click Add JDK

  • Enter name (e.g., OpenJDK 17)

  • Check Install automatically and select JDK version OR provide local path if installed manually

Step 5: Configure Tool Locations (optional)

  • For Maven, Git, Gradle, specify executable paths if Jenkins does not auto-detect.

Step 6: Configure Email Notification

  • Scroll to Extended E-mail Notification or E-mail Notification section

  • Enter SMTP server (e.g., smtp.gmail.com)

  • Enter default user email suffix if required

  • Test email sending using Test configuration by sending test e-mail

Step 7: Save changes

  • Click Save at the bottom.


2. Configure Global Security

Step 1: Go to Manage Jenkins > Configure Global Security

  • Click Manage Jenkins > Configure Global Security

Step 2: Enable Security

  • Check Enable security checkbox

Step 3: Configure Authentication

  • Select authentication method:

    • Jenkins’ own user database (default)

    • LDAP or Active Directory (if integrated with company network)

Step 4: Configure Authorization

  • Choose authorization strategy:

    • Matrix-based security: granular control over who can do what

    • Project-based Matrix Authorization

    • Logged-in users can do anything (simple)

    • Anyone can do anything (not recommended for production)

Step 5: Configure Agent Protocols and Security (optional)

  • Under Agent-to-master security subsystem, keep secure protocols enabled

  • Check Prevent Cross Site Request Forgery exploits

Step 6: Save configuration

  • Click Save


3. Manage Credentials

Step 1: Navigate Manage Jenkins > Credentials

  • Click Manage Jenkins > Manage Credentials

Step 2: Select domain or global credentials

  • Select appropriate domain (e.g., Global) or create new domain

Step 3: Add Credentials

  • Click Add Credentials

  • Choose credential type:

    • Username with password

    • SSH Username with private key

    • Secret text (API tokens, passwords)

    • Certificate

Step 4: Enter required details and save

  • Provide ID and description for easy identification in jobs


4. Manage Plugins

Step 1: Go to Manage Jenkins > Manage Plugins

  • Click Manage Jenkins > Manage Plugins

Step 2: Install new plugins

  • Open Available tab

  • Search and select plugins (e.g., Git, Pipeline, Docker plugins)

  • Click Install without restart or Download now and install after restart

Step 3: Update existing plugins

  • Open Updates tab

  • Select plugins to update and click Download now and install after restart

Step 4: Plugin management

  • Enable/disable installed plugins from Installed tab


5. Manage Nodes and Clouds (Agents)

Step 1: Go to Manage Jenkins > Manage Nodes and Clouds

  • Click Manage Jenkins > Manage Nodes and Clouds

Step 2: Add new node

  • Click New Node

  • Enter node name, select Permanent Agent, click OK

Step 3: Configure node

  • Enter remote root directory (e.g., /home/jenkins)

  • Set number of executors (parallel jobs it can run)

  • Set labels for job assignment

  • Choose launch method (SSH is recommended)

  • Provide SSH host, credentials, and test connection

Step 4: Save node configuration

  • Click Save


6. System Logs and Monitoring

Step 1: Go to Manage Jenkins > System Log

  • View Jenkins runtime logs here

  • Add Log Recorders to track specific classes/packages for debugging

Step 2: Load statistics

  • View executor load and system resource consumption


7. Using Script Console

Step 1: Manage Jenkins > Script Console

  • Execute Groovy scripts for administration tasks

  • Example: List all jobs

    groovy
    Jenkins.instance.getAllItems(Job.class).each { println it.name }

Summary Tips

  • Always backup Jenkins config files (usually in /var/lib/jenkins) before major changes.

  • Regularly update Jenkins core and plugins for security and features.

  • Use credentials plugin to securely manage secrets.

  • Use matrix-based security for fine-grained user permissions.

  • Label nodes properly to assign jobs efficiently.