4

 sudo apt update

sudo apt upgrade -y


sudo wget -O /etc/apt/keyrings/jenkins-keyring.asc \

  https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key


echo "deb [signed-by=/etc/apt/keyrings/jenkins-keyring.asc]" \

  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \

  /etc/apt/sources.list.d/jenkins.list > /dev/null


sudo apt update

sudo apt install jenkins -y


sudo systemctl start jenkins

sudo systemctl enable jenkins

sudo systemctl status jenkins

localhost:8080

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

Copy the displayed password and paste it into the "Administrator password" field in your browser. Click "Continue".
On the next screen, click "Install suggested plugins" to install a recommended set of plugins.
After the plugins are installed, you will be asked to create an administrator user. Fill in the required details and click "Save and Finish".
You should now see the Jenkins dashboard, ready for use.

Prepare Your Project on GitHub:

2. Log in to Jenkins

Open your web browser and go to your Jenkins instance (e.g., localhost:8080). Log in with the administrator credentials you created during the initial setup.

Create a New Jenkins Job (Freestyle Project)

  1. On the Jenkins dashboard, click "New Item" (or "Create a job" if it's a fresh installation).
  2. Enter an item name (e.g., Maven-GitHub-Freestyle or Gradle-GitHub-Freestyle).
  3. Select "Freestyle project".
  4. Click "OK".

    Configure Source Code Management (SCM) for Freestyle

    In the job configuration page for your Freestyle project:

  5. Scroll down to the "Source Code Management" section.
  6. Select "Git".
  7. In the "Repository URL" field, enter the URL 
    1. For Maven: https://github.com/devops-ds/your-maven-project.git
    2. For Gradle: https://github.com/devops-ds/your-gradle-project.git
  8. If you are using your own private repository, you will need to add credentials. Click "Add" next to "Credentials" and provide your GitHub username and password or a personal access token. If using the provided public repositories, no credentials are needed.
Under "Branches to build", ensure */main is specified. If your repository uses a different default branch (like */master), adjust this accordingly. For the provided public repositories, */main is correct.

5. Configure the Build Step for Freestyle

This is where you tell Jenkins how to build your project using Maven or Gradle.

If you are building a Maven Project:Scroll down to the "Build" section.
Click "Add build step".
Select "Execute shell" (or "Execute Windows batch command" on Windows).

In the "Command" area, enter the full path to your Maven executable followed by the goals you want to run (e.g., clean install).

To find the full path to your mvn executable on open terminal in Ubuntu and run: 

5. Configure the Build Step for Freestyle

This is where you tell Jenkins how to build your project using Maven or Gradle.

  • If you are building a Maven Project:
Scroll down to the "Build" section.
Click "Add build step".
Select "Execute shell" (or "Execute Windows batch command" on Windows).

In the "Command" area, enter the full path to your Maven executable followed by the goals you want to run (e.g., clean install).

To find the full path to your mvn executable on open terminal in Ubuntu and run: which mvn

The output will give you the full path (e.g., /usr/bin/mvn or /opt/maven/bin/mvn). Use this path in the command below. #/path/to/your/maven/bin/mvn clean install

# Example:

/usr/bin/mvn clean install

  • If you are building a Gradle Project:
Scroll down to the "Build" section.
Click "Add build step".
Select "Execute shell".

In the "Command" area, enter the full path to your Gradle executable followed by the tasks you want to run (e.g., build).

To find the full path to your gradle executable on open terminal in Ubuntu and run: which gradle

The output will give you the full path (e.g., /usr/local/bin/gradle or /opt/gradle/bin/gradle). Use this path in the command below.

#/path/to/your/gradle/bin/gradle build

# Example using the path provided:

/opt/gradle/gradle-8.13/bin/gradle build

6. Configure Post-Build Actions for Freestyle

To publish the JUnit test reports generated by your build tool so they are visible in the Jenkins UI, follow these steps:

  • Scroll down to the "Post-build Actions" section.
  • Click "Add post-build action".
  • Select "Publish JUnit test result report".
    • Test report XMLs: Enter the path to the test report XML files generated by your build tool.
      • For Maven Surefire/Failsafe: **/target/surefire-reports/*.xml
      • For Gradle: **/test-results/**/*.xml 
    • You can leave other options as default for a basic setup

7. Save the Freestyle Job Configuration

Scroll to the bottom of the page and click "Save".


8. Run the Freestyle Jenkins Job

  1. You will be redirected to the job's dashboard page.
  2. On the left-hand menu, click "Build Now".

Jenkins will now clone your repository, execute the build command, and then process the test report XML files.

Monitor the Freestyle Build and View Results

  1. Under "Build History" on the left, you will see a new build appear with a progress bar.
  2. Click on the build number once it starts.
  3. Click "Console Output" on the left to view the live build logs.
  4. After the build completes, you should see a "Test Result" link on the build's page if test reports were found and published. Click this link to see a summary of your test results.

10. Create a New Jenkins Job (Pipeline)

Pipeline is a more modern approach using Groovy scripts to define your build process.

  1. On the Jenkins dashboard, click "New Item".
  2. Enter an item name (e.g., Maven-GitHub-Pipeline or Gradle-GitHub-Pipeline).
  3. Select "Pipeline".
  4. Click "OK

    11. Configure the Pipeline Script

    In the job configuration page for your Pipeline project:

    1. Scroll down to the "Pipeline" section.
    2. Under "Definition", select "Pipeline script". This allows you to write or paste the Pipeline script directly into the Jenkins job configuration.
    3. A text area labeled "Script" will appear. This is where you will paste your Groovy Pipeline script.

    12. Example Pipeline Scripts to Paste Directly

Example Pipeline Script for a Maven Project:

pipeline {
    agent any // Or specify a specific agent label

    stages {
        stage('Checkout') {
            steps {
                // Checkout code from your GitHub repository
                git url: 'https://github.com/devops-ds/your-maven-project.git', branch: 'main' // Replace with your repo URL if different, adjust branch name if needed
                // If your repository is private, you'll need to add credentials here, e.g.:
                // git url: 'https://github.com/devops-ds/your-maven-project.git', branch: 'main', credentialsId: 'your-credential-id'
            }
        }

        stage('Build') {
            steps {
                // Execute Maven build using the 'mvn' command with the full path
                // To find the full path to your 'mvn' executable on Linux/macOS, open your terminal and run:
                // which mvn
                sh '/usr/bin/mvn clean package' // Replace with actual path
            }
        }

        stage('Test') {
            steps {
                // Optionally, separate test execution if needed
                // This step is often not strictly necessary if 'mvn clean package' already runs tests
                // sh '/path/to/your/maven/bin/mvn test' // Replace with actual path
                echo 'Tests are typically run during the Build stage with Maven.'
            }
        }
    }
    post {
        always {
            // Archive test reports
            junit '**/target/surefire-reports/*.xml'
        }
        success {
            echo 'Build and tests succeeded!'
        }
        failure {
            echo 'Build or tests failed.'
        }
        // Add other post conditions like 'unstable', 'changed' as needed
    }
}

Remember to replace the placeholder paths for mvn with the actual full path on your Jenkins agent if you are using the Maven example.
Example Pipeline Script for a Gradle Project:
pipeline {
    agent any // Or specify a specific agent label

    stages {
        stage('Checkout') {
            steps {
                // Checkout code from your GitHub repository
                git url: 'https://github.com/devops-ds/your-gradle-project.git', branch: 'main' // Corrected URL
                // If your repository is private, you'll need to add credentials here, e.g.:
                // git url: 'https://github.com/devops-ds/your-gradle-project.git', branch: 'main', credentialsId: 'your-credential-id'
            }
        }

        stage('Build') {
            steps {
                // Run Gradle build using the wrapper
                sh './gradlew clean build'
            }
        }

        stage('Test') {
            steps {
                // Run tests (if not already run in the build stage)
                // This step is often not strictly necessary if './gradlew clean build' already runs tests
                sh './gradlew test'
            }
        }
    }
    post {
        always {
            // Archive test reports (modify the path according to your project structure)
            junit '**/build/test-results/test/*.xml'
        }
        success {
            echo 'Build and tests succeeded!'
        }
        failure {
            echo 'Build or tests failed.'
        }
        // Add other post conditions like 'unstable', 'changed' as needed
    }
}

13. Save the Pipeline Job Configuration

Scroll to the bottom of the page and click "Save".


14. Run the Pipeline Jenkins Job

  1. You will be redirected to the job's dashboard page.
  2. On the left-hand menu, click "Build Now".

Jenkins will now execute the stages defined in the Pipeline script you pasted.

15. Monitor the Pipeline Build and View Results

  1. Under "Build History" on the left, you will see a new build appear.
  2. Click on the build number.
  3. You will see a visual representation of the pipeline stages. Click on a stage to see its details.
  4. Click "Console Output" on the left to view the logs for the entire pipeline run.
  5. Similar to the Freestyle job, if test reports are published, you will see a "Test Result" link on the build's page.

This covers setting up both Freestyle and basic Pipeline jobs in Jenkins to build Java projects from GitHub and publish test results.

Comments

Popular posts from this blog

1