Trigger automatic CD for SAE with EventBridge

Updated at:

Serverless App Engine supports using EventBridge to trigger automatic Continuous Deployment (CD) for SAE. This topic describes how to configure EventBridge to listen for specific events and integrate with ACR to create a seamless workflow from image updates to automatic application deployments. This workflow is triggered when a new image version is built and ensures the real-time performance and efficiency of your system.

Prerequisites

Limitations

This feature is available only for microservices applications.

Background information

This event-driven workflow uses EventBridge to listen for a notification from ACR that an image build is complete. EventBridge then triggers an automatic CD process in SAE to deploy the application with the new image version. This method allows for efficient application testing and development.

image

Step 1: Build an image with an ACR Enterprise Edition instance

  1. Log on to the ACR console.

  2. In the navigation pane on the left, click Instance List. In the top navigation bar, select a region.

  3. On the Instance List page, click the target Enterprise Edition instance.

  4. In the navigation pane on the left of the instance management page, choose Repositories > Image Repositories. Then, click Create Repository.

  5. In the Repository Information step, set Namespace, Repository Name, Repository Type, Image version, Summary, and Description. Then, click Next.

    In this example, set Namespace to sae-cicd, Repository Name to cicd-demo, Repository Type to Private, and Summary to Application image repository.

  6. In the Code Source step, select Gitee for Code source, select the target repository and repository name from the drop-down lists, select Automatically build images upon code changes for Build settings, then click Create Repository.

  7. On the Image Repositories page, find the repository that you created and click Manage in the Actions column.

  8. In the navigation pane on the left, click Build. In the Build rule settings section, click Add Rule. In the Build Information step, configure the parameters and click Next.

    Parameter

    Description

    Type

    The type of the source code repository. Valid values: Branch and Tag.

    Branch/Tag

    Select or enter a branch or tag. Regular expressions are supported. If the regular expression is release-(?<imageTag>\w*), the system detects source code changes in the release-v1 branch and automatically builds an image with the v1 tag. Manual builds are not supported in this case. For more information, see Named capturing groups in regular expressions.

    Note

    After you configure a regular expression, builds can only be triggered automatically, not manually.

    Build context directory

    The directory where the Dockerfile is located. This is a relative path. The parent directory is the root directory of the branch or tag. For example, if your branch is master and the Dockerfile is in the master branch, the build context directory is /.

    Dockerfile filename

    The name of the Dockerfile. The default value is Dockerfile.

  9. In the Image version step, set the parameters, click Save, then click Next.

    Note

    Click Add to add another image tag. You can add a maximum of three image tags.

    Parameter

    Description

    Image tag

    The tag of the image, such as latest. You can use the content captured from the Branch/Tag parameter.

    Time

    The UTC+8 time when the source code was pushed. Two formats are available, for example, 20201015 and 202010151613.

    Note

    This parameter is optional. If selected, builds can only be triggered automatically, not manually.

    Commit ID

    The ID of the latest code commit. By default, the first six characters are used. You can use the slider to set the number of characters.

    Note

    This parameter is optional. If selected, builds can only be triggered automatically, not manually.

  10. In the Build configuration step, set the build arguments and click OK.

    Parameter

    Description

    Build architecture

    Select the build architecture. Parallel builds are supported. This allows a single image version to have multiple architecture-specific images.

    Build arguments

    The runtime arguments for the image build. Build arguments consist of case-sensitive key-value pairs. You can set a maximum of 20 build arguments. Build arguments modify the environment variables in the Dockerfile, allowing a single Dockerfile to produce multiple image states.

    Available build architectures include linux/amd64, linux/arm64, linux/arm/v7, and linux/arm/v6. linux/amd64 is selected by default.

  11. Trigger the build rule.

    You can trigger the build rule in one of the following ways:

    • On the Build page, go to the Build rule settings section. Find the target build rule and click Build in the Actions column.

      After the build is triggered, view the record in the Build Logs area at the bottom and make sure Build Status is Success.

    • Commit code to the master branch of your code repository to trigger the build rule.

    Note
    • On the Build page, go to the Build log section. Find the target build task and click Cancel in the Actions column to cancel the build task.

    • On the Build page, go to the Build log section. Find the target build task and click Log in the Actions column to view the build log.

    In the navigation pane on the left, click Image Versions. If the new image is listed, the build was successful.

