All Products
Search
Document Center

Apsara Devops:Build a Java application deliverable and publish it to ECS by using the blue-green release policy

Last Updated:Sep 15, 2023

Background

You can use this topic to assist your process automation under the following conditions:

  • Code development in Java

  • Delivery in the JAR or WAR format and execution on ECS or a self-managed host

  • Publication by using the blue-green release policy

Customer requirements

Customers use host deployment in the following typical scenarios:

  • Perform some quality checks on the source code such as unit testing and code scanning.

  • Build source code into a deliverable such as a JAR or WAR file.

  • Publish a deliverable to an ECS virtual host by using a blue-green release policy

Apsara DevOps operations

The following example demonstrates how to build an application deliverable and publish it to ECS by using the blue-green release policy. A Java Spring Boot repository is used in the example.

Create a pipeline

  1. Log on to Apsara DevOps Flow (https://flow.alibabacloud.com/).

  1. Click Create Pipeline.

image 1
  1. Select the Java ยท Test, build Image, deploy to ECS/VM in Blue-Green template and click Create.

image b

Configure a code repository

  1. Add a code source. Select General Git. Set Code Repository to https://github.com/yunxiao-devops/spring-boot-example.git and Default Branch to master. You can enter any personal certificate, because a public code repository is used in the example.

image 2

Configure a build & archive task

  1. Modify the Java Build & Archive task and add the Archive Path field. Set the value to deploy.sh.The deploy.sh file exists in the code repository and contains the script to boot applications on ECS. For subsequent host deployment, you must add the deploy.sh file to the package file. How to use the deploy.sh file will be described in the host deployment part. Here you specify two paths: target/and deploy.sh. Flow packages and archives the two files (or folders) into an artifact. The artifact will also be used in the subsequent host deployment task.

image 3

Configure a host group deployment task

  1. Configure the host deployment task. Select Artifact Name.default from the drop-down list. The artifact is the artifact archived in the Java build & archive task.To configure a host group, you must click Create Deployment Group to create a host group first.

image1
  1. Click Create Service Authorization. You are redirected to RAM to complete RAM authorization. Then, you are automatically returned to Flow.Set Adding Method to Add Tags. Ensure that your ECS instances are marked with different tags. In this example, two host groups are marked with the ecs group:green and ecs group:blue tags.Select the region and the host group marked with the ecs group:green tag. Click Next. Enter the host group name and click Save. The host group is created. The newly created host group is automatically added to the pipeline configurations.

image2image3image4image5

  1. Configure the deployment script.

  • Download Path: indicates where you store the compressed package created in the build & archive task. The value in this example is /home/admin/app/package.tgz.

  • Execute User: indicates which role executes the script? The value in this example is root.

  • Deployment Script: the content of the script. The value in this example:

mkdir -p /home/admin/application/ 
tar zxvf /home/admin/app/package.tgz -C /home/admin/application/ 
sh /home/admin/application/deploy.sh restart
image6
  1. Configure a deployment policy

  • Suspend Strategy: indicates what suspension method is used in the host group, such as Suspend First Batch, Suspend Per Batch, or Without Suspension. We recommend that you select Suspend First Batch. After the first batch is published, check the online service. If no exceptions occur, you can continue publishing the remaining batches.

  • Batch Quantity: indicates the number of batches you want to divide the machines in the host group.For example, you divide a total of four machines into two batches. Each batch contains two machines.

Configure a manual approval task

  1. Click Manual Approval and set Validator.

image7

Configure an ECS tag swap task

  1. Click Swap Tags on ECS. Set Service Authorization, Region, Tag, Original Value, and Swapped To. In this example, Original Value is set to ecs group:green and Swapped To is set to ecs group:blue.Different tags on the hosts will be swapped after each publication is approved.

image8

Run a pipeline

  1. After the configurations are complete, click Save and Run to trigger the pipeline.

image9image10

  1. Then, you can view the details of the pipeline, including code scan results, build logs, and deployment logs.

image11
  1. After the application is published on the host group, the pipeline is waiting for manual approval. You can verify the application.After you approve the publication, the ecs group:green and ecs group:blue tags are swapped on hosts to ensure the application will be published to different hosts next time.

image12