Sunday, February 18, 2018

CI/CD with Baby Steps (Jenkins - Build with Git Hooks)

Git is the most widely used modern version control system in the world today, which allows multiple persons to safely work on the same project without hampering other team members. As a part of a team using Git, You and your team members will clone working copy of a local repository from Git server. You/team will add and commit the test scripts that are developed locally and push your changes to the Git.
We can make Jenkins to pull the project’s source code from the remote Git Server, by selecting the option in Source code management and specify the path/url can find the source code of the project.

Git hooks automate things like…
  • verifying that you included the associated JIRA issue key in your commit message
  • enforcing preconditions for merging
  • sending notifications to your team’s chat room
  • setting up your workspace after switching over to a different branch
Server-side pre-receive hooks are an especially powerful compliment to continuous integration because they can prevent developers from pushing code to master, unless the code meets certain conditions – like elite ninja guardians, protecting it from bad code.
Developers are generally conscientious enough not to merge to master when there are broken tests on their branch. But sometimes we forget to check. Or when we’re sharing a branch with other people, sometimes more changes get made since we last checked the branch build… stuff happens.
So you can add a server-side hook that looks for incoming merges to master. When it finds one, the script checks the latest build on your branch, and if there are any failing tests, the merge is rejected.

Step 1 : Jenkins - New Item - Enter an Item name (any name) - select Maven project - click OK

it will show multi Tab windows

Step 2 : In General Tab provide description

Step 3 : In Source Code Management Tab, Jenkins supports CVS and Subversion out of the box, with built-in support for Git, and also integrates with many other version control systems via plugins. We will look into these details in next tutorials. We will select Git and provide Repository URL like C:\Users\Imran\workspace\GreenFarm\.git

Step 4 : Build Triggers Tab, We will select Trigger builds remotely (e.g., from scripts) and also set Authentication Token.

Step 5 : In Build, We need to tell Jenkins where to find pom.xml file. Please specify the path of your pom.xml file in Build Root POM and Specify 'Goals and options', in this example 'clean test'.
Root Pom: C:\Users\imran\workspace\GreenFarm\pom.xml
Goals and options: package

Step 6:- In Build Settings, If you want to send an email notifications, you can check 'Email Notification' and add Recipients address.

Now configure project with git like below

Command Prompt/Shell
Eclipse GUI
  1. Open up your terminal
  2. Navigate to your project directory
  3. Type git init to create a repository
  4. Assuming you already have files in that folder, type git add --all to add all your files to the repository (Note: if you skip this step, you will have an empty repository. You can also use git add filename to add only specific files/folders)
  5. Type git commit -m "your message here" to perform your first commit
At this point, you have a brand new local repository containing your files! The following steps will hook it up to a remote repository.
  1. Create your remote repository (using GitHub as an example, simply click the New button, and follow the prompts.
  2. Open up your terminal and navigate to your project directory
  3. On the page for your repository, you should see an HTTPS link ending in your-repository-name.git
  4. Paste git remote add origin into your terminal, followed by that HTTPS link
  5. Paste git push -u origin master into your terminal (Note: these last two steps are shown on the GitHub new project page as well, for easy copy-and-pasting into your terminal)
Now you have a local repository connected to a remote repository, ready to use! All Eclipse projects exist somewhere in your file system, and can easily be accessed just like any other folder you might want to turn into a repository.
I do realize you asked to avoid the command line, but this is a relatively simple command line task, and learning to be somewhat familiar with how to use your command line can pay big dividends later on.

Right click on the project, select Team then Share and you will be prompted to select the tool you prefer. Select git and go on.

Next, let's navigate to the .git/hooks directory in our project

Here create file post-commit with following content


curl --user admin:2d910402b37e32a4279fb7523f9066be http://localhost:9192/job/GreenFarmJob-GitHooks/build?token=myhooktoken

now whenever you performed commit it will trigger this script.

No comments: