You need to enable JavaScript to run this app.
Code Pipeline

Code Pipeline

Copy page
Download PDF
Best practices
Package archive and deploy Java source code through a pipeline
Copy page
Download PDF
Package archive and deploy Java source code through a pipeline
This article explains how to use the one-stop pipeline provided by Code Pipeline to compile and build Java source code and automatically deploy it to ECS instances.​
Scenario overview​
For scenarios where applications are deployed on hosts, you can use a Code Pipeline to achieve an integrated experience that covers source code compilation and build, artifact packaging and archiving in the artifact repository, and ECS push deployment. This helps you automate and ensure compliance in your CI/CD processes.​
This article uses the Spring Boot Hello World code as an example, compiles and builds it using Maven, pushes the resulting artifact to an artifact repository for archiving, and downloads the artifact to an ECS instance for deployment.​
Prerequisites​
  • Cloud servers​
  • Cloud server is enabled.​
  • Artifact repository​
  • The artifact repository has been enabled.​
  • A Generic artifact repository has been created, and the repository's publishing policy is set to allow write operations. For specific actions, see Create a Generic repository.​
  • Code Pipeline​
  • Code Pipeline has been enabled.​
  • When uploading an artifact for the first time, you must complete cross-service authorization. For specific actions, see Cross-service authorization.​
Procedure​
Step 1: Create a pipeline​
  1. Log in to the Code Pipeline console.​
  1. In the left navigation bar, select workspace. On the workspace page, click the name of the target workspace to enter the selected workspace.​
  1. Click Create pipeline to open the Select pipeline templates dialog box.​
  1. Select Java - compile and build & artifact upload - application deployment. Click OK. Enter the process orchestration page for the current pipeline.​
