Trigger automatic CD for SAE with EventBridge
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
Container Registry (ACR)
You have created an Enterprise Edition instance. For more information, see Create an Enterprise Edition instance.
You have prepared a Dockerfile to build an image. In this topic, the Dockerfile is placed in the root directory of the project. For more information, see Build an image by using an Enterprise Edition instance.
You have linked a source code hosting platform. This topic uses a Gitee code platform as an example. For more information, see Associate an Enterprise Edition instance with a Gitee account.
You have created a namespace. For more information, see Create a namespace.
Serverless App Engine (SAE)
You have activated Serverless App Engine (SAE). For more information, see Activate SAE.
EventBridge
You have activated EventBridge. For more information, see Activate EventBridge and grant permissions.
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.
Step 1: Build an image with an ACR Enterprise Edition instance
Log on to the ACR console.
In the navigation pane on the left, click Instance List. In the top navigation bar, select a region.
On the Instance List page, click the target Enterprise Edition instance.
In the navigation pane on the left of the instance management page, choose . Then, click Create Repository.
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.
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.
On the Image Repositories page, find the repository that you created and click Manage in the Actions column.
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-v1branch and automatically builds an image with thev1tag. Manual builds are not supported in this case. For more information, see Named capturing groups in regular expressions.NoteAfter 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
masterand the Dockerfile is in themasterbranch, the build context directory is/.Dockerfile filename
The name of the Dockerfile. The default value is Dockerfile.
In the Image version step, set the parameters, click Save, then click Next.
NoteClick 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,
20201015and202010151613.NoteThis 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.
NoteThis parameter is optional. If selected, builds can only be triggered automatically, not manually.
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, andlinux/arm/v6.linux/amd64is selected by default.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
masterbranch of your code repository to trigger the build rule.
NoteOn 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
-
Log on to the SAE console.
-
In the left-side navigation pane, click Applications. In the top navigation bar, select a region. Then, click Create Application.
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.
NoteThe 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.
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.
NoteTo deploy an application by using an image from Container Registry Enterprise Edition, you must first configure VPC access control for the target instance.
-
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
Log on to the EventBridge console.
In the navigation pane on the left, click Event Buses. In the top navigation bar, select a region.
On the Event Buses page, go to the Custom Event Buses tab and click Quick Create.
In the Create custom event bus panel, configure the parameters.
In the Bus step, enter a Custom event bus name and Description, then click Next.
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.
In the Rule step, enter a Rule name and Description, enter an event pattern in the Pattern content code block, then click Next.
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.
Step 4: Create an ACR event rule
Log on to the ACR console.
In the navigation pane on the left, click Instance List. In the top navigation bar, select a region.
On the Instance List page, click the target Enterprise Edition instance.
In the navigation pane on the left of the instance details page, choose .
On the Event Rules tab, click Create Rule.
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.
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.