Command-only deployment package

更新时间:
复制 MD 格式

You can use a command-only deployment package to deploy an application using only scripts, without providing any source files.

How it works

Important
  • If you deploy to an Auto Scaling (ESS) group, scaling activities are paused during the deployment. They resume automatically after the deployment finishes, whether it succeeds or fails.

  • The deployment process follows a stop-then-start principle. Each deployment first runs the stop script and then runs the start script.

  1. Create and publish a deployment.

    Configure a deployment with your application's source file information, start script, and stop script.

    Create a release task, select a release mode, and publish the deployment to the target application group.

  2. The automated deployment process works as follows:

    1. The service retrieves the deployment package information, including the startup and shutdown scripts.

    2. Run shutdown script: The service executes the shutdown script to stop the previous application version.

    3. Run startup script: The service executes the startup script to start the new application version.

Usage notes

  • Deployments are supported only on Linux instances.

  • The start and stop scripts must be shell scripts.

Procedure

Java application example

  1. Create an application and import your Elastic Compute Service (ECS) instances.

    1. If you do not have an ECS instance, go to the ECS console - Custom Launch page to create a Linux ECS instance.

      The following example scripts are for these images. If you use a different image, you must modify the scripts accordingly.
    2. Go to the ECS console - Application management page and click Create from Existing Resources to create an application and an application group, and import the ECS instance into the application group.

  2. Create a deployment package.

    1. Go to the ECS console - Application management page. On the My Applications tab, click the name of your target application.

    2. On the application details page, select the Deployment tab, and then click Create Deployment.

    3. On the Create Deployment page, set Deployment Package Type to Run Command, configure the parameters, and then click OK.

      • Working directory: Specify the working directory for the startup and shutdown scripts. Example: /root/deploy.

      • Application startup script:

        Alibaba Cloud Linux

        start_application() {
          set -e
          curl -O https://oos-public-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/code-deploy/sample-spring-1.0-SNAPSHOT.jar
          yum install -y maven-3.5.4
          java -jar ./sample-spring-1.0-SNAPSHOT.jar &
        }
        
        start_application

        Ubuntu

        start_application() {
          set -e
          curl -O https://oos-public-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/code-deploy/sample-spring-1.0-SNAPSHOT.jar
          apt update
          apt install -y maven
          java -jar ./sample-spring-1.0-SNAPSHOT.jar &
        }
        
        start_application
      • Application shutdown script:

        ### Stop the application (if any)
        stop_application() {
          PID=$(ps -ef | grep "sample-spring-1.0-SNAPSHOT.jar" | grep -v "grep" | awk '{print $2}')
          if [ -n "$PID" ]; then
            kill -9 $PID
          fi
        }
        
        stop_application
  3. Go back to the deployment list, find the deployment that you created, and click Deploy. Select the target group and click OK to start the deployment.

  4. Verify the result.

    1. Navigate to the details page of the target instance. Click Connect and select Workbench. Follow the on-screen prompts to access the terminal.

    2. Run the curl http://localhost:8080/hello command. A response of Alibaba Spring Sample! indicates a successful deployment.

Docker application example

  1. Prepare the application, group, and ECS instances.

    Before you deploy, create an application and a group in ECS Application Management. Then, add your ECS instances to the group.

    1. If you do not have an ECS instance, go to the ECS console - Custom Launch page to create a Linux ECS instance.

      The following example scripts are for these images. If you use a different image, you must modify the scripts accordingly.
    2. Go to the ECS console - Application management page and click Create from Existing Resources to create an application and an application group, and import the ECS instance into the application group.

    3. Install Docker on the ECS instances in the group. On the O&M tab of the application group, select ACS-ECS-BulkyConfigureOOSPackageWithTemporaryURL to install Docker in a batch.

      If your ECS instance was created from a custom image, you cannot install extensions this way. You must remotely connect to the instance and manually install Docker.
  2. Create a deployment package.

    1. Pull the sample image to your local machine, and then push the image to your ACR Personal Edition repository.

      docker pull aliyun-computenest-opensource-registry.cn-hangzhou.cr.aliyuncs.com/default/aliyun-code-deploy:latest
    2. Go to the ECS console - Application management page. On the My Applications tab, click the name of your target application.

    3. On the application details page, select the Parameters tab and click Create Parameter. Create two parameters, username and password, for the ACR Personal Edition username and password. To ensure security, create the password parameter as an encrypted parameter.

    4. On the application details page, select the Deployment tab, and then click Create Deployment.

    5. On the Create Deployment page, set Deployment Package Type to Run Command, configure the parameters, and then click OK.

      • Working directory: Specify the execution directory for the startup and shutdown scripts. Example: /root/deploy.

      • Application startup script: Replace the <repo> and <image> placeholders with your ACR Personal Edition repository and image details.

        In the following image, the first field is repo, and the second field is image. Replace the corresponding parameters in the application startup script with the values of these two fields.image
        ### Start the current version of the application
        start_application() {
           repo="<repo>"
           image="<image>"
           container_name="my-container"
           docker login --username=${username} --password=${password} $repo
           docker pull $image
           docker run -d -p 8080:8080 --name $container_name $image
        }
        
        start_application
      • Application shutdown script:

        ### Stop the container (if any)
        stop_application() {
          # Find the container by name and remove it if it exists.
          container_name="my-container"
          container_id=$(docker ps -aq -f name=${container_name}) 
          if [ -n "$container_id" ]; then
            docker rm -f $container_id
          fi
        }
        
        stop_application
  3. Go back to the deployment list, find the deployment that you created, and click Deploy. Select the target group and click OK to start the deployment.

  4. Verify the result.

    1. Navigate to the details page of the target instance. Click Connect and select Workbench. Follow the on-screen prompts to access the terminal.

    2. Run the curl http://localhost:8080/hello command. A response of Alibaba Spring Sample! indicates a successful deployment.

Key parameters

Parameter

Description

Working directory

The working directory for the application startup and shutdown scripts.

  • Enter an absolute path.

  • You can specify a directory that does not exist. It is created automatically during execution.

Application startup script

A Shell script that starts the application.

Application shutdown script

The shell script to stop the application.
This script must correctly stop both the current and previous application versions. It must also exit gracefully if the application is not running.

For example, the following script stops a container named my-container. This script assumes all container versions share the same name and does not report an error if the container does not exist:

container_name="my-container"
container_id=$(docker ps -aq -f name=${container_name}) 
if [ -n "$container_id" ]; then
  docker rm -f $container_id
fi