All Products
Search
Document Center

Enterprise Distributed Application Service:Use Jenkins for CI

Last Updated:Mar 14, 2024

This topic describes how to build a continuous integration (CI) solution for applications deployed in Enterprise Distributed Application Service (EDAS). The following computer languages and development tools are used in a CI solution. You must be familiar with these languages and tools.

Prerequisites

Before you implement CI, you must complete the following preparations:

  1. An AccessKey ID and an AccessKey secret are obtained. To obtain an AccessKey ID and an AccessKey secret, perform the following steps:

    1. Log on to the official website of Alibaba Cloud by using the Alibaba Cloud account that you use to activate EDAS.

  2. An application is created and deployed in the EDAS console. To create and deploy an application in the EDAS console, perform the following steps:

    1. Log on to the EDAS console.

    2. Create and deploy an application. For more information, see Overview and Overview.

      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 created in the preceding step. On the Basic Information tab, obtain the application ID.

  3. Your code is hosted in GitLab. You can build a GitLab package or use Alibaba Cloud Code.

    In this topic, a self-managed GitLab package is used as an example. For more information, visit GitLab.

  4. You are familiar with Jenkins. For more information, visit Jenkins.

Background information

Note

You can use Jenkins to implement CI on the applications that are deployed on Elastic Compute Service (ECS) clusters and Kubernetes clusters.

Tool

Description

Maven

Maven is an automation tool that is used to automatically build and manage projects.

Jenkins

Jenkins is a scalable automation server that facilitates CI.

GitLab

GitLab is an open source, self-hosted software development platform that allows you to access private or public repositories on webpages. GitLab is built by using Ruby on Rails. Similar to GitHub, GitLab allows you to review code, manage defects, and leave comments.

Configure a project

Configure a project and the toolkit-maven-plugin plug-in, and specify the deployment information for the plug-in. For more information, see Use the toolkit-maven-plugin plug-in to update an application that is deployed in an ECS cluster. After you configure the project, we recommend that you use Maven on a self-managed server to verify 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 System Management > Plugins, and install the Git and GitLab plug-ins.

    • The git client plugin and git plugin fetch code from the Git repository.

    • The GitLab hook plug-in enables GitLab webhooks to trigger a build on GitLab projects.

  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. Generate an SSH RSA key pair on the Jenkins server and import the public key into GitLab. This enables automatic authentication of Jenkins when Jenkins fetches code from GitLab.

    1. Generate an SSH RSA key pair for a Jenkins user connected to the Jenkins server. For more information, see the GitLab documentation.

    2. On the GitLab homepage, choose Settings > Deploy Keys. Click New Deploy Key to add a key, and import the SSH RSA public key you create on the Jenkins server.

  5. Create a Jenkins job.

    1. On the Jenkins homepage, choose New Item, create a Jenkins job, and select Freestyle Project.

    2. On the Source Code Management page, select Git, and set the parameters.

      • Repository URL: the URL of the Git repository of your project.

      • Credentials: the security credentials. Select None if the SSH RSA key pair of the user that runs Jenkins has been added to GitLab of this Git 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. Set other 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 server 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 in the preceding figure is the URL of your Jenkins server, such as http://123.57.xx.xxx:8080.

  3. Then, click Test Hook.

Submit the changes to GitLab

If the preceding configurations are valid, a GitLab hook is triggered. After the hook is received, Jenkins builds your Maven project and then calls the EDAS POP API script to trigger deployment.

After the project is deployed, you can choose Build Number > Console Output to view the output log.

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 deployment fails, 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 Records to view the execution process of this deployment.

Create a Jenkins job for a multi-module project

The procedure used to create a Jenkins job for a multi-module project is the same as Step 5. You only need to adjust the invoke top-level Maven targets. If you want to deploy a submodule for a multi-module project in Jenkins, run the mvn clean install command in the parent module. Then, run the mvn clean package toolkit:deploy command in the submodule. The following structure of the demo project is used as an example:

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