Step 2: Deploy an SAE application from an image

  1. Log on to the SAE console.

  2. In the left-side navigation pane, click Applications. In the top navigation bar, select a region. Then, click Create Application.

  3. In the Basic Information step, configure the parameters and click Next: Deployment Configurations.

    Parameter

    Description

    Application name

    Enter a name for the application.

    VPC

    Select Custom. Then, select a namespace, VPC, vSwitch, and security group for the application.

    Note

    The VPC must be the same as the one used for the ACR Enterprise Edition instance.

    Namespace

    Select a namespace from the drop-down list. Namespaces have a one-to-one mapping with VPCs. For more information about how to modify a VPC, see Create and manage a VPC.

    vSwitch

    Click Select vSwitch and configure the settings in the Select vSwitch dialog box.

    Select one to three vSwitches. Each vSwitch must be associated with at least one application instance.

    Security group

    Select a security group from the drop-down list. If no security group is available in your VPC, click Create Security Group and follow the on-screen instructions. For more information, see Create a security group.

    Number of application instances

    Select the number of application instances to create.

    vCPU

    Select the vCPU specification for the instances.

    Memory

    Select the memory specification for the instances.

    Application description

    Enter a description for the application.

  4. In the deployment configuration step, configure the deployment settings and click Next: Confirm Specifications. For more information, see Deploy a multi-language application from an image in the SAE console.

    Parameter

    Description

    Language

    Select Other Languages, which includes C++, Go, .NET, and Node.js.

    Deployment method

    Only Image is supported.

    Configure image

    Specify the image for the application deployment. Click the My Alibaba Cloud Images tab, click Container Registry Enterprise Edition, and select the target Enterprise Edition instance from the drop-down list.

    Note

    To deploy an application by using an image from Container Registry Enterprise Edition, you must first configure VPC access control for the target instance.

  5. In the Specification Confirmation step, view the details of the application and the fee for the selected specifications. Then, click Confirm.

    The Creation Completed step appears. You can click Application Details to go to the Basic Information page of the application.

Step 3: Create a custom event bus

  1. Log on to the EventBridge console.

  2. In the navigation pane on the left, click Event Buses. In the top navigation bar, select a region.

  3. On the Event Buses page, go to the Custom Event Buses tab and click Quick Create.

  4. In the Create custom event bus panel, configure the parameters.

    1. In the Bus step, enter a Custom event bus name and Description, then click Next.

    2. In the Event Source step, enter an Event Source Name and Description, select HTTP/HTTPS Trigger as the Event Provider, configure the following parameters, then click OK.

      Parameter

      Description

      Request type

      Select HTTP&HTTPS.

      Request method

      Select GET and POST.

      Security settings

      Select No configuration required.

    3. In the Rule step, enter a Rule name and Description, enter an event pattern in the Pattern content code block, then click Next.

    4. In the Target step, for Service type, select Cloud product API call and then Serverless App Engine. For API version, select 2019-05-06. Select an API, and configure the API parameters and Role. Then, click Create.

      Trigger an SAE application

      Select DeployApplication for API and configure the following API parameters. For more information about the API parameters, see DeployApplication.

      Parameter

      Type

      Location

      Required

      Example

      Description

      AppId

      String

      Query

      Yes

      7171a6ca-d1cd-4928-8642-7d5cfe69****

      The ID of the application to deploy.

      ImageUrl

      String

      Query

      No

      registry.cn-hangzhou.aliyuncs.com/sae_test/ali_sae_test:0.0.1

      The address of the image. This parameter is required when Package Type is set to Image.

      AcrInstanceId

      String

      FormData

      No

      cri-xxxxxx

      This parameter is required if the image is in an ACR Enterprise Edition instance repository.

      Required for Container Registry Enterprise Edition.

Step 4: Create an ACR event rule

  1. Log on to the ACR console.

  2. In the navigation pane on the left, click Instance List. In the top navigation bar, select a region.

  3. On the Instance List page, click the target Enterprise Edition instance.

  4. In the navigation pane on the left of the instance details page, choose Instance Management > Event Notifications.

  5. On the Event Rules tab, click Create Rule.

  6. In the Event Scope step, configure the parameters and click Next.

    Parameter

    Description

    Rule name

    Enter a name for the event rule.

    Event type

    The event type that triggers the notification. Select Image build completed from the drop-down list.

    Select Success to trigger an event notification only when an image build succeeds.

    Scope

    The scope of the event rule. Select Repository, select a namespace and an image repository, and set a regular expression to filter image versions. Event notifications are triggered for image versions in the repository that match the regular expression.

  7. In the Event Notification step, select HTTPS as the notification method, enter the EventBridge from the EventBridge event source you created in the Address field, then click Save.

Step 5: Update source code and verify

When code in the Gitee repository is modified, ACR automatically builds a new image. This triggers EventBridge to seamlessly deploy the SAE.

You can view event traces in the EventBridge console to see the processing trace of events in EventBridge. After you publish an event in the EventBridge console, if an event target does not receive the event or the received event is not as expected, you can use the event tracing feature to help you identify the source of the problem and its cause during the event delivery process.

You can view the application's change history in the SAE console to check for new deployment records.