The previous tutorial has explained how to install it onto a Windows 10 system and create a Maven orGradle project in Jenkins. Jenkins and GitLab are two powerful tools on their own, but what about using them together? In this tutorial, learn about the benefits of a Jenkins GitLab integration and how to set up the integration on your own.
What is GitLab?
GitLab is a web-based Git repository that provides free open and private repositories, issue-following capabilities, and wikis. It is a complete DevOps platform that enables professionals to perform all the tasks in a project—from project planning and source code management to monitoring and security. Additionally, it allows teams to collaborate and build better software.
What is Jenkins?
Jenkins is a well-known open-source tool that aids in the implementation of Continuous Integration (CI) and Continuous Deployment/Continuous Delivery (CD) processes by automating parts of the software development pipeline such as building, testing, and deployment.
Jenkins Version Used – 2.361.2
In this version of Jenkins, Git Plugin is already installed. If the Git Plugin is not installed, then follow the below steps to add Git Plugin in Jenkins.
Step 1: Open your dashboard.
Click on the Manage Jenkins button on your Jenkins dashboard:
Step 2: Select Manage Plugins.
Click on the Manage Jenkins. Choose Manage Plugins.
Step 3: Add Git Plugin
On the Plugins Page, go to the Available option.
Select the GIT Plugin
Click on Install without restart.The plugin will take a few moments to finish downloading depending on your internet connection, and will be installed automatically.
You can also select the option Download now and Install after the restart button. In which plugin is installed after the restart
You will be shown a “No updates available” message if you already have the Git plugin installed.
In my case, Git Plugin is already installed, so you can’t see it in the Available Plugin.
Step 4: Verify Git Plugin is installed
Once the plugins have been installed, Go to Manage Jenkinson your Jenkins dashboard. You will see your plugins listed among the rest.
How to Integrate Jenkins With GitLab
Step 1: Create a new project using the Maven project plugin.
Give the Name of the project.
Click on the Maven project.
Click on the OK button.
In the General section, enter the project description in the Description box.
Click on create new jobs.
Enter the item name, select the job type, and click OK. We shall create a Maven project as an example.
Step 2: Describe the project in the description section
In the General section, enter the project description in the Description box.
Step 3 – Source Code Management section
You will see a Git option under Source Code Managementif your Git plugin has been installed in Jenkins.
Enter the Git repository URL to pull the code from GitHub. Enter the credentials to log in to GitLab.
I have already set up the credentials to log in to GitLab. Click on the Add button and select Jenkins.
Add the username and password used to log in to GitLab. Click on the “Add” button.
Note:- Please make sure that Git is installed on your local machine. To install Git on your local machine, go to this tutorial –How to install Git on Windows 10.
Step 4: Build Management
Go to the Build section of the new job.
In the Root POM textbox, enter pom.xml
In the Goals and options section, enter “clean test“
Step 5: Select “Publish TestNG Results” from “Post Build Actions“
Scroll down to “Post Build Actions” and click on the “Add Post Build Actions” drop-down list. Select “Publish TestNG Results“.
Enter TestNG XML Report Pattern as “**target/surefire-reports/testng-results.xml” and click on the “Save” button.
We have created a new project “Git_Demo” with the configuration to run TestNG Tests and also to generate TestNG Reports after execution using Jenkins.
Step 6: Execute the tests
Let’s execute it now by clicking on the “Build Now” button.
Right-click on Build Number (here in my case it is #2) and click on Console Output to see the result.
Step 7: View the TestNG Report
Once the execution is completed, we could see a link to view “TestNG Results“.
Click on the TestNG Results. It displays the summary of the tests.
If you want to check the execution history, then click on the shown link.
This way, we could integrate Git in Jenkins.
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!
Jenkins is a self-contained, open-source automation server that can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software.
Jenkins can be installed through native system packages, Docker, or even run standalone by any machine with a Java Runtime Environment (JRE) installed.
This tutorial explains the process to run the Rest API Tests in GitLab pipeline. This is a significant step towards achieving CI/CD. Ideally, the tests need to run after any change (minor/major) before merging the change to master branch. Suppose there are 100 changes in a day, and any QA won’t want to start the tests manually 100 times in a day. So, now adding tests to GitLab pipeline comes to the picture. We can add a test stage to the pipeline and the tests will run automatically when the pipeline run, or we can schedule the tests to run automatically every hour or day using GitLab pipeline.
Pre-Requisite:
Rest Assured – 4.3.3
Java 11
Maven / Gradle
TestNG /JUnit
GitLab account
To use GitLab CI/CD, we need to keep 2 things in mind:-
a) Make sure a runner is available in GitLab to run the jobs. If there is no runner, install GitLab Runner and register a runner for your instance, project, or group.
b) Create a .gitlab-ci.yml file at the root of the repository. This file is where you define your CI/CD jobs.
Step 1 – Create a new Maven Project
Step 2 – Add the below-mentioned pom.xml that shows all the dependencies need to add to the project
It is needed to add maven-surefire plugin to run the TestNG tests through command line. To know more about this, please refer to this tutorial.
Step 3 – Create the Test Code to test the Rest API
Here, 2 tests are created. One of the tests get all the employee data (GET) whereas another test creates an employee (POST).
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import org.json.JSONObject;
import org.testng.annotations.Test;
import io.restassured.http.ContentType;
public class RestAPIDemo {
@Test(description = "To get the details of employee with id 2", priority = 0)
public void verifyUser() {
// Given
given()
// When
.when().get("http://dummy.restapiexample.com/api/v1/employee/2")
// Then
.then().statusCode(200).statusLine("HTTP/1.1 200 OK")
// To verify booking id at index 3
.body("data.employee_name", equalTo("Garrett Winters"))
.body("message", equalTo("Successfully! Record has been fetched."));
}
@Test(description = "To create a new employee", priority = 1)
public void createUser() {
JSONObject data = new JSONObject();
// Map<String, String> map = new HashMap<String, String>();
data.put("employee_name", "APITest");
data.put("employee_salary", "99999");
data.put("employee_age", "30");
// GIVEN
given().baseUri("http://dummy.restapiexample.com/api").contentType(ContentType.JSON).body(data.toString())
// WHEN
.when().post("/v1/create")
// THEN
.then().statusCode(200).body("data.employee_name", equalTo("APITest"))
.body("message", equalTo("Successfully! Record has been added."));
}
}
Step 4 – Create testng.xml to run the tests through TestNG
Now, let’s create a testng.xml to run the TestNG tests. If JUnit is used instead of TestNG, then this step is not needed.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="com.example.RestAssured_TestNG_Demo.RestAPIDemo"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Step 5 – Run the tests through the command line
Now, let us execute the tests through the command line. Go to the place where pom.xml of the project is placed and use the below command to run the tests. This step makes sure that all the tests are running as expected.
Step 8 – Create .gitlab-ci.yml file in the project in GitLab
It is a YAML file where you configure specific instructions for GitLab CI/CD. In the .gitlab-ci.yml, we can define:
The scripts you want to run.
Other configuration files and templates you want to include.
Dependencies and caches.
The commands you want to run in sequence and those you want to run in parallel.
The location to deploy your application.
Whether you want to run the scripts automatically or trigger any of them manually.
image: adoptopenjdk/maven-openjdk11
stages:
- test
variables:
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
test:
stage: test
allow_failure: true
# Run the tests
script:
- mvn $MAVEN_OPTS clean package
- mvn compile test
# Store artifacts
artifacts:
when: always
name: "report"
paths:
- target/surefire-reports/*
expire_in: 1 h
Step 9 – Run the tests in the GitLab pipeline
Now, when a new change is committed, a pipeline kicks off and it runs all the tests.
Step 10 – Check the status of the pipeline
Once the Status of the pipeline changes to either failed or passed.. that means the tests are already executed. Here, the pipeline is passed with brown color means that the execution of the test is completed with some failures.
I have added an artifact in the gitalb-ci.yml with the name “report”. This artifact creates a folder with the name “report” and the reports in this folder come from the path /target/surefire-reports. This artifact gives us an option to download the reports or browse the report. This report will be available for 1 hour only as mentioned in the gitlab-ci.yml.
Step 11 – Download the report
Click on the Download button and the report zip file is downloaded. Unzip the folder, and it contains all different types of surefire-reports.
Example of Emailable-Report.html
Example of Index.html
Congratulations. This tutorial has explained the steps to run Selenium tests in GitLab CI/CD. Happy Learning!!
This tutorial explains the process to run the Selenium Tests in the GitLab pipeline. This is a very important step towards achieving CI/CD. Ideally, the tests need to run after any change (minor/major) before merging the latest change to the master branch. Suppose there are 100 changes merged to the master branch in a day and it is expected to run the tests every time before deployment. In this case, any QA won’t want to start the tests manually 100 times in a day. Now, what should be done to overcome this problem. Now, adding tests to the GitLab pipeline comes into the picture. We can add a test stage to the pipeline and the tests will run automatically when the pipeline run.
Pre-Requisite:
Selenium
TestNG/JUnit (for Assertions)
Java 11
Maven/ Gradle
GitLab Account
What is GitLab CI/CD Workflow?
Once the proposed changes are built, then push the commits to a feature branch in a remote repository that’s hosted in GitLab. The push triggers the CI/CD pipeline for your project. Then, GitLab CI/CD runs automated scripts (sequentially or in parallel) to build as well as to test the application. After a successful run of the test scripts, GitLab CI/CD deploys your changes automatically to any environment (DEV/QA/UAT/PROD). But if the test stage is failed in the pipeline, then the deployment is stopped.
After the implementation works as expected:
Get the code reviewed and approved.
Merge the feature branch into the default branch.
GitLab CI/CD deploys your changes automatically to a production environment.
To use GitLab CI/CD, we need to keep 2 things in mind:
a) Make sure a runner is available in GitLab to run the jobs. If there is no runner, install GitLab Runner and register a runner for your instance, project, or group.
b) Create a .gitlab-ci.yml file at the root of the repository. This file is where CI/CD jobs are defined.
The Selenium tests run on a headless browser in the pipeline.
What is a headless browser?
A headless browser is like any other browser, but without a Head/GUI (Graphical User Interface). A headless browser is used to automate the browser without launching the browser. While the tests are running, we could not see the browser, but we can see the test results coming on the console.
To explain, I have created 2 Selenium tests and used TestNG for asserting the tests. The tests will run on a headless Chrome browser. One more thing to keep in mind is that when tests run on a headless Chrome browser, the window screen won’t be full screen. So, the screen needs to be maximized explicitly otherwise some of the tests would fail.
Step 1 – Create a new Maven Project
Step 2- Add the dependencies to the POM.xml
Add the below-mentioned dependencies that need to add to the project to the pom.xml in Maven Project.
There are 2 different pages that need to be tested – LoginPage and ForgetPasswordPage
LoginPage contains the tests to log in to the application. After successful login, the application moves to the next webpage – HomePage. You can see that BaseTest class is extended in both the Test classes.
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;
@Test
public class ForgetPasswordPage extends BaseTest{
@Test
public void getHeading() {
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='forgotPasswordLink']/a")).click();
String heading = driver.findElement(By.xpath("//*[@id='content']/div[1]/div[2]/h1")).getText();
System.out.println("Title : "+ heading );
Assert.assertEquals(heading, "Forgot Your Password?");
}
}
Step 4 – Create testng.xml to run the tests
Now, let’s create a testng.xml to run the TestNG tests. If JUnit is used instead of TestNG, then this step is not needed.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="org.example.DockerDemo.LoginPage"/>
<class name="org.example.DockerDemo.ForgetPasswordPage"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Step 5 – Run the tests through command line
Now, let us execute the tests through the command line. Go to the place where the pom.xml of the project is placed and use the below command to run the tests. This step makes sure that all the tests are running as expected.
mvn compile test
GitLab Section
Step 6 – Create a blank project in GitLab
To know, how to create a blank new project in GitLab, please refer tothis tutorial.
Step 7 – Push the project from local repository to Gitlab Repository
To know, how to push the changes in GitLab, please refer to this tutorial.
Step 8 – Create a .gitlab-ci.yml file in the project in GitLab
There are many ways to create a new file in GitLab. One of the ways is to create a file as shown in the below image.
It is a YAML file where you configure specific instructions for GitLab CI/CD. In the .gitlab-ci.yml, we can define:
The scripts you want to run.
Other configuration files and templates you want to include.
Dependencies and caches.
The commands you want to run in sequence and those you want to run in parallel.
The location to deploy your application to.
Whether you want to run the scripts automatically or trigger any of them manually.
image: markhobson/maven-chrome
stages:
- test
variables:
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
test:
stage: test
allow_failure: true
# Run the tests
script:
- mvn $MAVEN_OPTS clean package
- mvn compile test
# Store artifacts
artifacts:
when: always
name: "report"
paths:
- target/surefire-reports/*
expire_in: 1 h
Image – markhobson/maven-chrome is used in this test. It is a docker image for Java automated UI tests.
This gitlab-ci.yml has only 1 stage – a test that contains the command to run the tests as well as also create an artifact that contains all the surefire reports which can be saved as Test Evidence.
Step 9 – Run the tests in the GitLab pipeline
Now, when a new change is committed, a pipeline kicks off and it runs all the tests.
Step 10 – Check the status of the pipeline
Once the Status of the pipeline changes to either failed or passed.. that means the tests are already executed.
As you can see the Status is failed here which means that the execution is completed. Let us see the logs of the execution it shows that out of 2 tests, 1 test passed and 1 test failed. This shows that tests are running successfully in the GitLab pipeline.
As I have added an artifact also in the gitalb-ci.yml, which is highlighted in the image. This artifact creates a folder with the name “report” and the reports in this folder come from the path /target/surefire-reports. This artifact gives us the option to download the reports or browse the report. This report will be available for 1 hour only as mentioned in the gitlab-ci.yml.
Step 11 – Download the report
Once, will click on the download button, it will download “report.zip”. Unzip the folder and it looks like something as shown below:
Example of Emailable-Report.html
Example of Index.html
Congratulations. This tutorial has explained the steps to run Selenium tests in GitLab CI/CD. Happy Learning!!
The git pull command fetches and downloads content from the remote repository and integrates changes into the local repository. The git pull command is called as the combination of git fetch followed by git merge.
This is my project in GitLab. If, you don’t have any project in GitLab, follow the below steps to create a GIT local Repository and push it to GitLab.
Step 4 – Let’s add a new file in the Remote Repository directly using GitLab. Follow the steps shown below to add and commit a new .
Step 5 – This is a new file. I have provided name to this file as products.txt. Add the comment as commit message. Click the Commit changes button to commit this change.
Below image shows that new file – products.txt is committed to this project.
Step 6 – Go to the directory where local GIT Repository is saved. Right-click and click on Git Bash Here to open GitBash at that place.
Use the below command to see the list of files present in GIT Local Repository.
ls -a
git log
Type git log to see the list of commits present.
Step 7 – Type git pull command is used to pull the latest changes from Remote GIT Repository to local GIT Repository.
git pull origin master
This command says that pull the content from the “master” branch in the “origin” repo.
As the “git pull” command fetches and merges, we can see the newly added file named “products.txt” in my local repo.
Again use the below commands:
ls -a
git log
This shows that the new file – products.txt which was present in GitLab is now present in Git local Repository means latest change is pulled from GitLab to local Repository.
Congratulation!! We are able to pull the changes from Remote Repository to Local Repository. Happy Learning!!
This tutorial explains the steps that need to be followed to push a new local GIT Repository to GitLab.
What is GitLab?
GitLab is a single application that spans the entire software development lifecycle. GitLab is an open-source project maintained by GitLab Inc with over 3,000 contributors. We can install and self-manage the GitLab Community Edition which is fully open-source under an MIT license. GitLab also provides an Enterprise Edition that has additional features built upon the open-source edition.
GitLab’s application offers functionality to collaboratively plan, build, secure, and deploy software as a complete DevOps Platform.
Steps to follow:
Step 1 – Login to GitLab using your username and password.
Step 2 – In the dashboard, click the blue New project button. This opens the New project page.
Step 3 – SelectCreate a blank project.
Step 4 – A new page will open. Provide the following information on that page:
1.Project Name – Mention the name of your project in the Project name field – GitTest.
2.Project slug – When a name is added, the Project slug auto-populates. This is the URL path for your project that the GitLab instance uses.
3. Project description (optional) – This field enables you to enter a description for your project’s dashboard, which helps others understand what your project is about.
4. Visibility Level – Select the appropriate Visibility Level for your project. I have selected private option.
5. I’m not checking ReadMe option as I already have a ReadMe file in my project.
Select the Create Projectbutton.
We can see that a new empty project is created in the GitLab as shown below.
Step 5 – For every remote repo, you will get a unique URL as highlighted below. This URL is used to push the local changes to this remote repo. Type the below command in GitBash:
Now run the Git push command as shown above. It will ask for your GitLab credentials in a new window as shown below. Please provide the username and password used to log in to GitLab.
Now we can see we have successfully pushed the local changes to the remote repository. Let’s go to GitLab and verify the latest changes. All the files fromthe local GIT Repositoryare moved to GitLab Remote Repository.
I hope this tutorial has helped you to set up a new project in GitLab and push the local changes to GIT Remote Repository.
This tutorial explains the steps to install Git on Windows 10.
Step 1 – To install Git, go to – Git (git-scm.com). I want to install Git on Windows, so can use highlighted option to install Git.
The latest version here is 2.34.1.
Step 2 – Save the Git.exe file.
Step 3 – Now open the installer you have downloaded and go through the installation process. Unless you know what you are doing, leave all settings to their defaults. Click the Next button.
Step 4 – Select the destination location where GIT will be installed. You can continue with the default location as shown below, or use the installation path suggested by Git. Click the Next button.
Step 5 – In this step, you need to select components to be installed. Apart from the pre-selected options, you may also want to select “Add a Git Bash Profile to Windows Terminal”. This can be useful later on, especially if you plan to use Visual Studio Code or other IDEs that have a built-in terminal window. Click the Next button.
Step 6 – Select if you would like to GIT shortcut in the start menu. Click the Next button.
Step 7 – Select the default editor of GIT. I use Notepad++ as the default editor used by GIT.
Click the Next button.
Step 8 – Git hosting tools like GitHub or GitLab already use main as their default branch.
Click the Next button.
Step 9 – Click the Next button.
Step 10 – Click the Next button.
Step 11 – Select SSL/TLS library used by GIT to use for the HTTPS connection. Go with default selection and click the Next button.
Step 12 – Click the Next button.
Step 13 – Click the Next button.
Step 14 – Configure the default behaviour of the git pull command. Click the Next button.
Step 15 – Click the Next button.
Step 16 – Enable file system caching to boost performance, which is the default selection. Click the Next button.
Step 17 – Click the Install button.
Step 18 – You will get the below screen after the successful installation of GIT. Click the Finish button. This wizard has also installed a new tool called Git Bash, a terminal alternative for cmd or Powershell.
From Git Bash or terminal of your choice, run the following command:-
git --version
You can also use Windows PowerShell as well.
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
GitLab is the open DevOps platform, delivered as a single application. This makes GitLab unique and creates a streamlined software workflow, unlocking your organization from the constraints of a pieced-together toolchain. Learn how GitLab offers unmatched visibility and higher levels of efficiency in a single application across the DevOps lifecycle.
In this tutorial, I will explain how we can create anew empty project in GitLab.
Implementation Steps
Step 1 – Login to GitLab using your username and password.
Step 2 – In your dashboard, click the blue New projectbutton. This opens the New project page.
Step 3 – The New project page provides 4 options to select:
Create a blank project.
Create a project using one of the available project templates.
Import a project from a different repository, if enabled on your GitLab instance.
Run CI/CD pipelines for external repositories.
As I have mentioned earlier, I want to create a new empty project, so I will select open 1 (Create a blank project).
Step 4 – A new page will open. Provide the following information on that page:
Project Name – Mention the name of your project in the Project name field – CucumberGradleDemo. You can’t use special characters, but you can use spaces, hyphens, underscores, or even emojis.
Project slug – When a name is added, the Project slug auto-populates. The path to your project is in the Project slug field. This is the URL path for your project that the GitLab instance uses. If the Project name is blank, it auto populates when you fill in the Project slug. If you want a different slug, input the project name first, then change the slug after.
Project description (optional) – This field enables you to enter a description for your project’s dashboard, which helps others understand what your project is about. Though it’s not required, it’s a good idea to fill this in.
Visibility Level – Select the appropriate Visibility Level for your project.
Selecting the Initialize repository with a README option creates a README file so that the Git repository is initialized, has a default branch, and can be cloned.
Select the Create Project button.
Congratulations!!. We have just created a new and empty project in GitLab. Now you can clone this project and start working on it.
GitLab is the open DevOps platform, delivered as a single application that spans the entire software development lifecycle. If you’re not using GitLab, your DevOps lifecycle is likely spread across any number of applications. These silos take overhead to integrate, manage, configure, and maintain, slowing down your team and your deployments. Moving to a single application will speed up your workflow and help you deliver better software, faster. To know more about GitLab, click here.
In this article, we will see how to push an existing project to GitLab using Eclipse IDE.
Implementation Steps
Step 1 – Go to Git at the top, select VCS ->VCS Oprations -> Create Git Repository.
This will convert the project to a Git project.
Step 2 – Go to Git option present at the top and then select “Commit” option.
Step 3 – This will show all the files which are uncommitted. Select the files you want to commit and mention a message in the below message box as “New project from IntelliJ to GitLab“. Click on the Commit button.
Step 4 – A window opens where we need to mention the location where the project should be pushed in GitLab.
Click the OK button. It will ask for credentials to the GitLab, provide them.
Step 5 – The below image shows that the latest code is moved to GitLab. Here, the origin branch is used to commit and pushed the changes. If we are using a local branch to commit and push the changes, then we need to create a merge request to merge the code of the new branch to the code of existing origin(master branch).
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!