Build a Java application and deploy it to an ECS host

更新时间:
复制 MD 格式

This topic describes how to build and deploy a Java application to an ECS host using Maven. This topic provides an example of how to quickly publish a project using Apsara Devops Flow.

Prerequisites

You must have an Alibaba Cloud ECS instance or another host instance with a Java environment installed. You must also add the host to a custom host group in Apsara Devops. For more information, see Host group management.

Step 1: Create a pipeline

  1. Go to Apsara Devops Flow. On the My Pipelines page, click New Pipeline in the upper-right corner. In the Select a pipeline template dialog box, select a programming language to view the default templates for creating a pipeline.

  2. Click Java, select the Java · Test, build, and deploy to Alibaba Cloud ECS/self-managed host template, and then click Create to open the pipeline editing page.

Step 2: Orchestrate the pipeline

Add a pipeline source

  1. Go to the pipeline editing page and click the Flow Configuration tab. Click Add Pipeline Source and select Sample Code Source. The Java code type is selected by default, and fields such as Code Repository Address, Default Branch, and Working Directory are automatically populated. This example uses the Apsara Devops sample code repository. You do not need to make any changes.

  2. Click Add. The pipeline source appears in the source area.

Configure the Maven unit test

  1. Click Maven Unit Test to open the task configuration panel.

  2. To ensure a successful application deployment in this tutorial, clear the Stop pipeline on test case failure checkbox. This allows the pipeline to proceed to the next deployment step even if the unit test fails.

    Important

    The configuration for clearing the "Stop pipeline on test case failure" checkbox is for demonstration purposes only. This lets you quickly validate the entire process from build to deployment. In a production environment, keep the Stop pipeline on test case failure checkbox selected.

Configure the Java build task

  1. Click the Java Build and Upload task to open the task configuration panel.

  2. You can view the configuration of the Java Build step and modify it as needed.

  3. View the configuration of the Upload Build Output step and modify it as needed. In this example, to package the target/application.jar and deploy.sh files into the artifact, set the Packaging Path as shown in the figure.image

Configure the host deployment task

  1. Click the Host Deployment task to open its configuration panel.

  2. Configure the form as follows:

    1. Artifact: From the drop-down list, select the output from the upstream build stage.

    2. Host Group: Select the host group for deployment. If you do not have a host group, you must create one. For more information, see Host group. To deploy to multiple host groups, you can add a parallel deployment task.

    3. Download Path: The path on the host where the artifact from the Upload Build Output step is downloaded. In this example, the path is /home/admin/app/package.tgz.

    4. Execution User: The user that runs the deployment script, such as root or admin. In this example, the user is root.

    5. Deployment Script: The actual deployment script is already packaged in the compressed build output. Therefore, the deployment script in this step only needs to decompress the package and run the script within it. Enter the following deployment script:

      mkdir -p /home/admin/application
      tar zxvf /home/admin/app/package.tgz -C /home/admin/application/
      sh /home/admin/application/deploy.sh restart

Run the pipeline and view the results

  1. After you complete the configuration, click Save and Run. The Run Configuration dialog box appears. Keep the default master branch and click Run. This action triggers the pipeline and opens the pipeline run page.

  2. The pipeline run page displays the progress and results of the pipeline. To view the build logs, click Log on the Java Build and Upload task card.

  3. On the Host Deployment task card, click Deployment Details to view details about the deployment, such as its duration, status, and logs. A status of Completed indicates that the project has been published.

Configure runtime notifications

If necessary, you can configure message notifications in the pipeline. This lets you send information about the pipeline run to your team to help them quickly identify and resolve issues.