Set up CI for Java with Jenkins
This topic shows you how to set up a continuous integration (CI) environment for your Serverless App Engine (SAE) applications using Jenkins.
Prerequisites
-
Host your code in GitLab.
-
Option 1: A self-managed GitLab instance. This topic uses a self-managed GitLab instance for demonstration.
-
Option 2: Alibaba Cloud Code.
-
Background information
You can use Jenkins to build a continuous integration solution for your Serverless App Engine applications. This topic assumes you are familiar with the following tools.
|
Tool |
Description |
|
Maven |
Maven is a project management and build automation tool. |
|
Jenkins |
Jenkins is an extensible continuous integration engine. |
|
GitLab |
GitLab is an open-source application developed with Ruby on Rails. It provides a self-hosted Git project repository accessible through a web interface for public or private projects. It offers features similar to GitHub, allowing you to browse source code, manage issues, and make comments. |
Step 1: Configure the project
Modify your Maven project configuration to add the toolkit-maven-plugin and application deployment information. For detailed instructions, see Use a Maven plugin to automatically deploy applications.
After you modify the project configuration, run a local Maven build to verify the configuration.
Step 2: Configure Jenkins
-
In the Jenkins console, go to and install the Git and GitLab plugins.
Note-
The Git Client and Git plugins allow Jenkins to pull code from Git repositories.
-
The GitLab Hook Plugin lets Jenkins trigger a build job when it receives a hook from GitLab.
On the Manage Jenkins > Manage Plugins page, select the Available tab. In the search box, enter
gitto filter the list. Select the required plugins from the list and click Install without restart. -
-
In the Jenkins console, go to . Set a name for your Maven version, configure its path, and then click Save.
For example, enter
Apache Maven 3.6.3in the Name field and the Maven installation path in the MAVEN_HOME field. -
Generate an SSH RSA key pair on the Jenkins server and add the public key to GitLab to allow authenticated code pulls.
-
Generate an SSH RSA key pair on the Jenkins server.
Run the command
ssh-keygen -t rsa -C "admin@example.com"to generate a key pair. Then, runcat ~/.ssh/id_rsa.pubto view the public key. Copy the public key to your clipboard: on Windows, runclip < ~/.ssh/id_rsa.pub; on macOS, runpbcopy < ~/.ssh/id_rsa.pub; on GNU/Linux, runxclip -sel clip < ~/.ssh/id_rsa.pub. -
In GitLab, go to , click New Deploy Key, and add the public key from the Jenkins server.
In the Title field, enter a name for the key. In the Key field, paste the public key that you copied. Then, click Add key.
-
Step 3: Create a Jenkins job
-
On the Jenkins homepage, click New Item in the left-side navigation pane. Enter a name for the job, select Freestyle project, and then click OK.
In this example, the job name is
testapp. -
On the Source Code Management tab, select Git and configure the parameters.
-
Repository URL: The Git protocol URL of your project.
-
Credentials: Select None.
ImportantEnsure you have added the SSH RSA public key to the GitLab project. Otherwise, the build will fail.
Under Source Code Management, select Git. In the Branch Specifier (blank for 'any') field under Branches to build, enter
*/master. -
-
On the Build Triggers tab, select Build when a change is pushed to GitLab.
-
On the Build Environment tab, select Add timestamps to the Console Output.
-
Click the Build tab, click Add build step, and select Invoke top-level Maven targets from the drop-down list.
-
In the Invoke top-level Maven targets section, configure Maven Version and Goals. To deploy a multi-module project, see (Optional) Create a Jenkins job for a multi-module project.
-
Maven Version: From the drop-down list, select the Maven version name that you configured in Global Tool Configuration.
-
Goals: Enter mvn clean package toolkit:deploy -Dtoolkit_profile=toolkit_profile.yaml -Dtoolkit_package=toolkit_package.yaml -Dtoolkit_deploy=toolkit_deploy.yaml. Add other parameters as needed.
NoteAfter Maven is configured, you can trigger application deployments by calling the POP API in Step 5: Commit changes to GitLab.
-
Step 4: Configure a GitLab webhook
-
In your GitLab project, go to Settings > Webhooks.
-
On the Webhooks page, in the URL field, enter
http://<jenkins-server-address>:<jenkins-listening-port>/git/notifyCommit?url=<git-project-ssh-url>.The Jenkins server address is the web access address of your Jenkins server, for example,
192.168.XX.XX:8080.In the URL field, enter the Jenkins notification URL. The format is
http://<Jenkins-server-address>/git/notifyCommit?url=<GitLab-project-SSH-address>. Ensure the GitLab project address matches the repository address configured in Jenkins. In the Trigger section, select Push events, and then click Add webhook. -
Click Test hook to verify the configuration.
An HTTP 200 status code confirms the hook is configured correctly.
Step 5: Commit changes to GitLab
If everything is configured correctly, a commit to GitLab triggers a webhook. Jenkins receives the hook, builds your Maven project, and then deploys the application by calling the Serverless App Engine POP API.
The Maven project is configured to deploy the application using the Serverless App Engine POP API.
A successful deployment generates the following log output, viewable in .
15:58:51 [INFO] Deploy application successfully!
15:58:51 [INFO] ------------------------------------------------------------------------
15:58:51 [INFO] BUILD SUCCESS
15:58:51 [INFO] ------------------------------------------------------------------------
15:58:51 [INFO] Total time: 24.330 s
15:58:51 [INFO] Finished at: 2018-12-25T15:58:51+08:00
15:58:51 [INFO] Final Memory: 23M/443M
15:58:51 [INFO] ------------------------------------------------------------------------
15:58:51 Finished: SUCCESS
If a deployment fails, view its progress in the SAE console. For more information, see View change records.
(Optional) Create a multi-module Jenkins job
If you need to create a Jenkins job for a multi-module project, refer to the following settings.
Creating a Jenkins job for a multi-module project is similar to the process in Step 3: Create a Jenkins job, but you must modify the Invoke top-level Maven targets settings. For a multi-module project, if you want to deploy a submodule in Jenkins, you must run the mvn clean install command in the parent module, and then run the mvn clean package toolkit:deploy -Dtoolkit_profile=toolkit_profile.yaml -Dtoolkit_package=toolkit_package.yaml -Dtoolkit_deploy=toolkit_deploy.yaml command in the submodule. For more information about the parameters, see Use a Maven plugin to automatically deploy applications.
The following example shows the structure of the demo project.
sh-3.2# tree -L 1 carshop
carshop
├── detail
├── itemcenter
├── itemcenter-api
└── pom.xml
In this structure, detail, itemcenter, and itemcenter-api are submodules. If you want to deploy the itemcenter module, you must set a clean install build goal in the parent project, and then set a clean package toolkit:deploy -Dtoolkit_profile=toolkit_profile.yaml -Dtoolkit_package=toolkit_package.yaml -Dtoolkit_deploy=toolkit_deploy.yaml build goal in the itemcenter module.
In the second build step, set the POM field to itemcenter/pom.xml to specify the submodule.