Step 2: Process orchestration​
  1. Configure the code source.​
  1. In the code source & trigger area, click add > code source.​
  1. On the configuration input source page that appears, select your code repository type, code repository, and default branch. After the configuration is complete, click OK.​
  1. Configure a Java compile and build task.​
  1. Click the Java Compile Build card to open the edit task panel.​
  1. Configure the code source download strategy. This practice involves compiling and building Java source code, so select download code.​
  1. Configure the Java compile and build step in the panel.​
  • ​
    Configuration Item​
    Descriptions​
    Step name​
    The default is Java compilation. This example uses the default settings.​
    Version​
    Select the Java version based on your requirements.​
    Compilation commands​
    Write compilation commands based on actual business requirements.​
    Skip on failure​
    Once enabled, if the current step fails to run, it will be skipped. This example is not turned on.​
    ​
  1. Configure the artifact upload step in the panel.​
  • ​
    Configuration Item​
    Descriptions​
    Step name​
    The default is artifact upload. Keep the default settings.​
    Region​
    Select the region where the artifact repository is located.​
    Examples​
    Select the instance name of the artifact repository.​
    Repository URL​
    Select the target repository to upload to. Only Generic artifact format repositories with a non-read-only policy can be selected.​
    Artifact file paths​
    Enter the target path in the artifact repository where the artifact will be stored after upload. In this example,/Indicates storage in the root directory.​
    Package Name​
    Custom archived file name in the artifact repository, in this example: webapp-$(PIPELINERUN_ID).jar.​
    Upload a single file?​
    In this example, upload a .jar package. To ensure it is not compressed, turn on this switch.​
    Packaging path​
    Enter the file path of the Java build artifact. This example uses /workspace/target/demo-0.0.1-SNAPSHOT.jar.​
    Skip on failure​
    When enabled, if the current step fails, it will be skipped. This example is not enabled.​
    ​
  1. Leave advanced settings at default.​
  1. Click OK to complete the current task configuration.​
  1. Configure an ECS deployment task.​
  1. Delete the original application deployment stage from the template, add a new stage after Java compilation and build, and add deployment >. ECS Operations and maintenance task.​
  1. Configure the current task as required. Configuration items not mentioned in the table should remain at their default values. After the configuration is complete, click OK.​
  • ​
    Configuration Item​
    Descriptions​
    Upstream tasks​
    Display the names of upstream serial tasks for the current task. This example demonstrates a Java build.​
    Artifact downloads​
    You can select artifacts built by the upstream artifact upload step. These artifacts will be downloaded to the path you configured. The file path for the configuration in this example is /tmp/demo.jar.​
    Region​
    Select the Region where the ECS instance is located.​
    ECS instances​
    Select the ECS instances to deploy. You can select multiple instances at once.​
    warning​
    ECS instances must have Operation Assistant installed; otherwise, the task will fail.​
    Execution paths​
    Enter the command execution path. For Linux instances, the default is in the /home directory.​
    Executing users​
    You can specify an existing user in an instance to execute a command. It is recommended to avoid using the root user to reduce security risks.​
    warning​
    Please specify a user who has permission to execute commands.​
    The timeout period​
    Set the timeout period for command execution on each instance. If execution exceeds the timeout, the task will be considered to have failed on the corresponding instance. This example requires 5 minutes.​
    Command types​
    Select the type of command to execute. This example uses Shell.​
    Command content​
    Enter the script you want to run on the ECS instance. This example script is as follows:​
    ​
    #!/bin/bash​
    ​
    Application configuration variables​
    APP_NAME="demo"​
    APP_JAR_PATH="/home/webapp/${APP_NAME}.jar"​
    APP_PID_PATH="/home/webapp/${APP_NAME}.pid"​
    NEW_APP_JAR="/tmp/${APP_NAME}.jar"​
    APP_LOG_PATH="/home/webapp/${APP_NAME}.log"​
    mkdir -p /home/webapp/​
    ​
    # Check whether the new and old jar files exist​
    if [ ! -f"${NEW_APP_JAR}" ]; then​
    echo "New application artifact not found!"​
    exit 1​
    fi​
    ​
    if [ -f"${APP_JAR_PATH}" ]; then​
    echo "Check current application artifact"​
    ls -al /home/webapp/​
    else​
    echo "Current application artifact not found!"​
    fi​
    ​
    echo "Check new application artifact"​
    ls -al /tmp​
    ​
    Stop the application​
    echo "Stopping the application..."​
    Perform a graceful shutdown of Spring Boot​
    if [ -f"${APP_JAR_PATH}" ]; then​
    if curl -s -o /dev/null -X POST http://localhost:8080/actuator/shutdown; then​
    echo "Shutdown request sent successfully."​
    echo "Application has been stopped."​
    # Delete old jar files​
    echo "Removing the old jar file..."​
    rm -f ${APP_JAR_PATH}​
    else​
    echo "Failed to send shutdown request."​
    fi​
    else​
    echo "Skip Stopping."​
    fi​
    ​
    if [ $? -eq 0 ]; then​
    echo "Old jar file removed successfully."​
    else​
    echo "Failed to remove old jar file."​
    exit 1​
    fi​
    ​
    Replace the JAR file​
    echo "Replacing with the new jar file..."​
    cp ${NEW_APP_JAR} ${APP_JAR_PATH}​
    ​
    if [ $? -eq 0 ]; then​
    echo "New jar file copied successfully."​
    else​
    echo "Failed to copy new jar file."​
    exit 1​
    fi​
    ​
    Start the application​
    echo "Starting the application..."​
    nohup java -jar ${APP_JAR_PATH} > ${APP_LOG_PATH} 2>&1 &​
    echo $! > ${APP_PID_PATH}​
    echo "Application started."​
    ​
    echo "Deployment complete."​
    ​
    ​
  1. Optionally configure basic pipeline information, cache directory, and notifications. This example keeps the default.​
  1. Click Save to save the current pipeline configuration.​
Step 3: Run the pipeline​
  1. On the pipeline list page, select the target pipeline you just created, and click the run icon in the actions column.​
  1. In the pop-up run pipeline dialog box, confirm the code source branch information and optionally enter a run description.​
  1. Click OK to start the pipeline. The system automatically redirects to the details page of the current pipeline.​
Result validation​
Process validations​
On the run details page of the current pipeline, view the run results of each task.​
  • Select the Java build task and click Details for the artifact to go to the artifact repository console, where you can view the details of the uploaded archived artifact.​
  • Select an ECS operations task and click the View batch tasks button to go to the cloud server console, where you can view the execution status of ECS batch tasks.​
Business verification​
Log in to the fully deployed ECS instance and run the following command. The expected output is Hello World! This indicates that the application is running normally.​
​
curl http://localhost:8080/hello​
​
​​​
​
Last updated: 2025.09.10 17:37:25