Build a Java application and deploy it to an ECS host
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 Alibaba Cloud 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 Alibaba Cloud DevOps. For more information, see Host group management.
Step 1: Create a pipeline
Go to Alibaba Cloud 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.
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
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 Alibaba Cloud DevOps sample code repository. You do not need to make any changes.
Click Add. The pipeline source appears in the source area.
Configure the Maven unit test
Click Maven Unit Test to open the task configuration panel.
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.
ImportantThe 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.
Select a build cluster
On the pipeline editing page, click Java Build and Upload to open the task configuration panel. The Build Cluster field is the first required configuration item in the panel. Select a build cluster from the drop-down list:
Public build cluster: A public build cluster available in the China (Beijing), China (Hangzhou), and China (Hong Kong) regions. No additional configuration is required.
VPC-hosted build cluster: Creates a build environment within your VPC for enhanced network security.
Private build cluster: A dedicated build cluster.
To create a new build cluster, click New Build Cluster or go to Build cluster management.
Configure the Java build task
Click the Java Build and Upload task to open the task configuration panel.
You can view the configuration of the Java Build step and modify it as needed. In the Java Build step of the task configuration panel, confirm that JDK Version is set to JDK 1.8, Maven Version is set to Maven 3.5.2, and Build Command keeps the default Maven build command.
View the configuration of the Upload Build Output step and modify it as needed. In this example, to package the
target/application.jaranddeploy.shfiles into the artifact, enter the paths of these two files in Packaging Path, set Upload Method to Archive to the Yunxiao public storage space (for subsequent tasks to pull and deploy), set the artifact name toArtifacts_${PIPELINE_ID}, and select Include the directory of the packaging path in the artifact.
Configure the host deployment task
Click the Host Deployment task to open its configuration panel.
Configure the form as follows:
Artifact: From the drop-down list, select the output from the upstream build stage.
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.
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.Execution User: The user that runs the deployment script, such as
rootoradmin. In this example, the user isroot.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
After you complete the configuration, click Save and Run in the upper-right corner. In the Run Configuration dialog box that appears, confirm that the pipeline source branch is master, and then click Run. This action triggers the pipeline and opens the pipeline run page.
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.
After the pipeline run is complete, the run page shows that all stages (test, build, and deploy) are in the Successful state. You can click Log on each task card to view the execution details.
After the pipeline is triggered, you can view the Maven build log in the Java Build step. Example output:
[executionStep begins at 2024-10-11 14:22:20] [INFO] PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin [14:22:20] [SUCCESS] Credential obtained successfully [14:22:20] [INFO] Using working directory /root/workspace/spring_boot_PP0D Loading user-defined environment variables USER_KEY=0.0.1 USER_KEY2=value2 [14:22:20] [INFO] Context initialization [INFO] PIPELINE_ID=3597198 [INFO] PIPELINE_NAME=Pipeline 2024-10-11 [INFO] BUILD_NUMBER=3 [INFO] EMPLOYEE_ID=66a6f60e368d88cff5a513fc [INFO] WORK_SPACE=/root/workspace [INFO] PROJECT_DIR=/root/workspace/spring-boot_PP0D [INFO] BUILD_JOB_ID=253376730 [INFO] CI_RUNTIME_VERSION=jdk1.8 [INFO] This enterprise has enabled the Alibaba Cloud DevOps Maven private repository service, so the default settings.xml contains the Alibaba Cloud DevOps private repository configuration. To use a custom settings.xml during the build, contact your enterprise administrator to configure a global custom settings.xml, or place settings.xml in the root directory of the code repository and add -s settings.xml to the Maven build command. [14:22:20] [INFO] Running step [14:22:20] [INFO]Loading base cache [14:22:20] [INFO]Running user command [14:22:20] [User Command] + mvn -B clean package -Dmaven.test.skip=true -Dautoconfig.skip [14:22:21] [INFO] Scanning for projects... [14:22:21] [INFO] [14:22:21] [INFO] ------------------------------------------------------------------------ [14:22:21] [INFO] Building spring-boot-hello-world 0.0.1-SNAPSHOT [14:22:21] [INFO] ------------------------------------------------------------------------ [14:22:22] [INFO] [14:22:22] [INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ spring-boot-hello-world --- [14:22:22] [INFO] [14:22:22] [INFO] --- jacoco-maven-plugin:0.8.5:prepare-agent (default-prepare-agent) @ spring-boot-hello-world --- [14:22:22] [INFO] argLine set to -javaagent:/root/.m2/repository/org/jacoco/org.jacoco.agent/0.8.5/org.jacoco.agent-0.8.5-runtime.jar=destfile=/root/workspace/spring-boot_PP0D/target/jacoco.exec [14:22:22] [INFO] [14:22:22] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ spring-boot-hello-world --- [14:22:23] [INFO] Using 'UTF-8' encoding to copy filtered resources. [14:22:23] [INFO] Copying 1 resource [14:22:23] [INFO] Copying 0 resource [14:22:23] [INFO] [14:22:23] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ spring-boot-hello-world --- [14:22:23] [INFO] Changes detected - recompiling the module! [14:22:23] [INFO] Compiling 3 source files to /root/workspace/spring-boot_PP0D/target/classesOn 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. In this example, the deployment details show the Successful state, with deployment order ID 50957834, a host failure rate of 0.00%, 1/1 completed machines, a deployment duration of 8 seconds, and deployment batch 1/1. The host list below shows that the target host is in the Completed state. You can click Log to view the deployment log of the host.
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.