How To Create A Pipeline Using Jenkins and GitLab



[UPDATE: There is an error on loading of images in every post. I am working to resolve this as soon as possible. Screenshots were mere snap of windows so you can still follow the steps written in text and do the work!] 


Hello, in this post I will show how to create a CI pipeline using Jenkins, SonarQube and GitLab. Before proceeding, make sure you have basic knowledge of commands in Git and Git Bash.

If you have any doubt or query please comment it below so that others having same question can benefit from it. 

Some common URL for following applications:

- Jenkins: localhost:8080

- SonarQube (version sonarqube-9.2.4.50792): localhost:9000 


How to open SonarQube on your local machine:

1. Go into the folder where your SonarQube installation is located. From here go to: \bin\windows-x86-64.


2. In address bar above in the file explorer, clear out the rest of the text and type cmd so that it opens a Command Prompt terminal in that directory.

3. In the Command Prompt, type startsonar and press Enter.


Wait until ElasticSearch is up and running.

4. When you see this message, it means you have SonarQube and Java successfully installed. You can now access SonarQube at localhost: 9000. You will be asked to set for new password if you have just installed it or else you will be shown a login page where you need to enter the username and password.


5. After a successful login, you will be shown with Projects page ( Projects ). On this page, look at top-right corner and click on Create Project. You will have two options, out of which click on Manually.
6. Under Create a project, type any name. As you type for the Project display name, Project key will be auto-generated from the above string. Click on 'Set Up'.

7. On being asked, How do you want to analyze your repository? click on  'With Jenkins'.

8. Select GitLab under Select your DevOps platform. Next you will be shown some Prerequisites that you'll need to do before moving forward.


9. Let's leave SonarQube for a while and open a new tab and go to localhost:8080 where our Jenkins is running. Log in to Jenkins using your username and password. Default username is root and if you can't find your password go to where your Jenkins is installed. Follow this path on your system: C:\Program Files (x86)\Jenkins\secretsOn . Open initialAdminPassword as text file in notepad and copy-paste the string to the password field of Jenkins' login page.


10. On the homepage of Jenkins, on the left hand side, find and click Manage Jenkins.

11. Under System Configuration, click on Manage Plugins.


12. Under Plugins manager, click on Available tab.


12. In Filter text box search for the following plugins needed to be installed:
    - Git plugin
    - Pipeline
    - GitLab plugin for Jenkins
    - SonarQube scanner for Jenkins

13. Now, let's switch back to the tab where SonarQube on localhost:9000 is open. After selecting GitLab, click on Configure Analysis.

14. Follow the guide under Create a Pipeline Job and click Continue. When you are on 3rd step of this card, follow the given steps to make a new branch on your GitLab as many of our files will be on master branch so we are going to create a new branch for it and load all our project files onto it.

On next step, configuring web hooks for GitLab is optional

->
1. Before doing any of the steps below, make sure to download your git repository as zip or clone it to your local machine for backup if you do any mistake because you don't want to lose your previous work files. This backup will make us confident that if anything wrong happens we can upload the entire zip file again to master branch. Make two copies of this folder. One for where we will create a new branch and experiment with it and another folder for backup.

2. To clone your git, open master branch. On the top-right, click on Clone and copy the link under "Clone with HTTPS".


3. Open Git Bash on your local machine and type: git clone <paste-copied-git-link here>



4. Copy this folder into any other folder for Backup.


5. Open Git Bash again in that repository folder which not meant for backup.
6. In the terminal, type: git checkout -b <some-name> or git checkout -b ResultSystem.
and press Enter. You should see message like: Switched to a new branch 'ResultSystem'

7. You are now under 'ResultSystem' branch. Delete all files in that folder on your local machine except .git which is usually hidden but if it's not then don't delete it.

8. Paste your project files now after it is empty. Make sure the pom.xml file is in this folder and not in any other. What I mean is don't copy the project folder into this folder just copy the contents and paste it here.

 Come back to same Git Bash terminal and type in following commands:
 - git add .
 - git commit -m "uploading project files in new branch"
 - git push origin ResultSystem

Make sure you are doing all this under new branch and NOT master branch.

Now back to step #14 above.

15. Under Pipeline tab, 
- select Pipeline script from SCM under Definition
- select Git under SCM.
- paste your repository link under Repository URL under Repositories. The git link always ends with ".git"
- give your login credentials for GitLab under Credentials
- click Add Branch under Branches to build
- type */ResultSystem under Branch Specifier
- type Jenkinsfile under Script Path
- click Apply and Save.

The Jenkinsfile will contain the steps and stages for the pipeline and SonarQube.

16. Open your git in your browser. Click the dropdown to select ResultSystem. Click the " + " icon and select "New file".





17. Give Jenkinsfile as file name. This file doesn't have any extension so leave the name part just like that.

Type the following in Jenkinsfile file:


On line 29, make sure the project key is same with the one created during SonarQube configuration on localhost:9000.
Scroll down and click Commit changes.

18. In your Jenkins, click Build Now on left side pane of your pipeline.

19. After a successful build, you will see FINISED: SUCCESS in the Output Console and this:

20. If you are still getting errors, go to Configure System [Jenkins] (change the port number for Jenkins with yours). 
- Search for SonarQube servers. Check Environment variables true.  
- Give the URL of your SonarQube server which is currently running on your local machine. Default URL is http://localhost:9000
- Give SonarQube token under Server authentication token. The SonarQube tokens can be generated here: Security - My Account  (change the port number for SonarQube with yours)
- Give Name as 'Default'


If you have any doubt or query please comment it below. 

Comments

Popular

How To Create MySQL Table with SequelizeJS

How To Read CSV Files in Java as Maven Project

How To Create A Hibernate Project in Maven