Build and deploy a Java application to ECS
This topic describes how to use Apsara DevOps pipelines to quickly build and deploy a Java application to ECS.
Background
You develop applications using Java.
You deliver your application as a JAR or WAR artifact.
The artifact runs on ECS instances or your own hosts.
This document helps automate and streamline your development workflow.
User requirements
Typically, users deploy applications to hosts using the following process:
Run quality checks on source code, such as unit tests and code scans.
Build the source code into a deliverable artifact, such as a JAR or WAR file.
Validate the artifact in a staging environment.
Deploy the validated artifact to production.
This process involves developers, testers, and O&M personnel. Apsara DevOps Flow delivery pipelines simplify this workflow and enable seamless collaboration across teams to improve efficiency.
Apsara DevOps solution
Apsara DevOps combines continuous delivery pipelines with host deployment capabilities to provide a solid foundation for continuous application delivery, as shown below:

When a developer pushes code changes to the repository, Apsara DevOps detects the change and automatically triggers a pipeline run. This includes code inspection, building, test deployment, validation, and production deployment. After the build completes, the pipeline packages the artifact and uploads it to a repository. During deployment—whether to staging or production—the pipeline pulls the latest version from the artifact repository and deploys it to Alibaba Cloud ECS instances or self-managed hosts based on your deployment strategy. This approach ensures environments stay synchronized with the latest version, efficiently and reliably.
Hands-on guide with Apsara DevOps
The following example uses a Java Spring Boot repository to demonstrate how to build and deploy to Alibaba Cloud ECS servers.
Use a template to build and configure a pipeline
Go to Apsara DevOps Flow. Click Create Pipeline in the upper-right corner. Select the “Java: Test, Build, and Deploy to Alibaba Cloud ECS/Self-Managed Hosts” template, then click Create.
After creating the pipeline, click Add Pipeline Source. Choose the sample code source provided by Flow and click Add.
Edit the Java Build and Upload task. In the Artifact Upload step, add a packaging path and enter deploy.sh. This file exists in your code repository and contains the script to start your application on ECS. To support later host deployment, include this file in the compressed package. The configuration specifies two paths: target/ and deploy.sh. The pipeline packages these files (or folders) into a single archive, which it stores as an artifact. Later deployment tasks use this artifact. In the build stage, click the Java Build and Upload task to open its editor. In the Artifact Upload step, set the upload method to Archive to Apsara DevOps public storage (for later tasks to pull and deploy). Keep the default artifact name
Artifacts_${PIPELINE_ID}.Next, configure the Host Deployment task. In the artifact dropdown, select the artifact archived in the previous Java Build and Upload step. Choose a host group to deploy to (if you don’t have one, click Create Host Group and refer to Host Group Management). Then configure the deployment script and strategy:
Download path: Specifies where to download the compressed package from the “Build and Upload” task on the target machine. In this example, the value is: /home/admin/app/package.tgz
Execution user: Specifies which user runs the deployment script. In this example, the value is: root
Deployment script: The exact commands to run on the machine. In this example, the value is:
mkdir -p /home/admin/application/ && tar zxvf /home/admin/app/package.tgz -C /home/admin/application/ && sh /home/admin/application/deploy.sh restartPause mode: Determines how machines in a host group pause during deployment—for example, pause after the first batch, pause after every batch, or do not pause. We recommend pausing after the first batch. After deploying the first batch, observe the online service. If no issues appear, continue deploying the remaining batches.
Batch count: Specifies how many batches to split the host group into. For example, if you have four machines and set the batch count to two, each batch deploys two machines simultaneously. In the deployment step, set the Artifact name to
Artifacts_${PIPELINE_ID}, select the Test Sample Host Group, and set the batch count to2.
Add manual approval
To ensure only approved artifacts reach deployment environments, add a manual gate before the host deployment task. On the pipeline editing page, under Flow Configuration, click the + button between stages. In the task selection panel that appears on the right, find and click Manual Gate to add it to your pipeline.
In the manual gate task, select approvers and click Confirm.
Set Approver Mode to OR-sign (approval or rejection by any one approver is sufficient) and Approver Type to Member.
Run the pipeline
After configuration, click Save and Run to trigger the pipeline.
The scan, unit test, and build-and-upload tasks complete automatically and stop at the manual gate: After triggering, the pipeline runs the test and build stages sequentially. When it reaches the Manual Gate stage, it pauses and displays two buttons: Approve and Reject.
Click Approve. The pipeline proceeds to the host deployment task. Click Deployment Details to view more information: The pipeline pauses at the Host Deployment task. The status shows Batch 1/2. The first batch (one machine) has completed. The second batch is pending deployment, with pause mode set to Pause after first batch. Buttons labeled Cancel and Continue appear in the upper-right corner. Click Continue to proceed with the next batch.
Click Logs to view detailed execution logs: The deployment details page shows the deployment record status as Completed, with 2/2 machines finished and 2/2 batches deployed. In the deployment batch list, click the Logs button in the Actions column to view deployment logs. The logs show the deploy.sh execution (stop Java process → start Java process) and the health check process (probe http://127.0.0.1:8080; after several “application not started” responses, it returns code 200 and reports “check success”). The final output is DeployCommand execution completed, indicating successful deployment.
Roll back a deployment
If you discover issues in the online service after deployment, roll back quickly. Apsara DevOps Flow supports direct rollback using historical versions.
On the pipeline run page, click Deployment History, then select the relevant deployment task to view all successful deployment records.
Click Roll Back next to version #2 to revert to that version.
In the Rollback Information – Host Deployment dialog box, confirm the current and target rollback versions. Set Pause Mode to Pause after first batch and Batch Count to 2, then click Confirm.
The deployment history page might not appear after the initial deployment. Refresh the page to resolve this.
Notifications
To improve team collaboration, Flow sends notifications at key lifecycle stages of the pipeline. Development teams typically care about deployment success or failure. You can push these events to your team’s DingTalk group. To configure this, click Add Plugin, choose DingTalk Robot Notification, enter your webhook URL, and run the pipeline again. You will then receive notifications.