Integration of Git, Maven and Jenkins provides capability of auto triggering Linux shell or Windows batch scripts to instantly test the newly pushed code or data in Git repository. This article is listing the detail configuration steps of each component for the purpose. The whole architecture is:

This whole solution can be deployed in most mainstream OS including Linux, Windows and MacOS. It is assumes JKD has already been installed in the machine. In this demo, Oracle JDK 1.8 has been used.
Configure Maven
Configure Maven is as easy as two steps:
- Download Maven from https://maven.apache.org/download.cgi. Current up-to-date version is 3.6.3. Download the Binary zip archive to local driver and unzip it.
- Add Maven executable files in environment variable PATH. In Windows, add Maven bin directory into Path. In Linux or MacOS, append the Maven bin directory in path variable in ~/.bash_profile.

# Linux setup
path = path:/apache-maven-3.6.3/bin
- Verify if mvn command can be recognized in command window.


Install Jenkins
- Download Jenkins from https://www.jenkins.io/download. Current up-to-date LTS version is 2.2263.4. The most simple way to run it is to use the Generic Java package jenkins.war. Download it to local driver.
- Run jenkins.war using following command
# Run Jenkins using Java
java -jar jenkins.war

When Jenkins is running for the first time, admin password is generated and visible from console:

Copy it and open a browser to login to default address http://localhost:8080. Past the default password and unlock Jenkins:

Either install recommended Jenkins plugins, or manually choose required plugins. Here we choose recommended plugins, and wait for all plugin installed:


Register new admin user for Jenkins:

Keep the default login URL or change it as per preference, and finish:

Make sure there is no problem to login to Jenkins with admin user:

Configure Jenkins
- Manage Global Tool Configuration

Configure JDK with JAVA_HOME and Name

- Manage Credentials. Because Jenkins need to fetch and pull data from Git repository, it needs the credential to access Github. To access Github, it is recommended to use SSH key which has higher security level. Here in the credential management page, choose “SSH Username with private Key” as credential kind, and manually input private key content in the Key area. SSH key can be generated using this guide: https://docs.github.com/en/enterprise-server@2.21/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent. By default, private key is named as id_rsa, and it is exactly which we need here. The other file id_rsa.pub is public key which is required later when we configuring Git repository.


- Manage Users


Add and generate new API token for user. This is necessary if Jenkins job is designed to be auto triggered from Git web hook.

Markdown the token somewhere as it will be used in Git web hook definition, and save it.

Create Jenkins Job
This is the core setup of continuous integration among Git, Jenkins, Maven and Selenium. Jenkins job is created to be auto triggered by Git push, and then it coordinates Maven build, package, and testing execution.


Provide repository URL of Selenium Automation repository, provide credential of what we just created, and choose the Git repository branch which we want to monitor:

Following 3 checkbox are optional.
- Checking the box for “Trigger builds remotely” enables the job to be triggered from web service.
- Checking the box for “Build periodically” enables the job to be scheduled and executed at regular interval.
- Checking the box for “GitHub hook trigger for GITScm polling” enables the job auto triggered from Github. However, in this case Jenkins must has a public network URL so that Github can reach Jenkins, otherwise it won’t be working.

Add following windows batch commands to execute Maven build, package as well as Selenium testing execution. In Linux there are equivalent shell commands to perform the same operations.
REM Copy the files pulled from Git repository, and move then to another folder maven for build
xcopy /E /Y C:\Users\clark\.jenkins\workspace\SeleniumAutomationExecutionJob\SeleniumAutomation c:\Users\clark\maven\SeleniumAutomation\
REM Change directory to maven folder
cd c:\Users\clark\maven\SeleniumAutomation
REM Maven build
mvn clean install package
REM Change directory to packaged Selenium testing JAR file
cd c:\Users\clark\maven\SeleniumAutomation\target
chdir
REM Execute Selenium testing
java -jar SeleniumAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar

Build it manually to ensure configuration is correct:

Click build number to go into specific Jenkins build:

Click Console Output to monitor the execution progress:

Ensure maven build succeed and Selenium testing is executed:

Configure Github
Configure Public Key
To enable access from Jenkins, public key need to be registered in Github. Click user icon at right top and choose Settings:

Click “SSH and GPG keys” and then “New SSH Key”. This should be the public key id_rsa.pub generated in previous step.

Configure Web Hook
If Jenkins has public network URL, then the job can be triggered by Github web hook. To enable web hook, go to Github repository setting page:

Go to Hooks tab and Add webhook:

Payload URL is public URL of Jenkins with fixed suffix “github-webhook”. Content type can be left as default. Secret the the API token which has been generated in previous Manage User step in Jenkins configuration. By default, web hook is configured for Git push event, but it can be customized as per individual requirement.

If every thing is fine, try perform a new push to the repository monitored by Jenkins job. Web hook is auto triggered after Git push:

Here we are using another Jenkins instance hosted in public network to finish the POC. In Jenkins job GitHub Hook Log, verify Jenkins has received the web hook. In this case, it doesn’t detect new push, so the build is not triggered. If there is new push detected, the job will be executed automatically.

Trigger Jenkins Job from Web Service
In case Jenkins is located in local area network and not directly reachable from Github, an alternative way to trigger the Jenkins job from external system is to use web service.
To simply demo this operation, use command line tool curl to call Jenkins web service:
curl -u clark:<User API Token> localhost:8080/job/SeleniumAutomationExecutionJob/build?token=<Jenkins Job Token>
In this script, <User API Token> is what has been generated in previous Manage User step in Jenkins configuration. <Jenkins Job Token> is what has been defined in Authentication Token in job Build Triggers definition. Specifically in this case, the whole command is:
curl -u clark:112d97bd86d72a38450e04318d5dbf4166 http://localhost:8080/job/SeleniumAutomationExecutionJob/build?token=SeleniumAutomationExecutionJobToken
Verify Jenkins job is triggered remotely from LAN:
