All Products
Search
Document Center

CloudOps Orchestration Service:Deploy an application on an ECS instance

Last Updated:Mar 19, 2025

If you want to pull code from GitHub and use the code to build and deploy an application on a single Elastic Compute Service (ECS) instance, you can execute a CloudOps Orchestration Service (OOS) template to build and deploy the application.

Note

This sample template is used to build and deploy applications on a single ECS instance. If you want to publish a build to a repository or deploy it on multiple ECS instances, see Build an image and upload it to Container Registry, Create a Docker image, upload it to Container Registry, and deploy it to ECS, Build a software package and upload it to OSS, and Build a software package, upload it to OSS, and then deploy it on ECS instances.

Prerequisites

Sample template

image
  1. You specify the code source, such as OSS or GitHub. OOS generates a temporary URL for the code source to pull code from the code source.

  2. OOS automatically pulls code to execute a script used to build and deploy an application.

Example

Prepare the code source

The sample code of a Spring Boot project is used in this example. The code is uploaded to Gitee and GitHub. If you want to use the code, you must fork the code to your own repository. Code address:

  • Gitee: Sample code (recommended for users in the Chinese mainland)

  • GitHub:Sample code (recommended for users in the Chinese mainland)

Create a template

  1. Log on to the CloudOps Orchestration Service console.

  2. In the left-side navigation pane, choose Automated Task > Custom Template and click Create Template.

  3. In the Build and Deploy section, select ACS-ECS-ExampleLocalBuildAndDeployOnSingleECSFromGit and click Next Step.

  4. On the Process Configuration tab, set the parameters and click Create Template.

    1. Specify the code source to generate a temporary authorization link.

      In this example, Gitee is used as the code source. Set the following parameters: Owner, Organization, Repository, and Branch.

      Note
      • If Alibaba Cloud is not authorized to access your GitHub or Gitee repository, click Grant the required permissions.

      • If you have forked the sample code, all repositories that belong to your account are automatically displayed in the Repository drop-down list. In this case, select the repository to which you forked the code.

      2024-12-16_11-42-41.png

    2. Pull code from Gitee to build and deploy an application.

      Select the ECS instance on which you want to deploy the application. Set CodeResource to git. The default value authorizedUrl, which is the output of the previous task, is used for CodeResourceUrl. This example is for reference only. You can specify a script based on your requirements.

      部署脚本.png

      Sample script (Alibaba Cloud Linux 3)

      ### Build jar file.
      set -e
      yum install -y maven-3.5.4
      mvn package
      
      ### Stop the previous version of the application (if any) and deploy the current version.
      PID=$(ps -ef | grep "sample-spring-1.0-SNAPSHOT.jar" | grep -v "grep" | awk '{print $2}')
      if [ -n "$PID" ]; then
          kill -9 $PID
      fi
      java -jar target/sample-spring-1.0-SNAPSHOT.jar &

      Sample script (Ubuntu)

      ### Build jar file.
      set -e
      apt install -y maven
      mvn package
      
      ### Stop the previous version of the application (if any) and deploy the current version.
      PID=$(ps -ef | grep "sample-spring-1.0-SNAPSHOT.jar" | grep -v "grep" | awk '{print $2}')
      if [ -n "$PID" ]; then
        kill -9 $PID
      fi
      java -jar target/sample-spring-1.0-SNAPSHOT.jar &

      Sample script (CentOS)

      ### Build jar file.
      set -e
      yum install -y maven
      mvn package
      
      ### Stop the previous version of the application (if any) and deploy the current version.
      PID=$(ps -ef | grep "sample-spring-1.0-SNAPSHOT.jar" | grep -v "grep" | awk '{print $2}')
      if [ -n "$PID" ]; then
        kill -9 $PID
      fi
      java -jar target/sample-spring-1.0-SNAPSHOT.jar &
      Note

      After the template is executed, the code is pulled to a folder in the /root/workspace/task ID directory. This folder is also used as the working path for the build and deployment script. You can also specify a custom path in the script.image

    3. Click Create Template.

Execute the template

  1. Log on to the CloudOps Orchestration Service console.

  2. In the left-side navigation pane, choose Automated Task > Custom Template.

  3. On the Custom Template page, find the template and click Create Execution in the Actions column.

  4. In the Basic Information step, set the parameters and click Next Step: Parameter Settings.

  5. In the Parameter Settings step, click OK if no parameters are available.

  6. Click Create.

    On the Task Execution Management page, if the execution status is Success, the execution is complete.

    执行模板结果-zh.png

  7. Log on to the ECS console.

  8. Find the ECS instance and connect to the instance.

    For more information, see Use Workbench to connect to a Linux instance over SSH.

  9. Run the following command to check whether the deployment is successful.

    curl http://localhost:8080/hello

    If the following field is returned, the deployment is successful.image