Include all the test case classes in testNg.xml file then-->run as testNg Suite
While running through testNG.xml if you see below error
Solution: add the fremarker dependency in pom.xml
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
problem2)
if you see below error
Solution:change the extentreports version from 4.1.0 to 4.1.1
Problem related to log4j:
[Utils] [ERROR] [Error] java.lang.NoSuchMethodError: org.apache.logging.log4j.Logger.atDebug()Lorg/apache/logging/log4j/LogBuilder;
Solution is:
2)Run tests through pom.xml
Add build configurations inside the pom.xml then mention the testNG.xml file
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/TestNgSuite/testNG.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
right click on pom.xml-->run as -->maven clean
right click on pom.xml-->run as -->maven install or maven test
right click on pom.xml-->run as -->maven build-->goals-->clean install-->apply-->run
3)Run tests through command prompt
1)Download maven to your local machine from below url
click on Binary zip archive apache-maven-3.8.5-bin.zip
place the zip file in c drive- one folder named: apachemaven
Unzip the compresed maven file
go to maven folder
go to environment variables
click on New button
set the variable name as:MAVEN_HOME and value as:C:\ApacheMaven\apache-maven-3.8.5-bin\apache-maven-3.8.5 under system variables
select the path variable--click on edit
click on new button in open window
%MAVEN_HOME%\bin
click ok-->ok-->ok
Go to project location
open project location in command prompt -->then type below command
mvn clean install
4)Run tests through batch file
create a batch file(run.bat) under project locaiton and include below two lines
cd D:\eclipseworkspaceAug2018\HybridFrameworkWeekendApril52020
mvn clean install
save file as run.bat file-->then double click on the bat file--tests will be triggered.
Note:No need to go to eclipse to triggere the tests with this process.
5)Run tests through Jenkins CI tool
What is Continuous Integration?
Continuous Integration is a development practice in which the Automation Testers are required to commit changes to the source code in a shared repository several times a day or more frequently. Every commit made in the repository is then built. This allows the teams to detect the problems early. Continuous Integration is the most important part of DevOps that is used to integrate various DevOps stages. Jenkins is the most famous Continuous Integration tool.
What is Jenkins?
Jenkins is an open source automation tool written in Java with plugins built for Continuous Integration purpose. Jenkins is used to build and test your software projects continuously making it easier for developers to integrate changes to the project, and making it easier for users to obtain a fresh build. It also allows you to continuously deliver your software by integrating with a large number of testing and deployment technologies. Jenkins integrates development life-cycle processes of all kinds, including build, document, test, package, stage, deploy, static analysis and much more. Jenkins achieves Continuous Integration with the help of plugins. Plugins allows the integration of Various DevOps stages. If you want to integrate a particular tool, you need to install the plugins for that tool. For example: Git, Maven, HTML publisher etc.
Advantages:
- Advantages of Jenkins include:
- It is an open source tool with great community support.
- It is easy to install.
- It has 1000+ plugins to ease your work. If a plugin does not exist, you can code it and share with the community.
- It is built with Java and hence, it is portable to all the major platforms.
Apart from Jenkins, we have many more tools in the market such as:
- Anthill
- Bamboo
- Cruise Control
- Team City
Complete Jenkins Installation Process
URL: https://www.jenkins.io/download/
Place jenkins.war file in c drive
Open jenkins.war file location in command prompt
Go to jenkins downloaded folder location, Default port is 8080, no need to specify port if your jenkins running on 8080 port
provide below command in command prompt: to start the jenkins server
java -jar jenkins.war
Then Press ENTER key from keyboard
if you want to start the jenkins in particular port use below command
java -jar jenkins.war --httpPort=8082
copy the password from command prompt console or
goto the location for password:C:\Users\LENOVO\.jenkins\secrets\initialAdminPassword
STEP1:
open any browser & type below url in address bar
localhost:8080 or localhost:portnumber -->press enter key
Step2)
Enter the Administrator password that is copied from commandprompt or terminal
After entering the password click on continue button
select install suggested plugins option
it will install all the suggested plugins
Step5):
create username and password in below screen
STEP6)after setting username and password then login to jenkinswe will see welcome to jenkins page like below
complete the installation
Manage Jenkins(configuring the jenkins)-->Manage Plugins
install Html Publisher plugin(Note :https://plugins.jenkins.io/htmlpublisher/)
The HTML Publisher plugin is useful to publish HTML reports that your build generates to the job and build pages. It is designed to work with both Freestyle projects as well as being used in a Jenkins Pipeline.
maven integration plugin
Green Balls
create a job
trigger build
NOTE: if Jenkins installation is already done and the session is closed, just start the Jenkins server from the command prompt using the below command
java -jar jenkins.war
and wait for the message
jenkins is fully up and running in commond prompt console
then go to any browser & type below url in addressbar
localhost:8080
then page will open with jenkins login page then perform the login with username and password created during the installation
Configure Email Notification in Jenkins|How To Send Email from Jenkins Job:
Note:https://plugins.jenkins.io/email-ext/
1)click on manage Jenkins from the left panel
2)Click on Configure System3)Enter Details SMTP server and Port in the Extended Email Notification section like the below screen
Under the Credentials, section click on add-->select JenkinsNow Jenkins Credentials provider popup will be displayed
select the Kind option username with password
Enter username -rameshitusa29@gmail.com and password[gmal app password need to be provided here]Description:unique name for credentials
click on Add button
default content type: plain/text
default Recipients:rameshitusa29@gmail.com
Enter default subject:
Automation Report - $PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS
Enter default content:
Hi,
Please find the Automation job summary report
$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS
Check console output at $BUILD_URL to view the results.
Thanks,
Automation Team
Note: Please do not reply to this email
Enter the details in the Email Notification Section
provide SMTP server:smtp.gmail.com
default user email suffix:@gmail.com
click on advanced
check the use SMTP Authentication checkbox
Enter username rameshitusa29@gmail.com and password[gmal app password need to be provided here]
check the use SSL checkbox
enter Gmail SMTP port:465Click on Test Configuration by sending the test email checkbox
Enter the recipient's email address
Click on Test Connection
One Test Email should be sent
Click on Save and Apply buttons.
----------------------------------------------
Note:
navigate to https://myaccount.google.com/
click on Security on left panel
turn on 2 step verification
It might ask you the password again enter it then below page will be shown
Select app :mail
select device:windows computer
Copy the 16 character App password: flmzzrxjphqdjkfo
NOTE: Use the above password in Jenkins
The setting that you are looking for is not available for your account.” – this means 2-Step Verification is not enabled on your account. Enable it first.
======================================================================= Create a job and test the email
under build section select editable email notification option
Enter project recipients listrameshitusa29@gmail.com,rameshn3@gmail.com
click on advanced
trigger part:when do you want to trigger the build
click on save and apply
====================================
Integrate Selenium Maven Github project in Jenkins:
1)first push the code to the GitHub
2)go to Jenkins
3)click on New Item name:integrategithubwith jenkins to create a new job
4)select maven project
6)Enter the description: integrating selenium GitHub project in Jenkins
7)Source Code Management section-->select git radio button
8)Copy the Github code url like below
9)Enter Repository URL in jenkins job:https://github.com/rameshn3/HybridFrameworkOct262022.git
10)click on add under Credentials -->select Jenkins
11)Select Kind:username with PasswordEnter username:rameshn3@gmail.com
Password:********
Description:GithubCredentials
click on Add button
select the branch you want to run either */master or specific branch:automationtaskNov32022 Refer more:https://stackoverflow.com/questions/23906352/git-pullrequest-job-failed-couldnt-find-any-revision-to-build-verify-the-repo
12)give Root pom from github like below Note: if you parameterise the pom.xml like step14
Goals and options should be like below
13)Under post steps select run regardless of build result14)In Your Project pom.xml parameterise the build section plugin-surefire plugin like below, then commit to github
Click on Apply and Save buttonsThen trigger the build
It will download the project from GitHub and runs the testcases
====================================
Groovy's Domain-Specific Language (DSL) for Jenkins Pipelines
Introduction
In today's fast-paced development environment, automation is key to delivering software quickly and reliably. Jenkins, a leading open-source automation server, enables continuous integration and continuous delivery (CI/CD) for any project. At the heart of Jenkins is the Pipeline, which uses Groovy-based Domain-Specific Language (DSL) to automate build, test, and deployment processes. This article explores Groovy's DSL for Jenkins Pipelines, explaining its terminologies, structure, and usage through detailed examples.
Key Terminologies
Before diving into the DSL, it's crucial to understand the key terminologies:
1. Pipeline: A Pipeline defines a series of automated steps that take your code from version control to production.
2. Groovy: Groovy is an object-oriented programming language that enhances Java and is used to script Jenkins Pipelines.
3. DSL (Domain-Specific Language): A specialized language designed to solve problems in a specific domain—in this case, defining Jenkins Pipelines.
4. Stage: A stage is a block that groups steps in a Pipeline, typically representing a phase like "Build," "Test," or "Deploy."
5. Step: A step is a single task that Jenkins performs, such as running a shell command or executing a script.
6. Agent: An agent specifies where the Pipeline or a particular stage runs, whether on any available node, a specific node, or within a Docker container.
Step-by-Step Guide to Groovy DSL for Jenkins Pipelines
Step 1: Creating a Jenkins File
The Jenkinsfile is a text file containing the Pipeline definition, written using Groovy DSL. It should be placed in the root directory of your repository.
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}
Step 2: Defining Agents
Agents specify where the Pipeline or a specific stage runs. The agent any directive indicates that the Pipeline can run on any available agent.
pipeline {
agent {
label 'my-agent-label'
}
stages {
// Stages go here
}
} Step 3: Creating Stages and Steps
Stages are used to organize the Pipeline into discrete phases. Each stage can contain multiple steps that perform specific tasks.
stage('Build') {
steps {
sh 'make' // Execute shell command to build the project
}
}
stage('Test') {
steps {
sh 'make test' // Execute shell command to test the project
}
}
stage('Deploy') {
steps {
sh 'make deploy' // Execute shell command to deploy the project
}
} Step 4: Using Post-Actions
Post actions are blocks that define steps to run at the end of a Pipeline or stage, regardless of the outcome.
post {
always {
echo 'This will always run'
}
success {
echo 'This will run only if the Pipeline succeeds'
}
failure {
echo 'This will run only if the Pipeline fails'
}
}
Step 5: Parallel Execution
Jenkins Pipelines can run multiple stages in parallel to optimize the CI/CD process. This is done using the parallel directive.
stage('Parallel Execution') {
parallel {
stage('Unit Tests') {
steps {
sh 'make test-unit'
}
}
stage('Integration Tests') {
steps {
sh 'make test-integration'
}
}
}
} Practical Examples
Example 1: Simple Pipeline
This example demonstrates a basic Pipeline with build, test, and deploy stages.
pipeline {
agent any
stages {
stage('Compile') {
steps {
echo 'Compiling..'
sh 'javac MyClass.java'
}
}
stage('Unit Test') {
steps {
echo 'Running Unit Tests..'
sh 'java MyClassTest'
}
}
stage('Deploy') {
steps {
echo 'Deploying Application..'
sh 'scp MyClass.class user@host:/deploy'
}
}
}
} Example 2: Declarative Pipeline with Notifications
This example shows how to add email notifications for success and failure outcomes.
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
sh 'make build'
}
}
stage('Test') {
steps {
echo 'Testing...'
sh 'make test'
}
}
}
post {
success {
mail to: 'team@example.com',
subject: "Build Successful: ${env.JOB_NAME} ${env.BUILD_NUMBER}",
body: "Good job! The build was successful."
}
failure {
mail to: 'team@example.com',
subject: "Build Failed: ${env.JOB_NAME} ${env.BUILD_NUMBER}",
body: "Unfortunately, the build failed. Please check the Jenkins logs for more details."
}
}
}FAQs
Q1: What is the primary benefit of using Groovy DSL for Jenkins pipelines?
A1: The primary benefit is the ability to define complex, multi-step workflows in a clear, readable, and maintainable manner. Groovy DSL's flexibility allows for the customization of build, test, and deployment processes to fit specific project requirements.
Q2: Can I use Jenkins Pipelines without knowing Groovy?
A2: While a basic understanding of Groovy helps in writing and maintaining Jenkins Pipelines, the DSL is designed to be user-friendly. Jenkins' documentation and community support can assist in learning the necessary Groovy basics.
Q3: How do I debug issues in my Jenkins Pipeline?
A3: You can use the echo command to print debug information to the Jenkins console. Additionally, Jenkins provides detailed logs for each step of the Pipeline, which can help identify and resolve issues.
Q4: What is the difference between Declarative and Scripted Pipelines in Jenkins?
A4: Declarative Pipelines provide a more structured and user-friendly syntax, ideal for simpler, standardized tasks. Scripted Pipelines offer greater flexibility and are suited for more complex, customized workflows. Both use Groovy DSL but differ in their structure and capabilities.
Q5: How do I handle parallel execution in Jenkins Pipelines?
A5: You can define parallel stages in a Jenkins Pipeline using the parallel directive. This allows multiple stages to run simultaneously, speeding up the overall pipeline execution.
stage('Parallel Execution') {
parallel {
stage('Unit Tests') {
steps {
sh 'make test-unit'
}
}
stage('Integration Tests') {
steps {
sh 'make test-integration'
}
}
}
} Conclusion
Groovy's DSL for Jenkins Pipelines offers a powerful and flexible way to define CI/CD workflows. By understanding the core concepts and leveraging the provided examples, you can create robust Pipelines tailored to your project's needs. Whether you're a novice or an experienced DevOps engineer, mastering Jenkins Pipelines will significantly enhance your automation capabilities.