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
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.
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 Apsara 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.
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.
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, set the Packaging Path as shown in the figure.
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. 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.
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.
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.