This topic shows you how to use Jenkins to build a project for continuous integration of applications in Enterprise Distributed Application Service (EDAS). Computer languages and development tools are used in the following examples. Before you get started with this topic, make sure that you are familiar with these languages and tools.

Prerequisites

Before you configure continuous integration, make sure that the following operations are performed:
  1. Obtain the AccessKey ID and AccessKey secret of your Alibaba Cloud account.

    1. Log on to the official Alibaba Cloud website by using your Alibaba Cloud account for which EDAS is activated.
  2. Before you use Jenkins to enable automatic deployment of applications, create and deploy an application in the EDAS console.
    1. Log on to the EDAS console.
    2. Create and deploy an application. For more information, see Overview and Application hosting overview (applicable to Kubernetes clusters).

      If you have created an application, skip this step.

    3. In the left-side navigation pane, click Applications. Click the name of the application that you create in the preceding step. On the application details page, obtain the application ID.
  3. Manage code on GitLab. You can build a GitLab project or use Alibaba Cloud Code.

    In this topic, a self-managed GitLab project is used as an example. For more information about GitLab, see GitLab.

  4. Familiarize yourself with Jenkins. For more information about Jenkins, see Jenkins official website.

Background information

Note You can use Jenkins to enable continuous integration of applications in Elastic Compute Service (ECS) clusters and Container Service Kubernetes clusters.
Tool Description
Maven Maven is a tool that enables the system to automatically build and manage projects.
Jenkins Jenkins is a scalable engine for continuous integration.
GitLab GitLab is an open source application developed by using Ruby on Rails. GitLab provides a self-managed Git repository that allows you to connect to a public or private project from a web page. GitLab enables similar features to GitHub. For example, you can view source code, and manage bugs and annotations on GitLab.

Configure a project

Modify the configuration of your project. You can add toolkit-maven-plugin and deployment information. After you modify the project configuration, we recommend that you use Maven on your on-premises machine to check whether the configuration is valid.

Install and configure Jenkins

  1. Download Jenkins from the Jenkins official website and install it.
  2. In the left-side navigation pane of the Jenkins console, choose Manage Jenkins > Manage Plugins. Then, install the Git and GitLab plug-ins.
    • The Git client plug-in and Git plug-in enable Jenkins to pull code from Git repositories.
    • The GitLab Hook plug-in enables Jenkins to trigger project building after Jenkins receives a hook from GitLab.
  3. Install Maven. For more information, visit the Maven official website.

    In the left-side navigation pane of the Jenkins console, choose Manage Jenkins > Global Tool Configuration, select the Maven version, and then configure the path.

  4. Create a Secure Shell (SSH) RSA key pair on the Jenkins server and import the public key into GitLab. This provides automatic authentication for Jenkins when Jenkins pulls code from GitLab.
    1. Create an SSH RSA key pair for a Jenkins user that is connected to the Jenkins server. For more information, see GitLab documentation.
    2. On the GitLab homepage, choose Settings > Deploy Keys and click new deploy key. On the page that appears, import the SSH RSA public key that you create on the Jenkins server to add the key.
  5. Create a Jenkins job.
    1. In the left-side navigation pane of the Jenkins homepage, click New Item to create a Jenkins job and select Freestyle project.
    2. On the Source Code Management page, select Git and configure the parameters.
      • Repository URL: the URL of the Git repository of your project.
      • Credentials: the security credentials. Select None if the SSH RSA public key of the user that runs Jenkins has been added to your GitLab project. Otherwise, an error is returned.
    3. Click the Build Triggers tab and select Poll SCM.
    4. Click the Build Environments tab and select Add Timestamps to the Console Output.
    5. Click the Build tab and click Add Build Step.
    6. In the Invoke Top-level Maven Targets section, configure Maven Version and Goals. You can also deploy a multi-module project. For more information, see Create a Jenkins job for a multi-module project.
      • Maven Version: Click the drop-down list next to this field and select the Maven version you configured on the Global Tool Configuration page.
      • Goals: Enter clean package toolkit:deploy. If other parameters are available, configure the parameters as needed.

Configure GitLab web hooks to enable automatic building

  1. Right-click the GitLab project and choose Setting > Web Hooks.
  2. On the Web Hooks page, enter http://Jenkins sever address:Listening port of the Jenkins server/git/notifyCommit?url=Git repository address of your project in the URL field.

    For example, you can enter http://123.57.xx.xxx:8080/git/notifyCommit?url=git@code.aliyun.com:tdy218/hello-edas.git.

    The Jenkins server address indicates the web address of your Jenkins server, as shown in the preceding figure. For example, you can specify http://123.57.xx.xxx:8080.

  3. Then, click Test Hook.

Commit the changes to GitLab

If the preceding configurations are valid, a GitLab hook is triggered. Jenkins builds your Maven project after Jenkins receives this hook. After your project is built, Jenkins calls the EDAS POP API script to trigger application deployment.

You can choose Build Number > Console Output to view the output log. The following log information shows that the application is deployed.

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 the application fails to be deployed, log on to the EDAS console. In the left-side navigation pane, click Applications. On the Applications page, click the name of the application that you want to manage. The application details page appears. In the left-side navigation pane, click Change List to view the execution process of this deployment job.

Create a Jenkins job for a multi-module project

The operations for creating a Jenkins job for a multi-module project is the same as those that are described in Step 5 in Install and configure Jenkins. You need to change only the Invoke Top-level Maven Targets configurations. Assume that your project is a multi-module project and you want to deploy a submodule in Jenkins. In this case, run the mvn clean install command in the parent module and run the mvn clean package toolkit:deploy command in the submodule. The demo project uses the following structure:

In the demo project, detail, itemcenter, and itemcenter-api are submodules. Assume that you want to deploy the itemcenter submodule. In this case, run the clean install command in the parent module to build a target. Then, run the clean package toolkit:deploy command in the itemcenter submodule to build a target.