Day 4 of 90 Days of DevOps: Resetting the Jenkins Password on Windows and Setting Up CI with Jenkins
Tags: jenkins, devops, 90daysofdevops, tutorial
Published: 2024-06-22 02:15:52

I guess from today, I've learned my lesson to take my password manager app more seriously. I had issues logging into my Jenkins dashboard and had to delete Jenkins because of the previous configurations. I also deleted my Java installations (both version 17 and 21). I struggled with the Jenkins settings, trying different passwords that didn’t work. I guess I was snoring the day I created the Jenkins admin account. Here’s the process I followed to reset my Jenkins password on a Windows machine. I'm still working on the application, and I’ll update you guys on how it finally goes. Thank you for stopping by!

### Resetting the Jenkins Password

Resetting the Jenkins password or disabling security temporarily on a Windows machine requires a specific approach. Here’s a step-by-step guide to achieve this:

#### Stopping Jenkins Service
- Open the Services management console by pressing `Win + R`, typing `services.msc`, and pressing Enter.
- Find the Jenkins service, right-click on it, and select `Stop`.

#### Locating Jenkins Home Directory
- Navigate to the Jenkins home directory (usually `C:\ProgramData\Jenkins`).

#### Editing `config.xml`
- Open the `config.xml` file in the Jenkins home directory with a text editor.
- Find the `<useSecurity>` element and change its value from `true` to `false`.
  ```xml
  <useSecurity>false</useSecurity>
  ```
- Save the changes and close the text editor.

#### Starting Jenkins Service
- Go back to the Services management console.
- Find the Jenkins service, right-click on it, and select `Start`.

#### Accessing Jenkins
- Open your browser and navigate to your Jenkins URL (usually `http://localhost:8080` but I used 8084).
- Log in to Jenkins (no password required since security is disabled).

#### Re-enabling Security
- Navigate to `Manage Jenkins` -> `Configure Global Security`.
- Re-enable security and set a new admin password.
- Save the configuration.

---

### Day 4: Setting Up CI with Jenkins

In Day 4 of our project, we focus on setting up Continuous Integration (CI) with Jenkins and integrating it with a GitHub project. This guide will help you install Jenkins, configure it with GitHub, and structure your project folders appropriately.

#### Installing Jenkins

**Prerequisites:**
- A Windows machine.
- Java Development Kit (JDK) installed.
- Access to the machine.

**Install Jenkins:**
- Download Jenkins from the [official website](https://www.jenkins.io/download/).
- Run the installer and follow the on-screen instructions.
- Once installed, Jenkins will automatically start as a service.

**Initial Setup:**
- Open a browser and navigate to `http://localhost:8080`.
- Retrieve the initial admin password from the Jenkins installation directory (e.g., `C:\Program Files (x86)\Jenkins\secrets\initialAdminPassword`).
- Complete the setup wizard by installing recommended plugins and creating an admin user.

#### Configuring Jenkins to Integrate with GitHub

**Install the GitHub plugin:**
- Go to Jenkins dashboard -> Manage Jenkins -> Manage Plugins.
- In the Available tab, search for "GitHub" and install "GitHub Plugin".

**Generate a GitHub token:**
- Navigate to GitHub -> Settings -> Developer settings -> Personal access tokens.
- Generate a new token with the name "Jenkins CI" and select appropriate scopes.

#### Creating a Jenkins Job

**New Jenkins Task:**
- In the Jenkins dashboard, click “New Item”.
- Enter an item name suitable for your project and select "Pipeline", then click "OK".
- Select the GitHub project checkbox and set the Project URL to your GitHub Repository URL.

**Configure Source Code Management:**
- Under the Source Code Management tab, select "Git".
- Set the Repository URL to your GitHub repository and add your GitHub credentials.

**Build Triggers:**
- Under the Build Triggers tab, select “Build when a change is pushed to GitHub”.

**Build Steps:**
- Add a build step to execute shell commands for building and testing your application. For Windows, use "Execute Windows batch command".

#### Defining and Structuring Your Project

**Folder Structure:**
Organize your project in a standard Maven format:
```
├── Jenkinsfile
├── README.md
├── src
│   ├── main
│   │   └── java
│   │       └── com
│   │           └── jenkinsfour
│   │               └── App.java
│   └── test
│       └── java
│           └── com
│               └── jenkinsfour
│                   └── AppTest.java
└── pom.xml
```

**Jenkinsfile:**
Create a `Jenkinsfile` at the root of your repository. This file defines the stages of your Jenkins Pipeline:
```groovy
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                bat 'mvn clean package'
            }
        }
        stage('Test') {
            steps {
                bat 'mvn test'
            }
        }
        stage('Deploy') {
            steps {
                bat 'echo "Deploying..."'
            }
        }
    }
}
```
Check this file into your GitHub repository.

#### Running the Pipeline

**Trigger the Job:**
- Push changes to GitHub, which will automatically trigger the job in Jenkins based on the configured GitHub webhook.
- Jenkins will execute the stages defined in the Jenkinsfile.

---

### Challenges and Solutions

**Challenge 1: Jenkins Service Issues**
- Sometimes, the Jenkins service might not stop or start correctly. Ensure you have administrative privileges and check the service logs for detailed error messages.

**Solution:**
- Use the Windows Event Viewer to check for specific error messages and troubleshoot accordingly.

**Challenge 2: Configuration Errors**
- Incorrectly editing the `config.xml` file can cause Jenkins to fail on startup.

**Solution:**
- Always backup the `config.xml` file before making changes. Ensure XML syntax is correct after editing.

**Challenge 3: GitHub Integration**
- Issues with GitHub integration often arise from incorrect token permissions or repository URL configurations.

**Solution:**
- Double-check GitHub token permissions and ensure the repository URL in Jenkins is accurate.

**Challenge 4: Maven Configuration**
- Errors might occur if Maven is not correctly configured on the Windows machine.

**Solution:**
- Ensure the JAVA_HOME and M2_HOME environment variables are set correctly and included in the system PATH.

By following these steps and considering the common challenges and solutions, you can successfully set up CI with Jenkins for your GitHub project, ensuring a smooth and automated development workflow.

---

I hope this guide helps you get started with Jenkins and Maven on your Windows machine. I'll keep you updated as I progress further with the application. Thanks for stopping by!