Deploy application code to an ECS instance
The ECS build and deploy feature automatically pulls the latest code from a Git repository for local builds and automated deployments on a single instance. This method is convenient, efficient, and cost-effective. This topic describes how to quickly deploy application code to a single ECS instance.
This topic describes how to build and deploy a frequently updated application on a single ECS instance. If you need to deploy an application on multiple ECS instances, see Build and deploy example templates.
Prerequisites
The target ECS instance must meet the following requirements:
-
The ECS instance runs a Linux operating system.
-
The ECS instance must have public network access enabled.
-
The ECS instance must be in the Running state.
-
Cloud Assistant is installed on the ECS instance, and its agent status is Normal.
Prepare the code source
This topic uses a Spring Boot project as an example. The code is available in Gitee and GitHub repositories. To use the sample code, you must first fork it to your own repository. Alternatively, you can download the code as a ZIP package and upload it as a local code source (see Procedure for details). The repository URLs are:
-
Gitee: Gitee sample code (Gitee is recommended for regions in the Chinese mainland.)
-
GitHub: GitHub sample code (GitHub is recommended for regions outside the Chinese mainland.)
Procedure
Go to ECS console - Instances.
In the upper-left corner of the page, select a region and resource group.
-
Click the ID of the target instance to open its details page. Choose .
Switch to the standard console to access the Scheduled and Automated Tasks option.
-
On the Create an execution page, configure Code Source Configuration and Build and Deploy Configuration, and then click Next.
-
Code Source Configuration:
Git repository
Select the Owner, Organization, Repository, and Branch, and then enter a Download Path.
Important-
If you have not authorized Alibaba Cloud to access your code repository on GitHub or Gitee, click Go to Authorization to complete the authorization process.
-
If you have already forked the sample repository, it will automatically appear in the repository drop-down list. Select the forked repository.
-
The Download Path is the path on your ECS instance where the code is downloaded. If you leave this field empty, the default path
/root/workspace/{current_execution_ID}is used. If you specify a custom path, make sure the directory is empty. Otherwise, the code pull will fail.
Upload local code
You can compress your local project code and upload it (only ZIP and tar.gz formats are supported), and then specify a Download Path.
-
Note: The system automatically downloads and extracts the uploaded compressed file to the download path. The top-level folder is not preserved during extraction. For example, assume you have a compressed file named project_files.zip with the following internal structure.
project_files.zip ├── project/ # Top-level folder │ ├── design.png # File │ ├── report.docx # File │ └── data/ # Subfolder (contains other files)If your target folder is
Work/, the top-level folderprojectis not preserved after extraction. Instead,design.png,report.docx, anddata/are placed directly inWork/.Work/ # Placed directly in the root of the target folder ├── design.png ├── report.docx └── data/ -
If you leave the Download Path empty, the default path
/root/workspace/{current_execution_ID}is used.
Before uploading the file, select the I agree to store this file in a temporary OSS bucket provided by Alibaba Cloud checkbox. When specifying a download path, make sure the directory is empty, or the code pull will fail.
-
-
Build and Deploy Configuration:
-
Use Docker to build and deploy: When you enable this feature, the system checks whether Docker is installed on your ECS instance. If Docker is not installed, the system automatically installs it. You can then use Docker commands in your build and deploy script.
ImportantOnly the following operating systems support building and deploying with Docker:
-
Alibaba Cloud Linux 3/2
-
Alibaba Cloud Linux 3 Pro
-
CentOS 7.7/7.8/7.9
-
Ubuntu 18.04/20.04/22.04/24.04
-
Debian 11/12
Build and Deployment Mode supports both Automated Mode and Custom Script Mode. If you select Automated Mode, you only need to provide information such as the Dockerfile Path, Context Path, or Docker Run Options. When the task runs, it automatically builds a Docker image and starts a container. Alternatively, you can select Custom Script Mode to write your own build and deployment script. The following code provides a sample custom Docker deployment script:
# Build an image for the current version. docker build -t myapp:latest . container_name="my-container" # Stop the previous version of the container, if any. container_id=$(docker ps -aq -f name=${container_name}) if [ -n "$container_id" ]; then docker stop $container_id docker rm $container_id fi # Start the current version of the container. docker run -d -p 8080:8080 --name ${container_name} myapp:latest -
-
Build and deploy without Docker: If you do not use the Docker build feature, you only need to provide a build and deploy script. This topic provides only a sample script. You can replace the sample script with one that is suitable for your project.
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 &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 &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 &
-
-
-
Confirm the Basic Information and Task Information, and then click OK.
On the Building and Deployment tab, find the task in the execution list and check its status. When the Execution Status is Completed, the deployment is complete. The task pulls code from your specified repository to the ECS instance and runs the build and deploy script in the root directory of your code.
-
Verify the deployment on the ECS instance.
For the example in this topic, open a web browser and enter
http://<public_IP_of_your_ECS_instance>:8080/helloto view the deployed application. The following message indicates a successful deployment.NoteFor this example, you must add a security group rule to allow inbound traffic on port 8080 for the ECS instance. For more information, see Add a security group rule.
Alibaba Spring Sample! -
(Optional) To deploy new code changes from your repository, click Execute Again. The deployment script stops the current application, and then pulls and deploys the latest code.