DEVOPS (BCSL657D)
Program 6. Continuous Integration with Jenkins:
Setting Up a CI Pipeline, Integrating Jenkins with
Maven/Gradle, Running Automated Builds and
Tests.
Step 1: Install Jenkins:
1. Download Jenkins: Visit the [Jenkins download page]([Link] and choose
the version suitable for your operating system. - Follow the installation instructions for your platform.
2. Start Jenkins: After installation, Jenkins typically runs on [Link] by default. - Open a web
browser and access the Jenkins dashboard.
Step 2: Set Up Jenkins (Initial Configuration)
1. Unlock Jenkins: After installation, Jenkins will provide an unlock key. Open the `jenkins` folder and
retrieve the key from secrets/initial Admin Password`.
2. Install Suggested Plugins: Once logged in, Jenkins will ask if you want to install suggested plugins. Click
on Install suggested plugins.
3. Create an Admin User: You’ll be prompted to create an admin user. Provide an username, password,
and full name.
4. Jenkins Dashboard: After setup, you will be redirected to the Jenkins dashboard where you can create
and configure jobs.
Step 3: Install Required Plugins
To integrate Jenkins with Maven or Gradle, you need to install the respective plugins:
1. Maven Plugin: Go to Manage Jenkins > Manage Plugins. - In the Available tab, search for Maven
Integration Plugin and install it.
2. Gradle Plugin: In the same Manage Plugins section, search for Gradle Plugin and install it.
3. JUnit Plugin: If you’re using JUnit for automated testing, make sure the JUnit Plugin is installed for
reporting purposes.
Step 4: Configure Jenkins for Maven/Gradle Maven Integration:
1. Install Maven:
- If Maven is not installed on your Jenkins server, you can install it globally or point Jenkins to an existing
Maven installation.
- Go to Manage Jenkins > Global Tool Configuration.
- Under Maven, click Add Maven and specify the Maven version or path to your Maven installation.
2. Configure Maven in Jenkins:
- In your Jenkins project configuration, specify Maven as the build tool by selecting Invoke Maven under
Build section.
- Provide the goals, such as `clean install` or `clean deploy`.
Gradle Integration:
1. Install Gradle:
- Similarly, install Gradle by going to Manage Jenkins > Global Tool Configuration.
- Under Gradle, click Add Gradle and specify the Gradle version or path to the installation.
2. Configure Gradle in Jenkins:
- In your Jenkins project configuration, choose Invoke Gradle script under the Build section.
- Specify Gradle tasks like `clean build`.
Step 5: Create a Jenkins Pipeline/Job
1. Create a New Job:
- From the Jenkins dashboard, click on New Item.
- Choose Freestyle project or Pipeline depending on your needs.
2. Freestyle Project Configuration (Basic):
- Source Code Management: Set up Git or another source control system to fetch the code (e.g., GitHub,
Bitbucket).
- Build Environment: Configure the environment, e.g., for Maven, you’ll configure Maven goals (e.g., `clean
install`).
- Build: In the Build section, you can specify the build commands, such as:
- For Maven: `mvn clean install`
- For Gradle: `gradle build`
- Post-build Actions: You can set up post-build actions to publish test results or deploy to servers.
3. Pipeline Project Configuration (Advanced):
- In the case of a Pipeline, you define your CI/CD pipeline in a ‘Jenkinsfile’ (which is a script).
- Example Jenkinsfile for Maven:
“groovy”
pipeline {
agent any
stages {
stage('Checkout') {
steps { git '[Link]
}
}
stage('Build') {
steps {
script {
sh 'mvn clean install'
}
}
}
stage('Test') {
steps {
script { sh 'mvn test'
}
}
}
}
}
```
- For Gradle, you would use:
```groovy
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git '[Link]
}
}
stage('Build') {
steps {
script {
sh './gradlew clean build'
}
}
}
stage('Test') {
steps {
script {
sh './gradlew test'
}
}
}
}
}
```
4. Run Automated Tests:
- You can configure Jenkins to run tests automatically as part of the build.
- For JUnit, specify the test report location to view the results under Post-build Actions.
- Example configuration for Maven: “groovy” junit '/target/[Link]'
Step 6: Triggering Builds
1. Automatic Trigger:
- You can configure Jenkins to trigger builds automatically on changes to your source code
repository. Under Build Triggers, enable GitHub hook trigger for GITScm polling or Poll SCM depending on
your setup.
2. Manual Trigger:
- You can trigger builds manually from the Jenkins dashboard.
Step 7: Monitor and Analyze Builds
- Build History: Jenkins keeps a history of your builds, and you can access logs and other build artifacts.
- Test Reports: If you’re using JUnit or another testing framework, you can access test reports to view failed
tests and debug information.
Step 8: (Optional) Deploy to a Server
- After a successful build and test, you can configure Jenkins to deploy the build to a test/staging server or
even production.
- This can be done via Post-build Actions or in the pipeline script with a `deploy` stage.
RESULT: With these steps, you can successfully set up a CI pipeline in Jenkins using Maven or Gradle. By
integrating source control, build tools, and automated testing, you create a fully automated pipeline that
ensures consistent and rapid development workflows.