Container Registry lets you build a container-based DevOps environment. This topic describes how to use a trigger to automatically build an image and redeploy an application in Container Service for Kubernetes (ACK) when the source code is updated.
Prerequisites
An Enterprise instance is attached to Alibaba Cloud Codeup. For more information, see Attach an Enterprise instance to Alibaba Cloud Codeup.
An image repository is created and attached to a source code repository. For more information, see Create an image repository. The build setting for the image repository is set to Automatically build images upon code change.
Step 1: Create an image build rule
To use a trigger, ensure that the image tag remains fixed.
The following steps describe how to place a Dockerfile in the master branch and ensure that the built image always uses the fixed tag `v1` when the code is updated.
Log on to the Container Registry console.
In the top navigation bar, select a region.
In the left-side navigation pane, click Instances.
-
In the left-side navigation pane, choose
Repository >Namespace . -
On the
Repositories page, find the image repository you created and clickManage in theActions column. -
In the left-side navigation pane, click
Build . In theBuild Rules section, clickAdd Build Rule . In the navigation pane on the left, click Build. In the Build Rule Settings section, click Add Rule. In the Build Information wizard, set the parameters and click Next.
Parameter
Description
Example value
Type
The type of the source code repository. Valid values: Branch and Tag.
Branch
Branch/Tag
Select or enter a branch or tag. Regular expressions are supported. For example, if the regular expression is release-(?<imageTag>\w*), the system detects source code changes in `release-v1` and automatically builds an image with the tag `v1`. Immediate builds are not supported. For more information, see Named capturing groups in regular expressions.
NoteIf you set a regular expression, only automatic builds are supported. You cannot manually trigger an immediate build.
master
Build Context Directory
The directory where the Dockerfile is located. This is a relative path. The root directory of the branch or tag is the parent directory. For example, if your branch is `master` and the Dockerfile is in the root of the master branch, the Dockerfile directory is `/`.
/
Dockerfile Name
The name of the Dockerfile. The default value is Dockerfile.
Dockerfile
In the
Tag step, configure the following parameters, clickSave , and then clickNext .NoteClick Add Configuration to add more image versions. You can add up to three image versions.
Parameter
Description
Example
Image Version
The image tag, such as `latest`. Named capturing is supported. For example, you can use the content captured in the Branch/Tag field.
v1
In the
Build Configurations step, configure the following parameters and then clickNext .Parameter
Description
Build Architecture
Select the build architecture. Parallel builds are supported. This lets you create images for multiple architectures under a single image version.
Build Arguments
Runtime parameters for the image build. Build arguments are case-sensitive key-value pairs. You can set up to 20 build arguments. These arguments modify environment variables in the Dockerfile, which allows a single Dockerfile to produce different results.
You can trigger the build rule in one of the following two ways:
On the Build page, in the Build Rule Settings section, find the target build rule and click Build Now in the Actions column.
Commit code to the master branch of the source code repository.
Step 2: Get the Container Service trigger
For stateless deployments in an ACK cluster, set the image pull policy to Always pull image (Always) (imagePullPolicy: Always). This ensures that the latest image is pulled from Container Registry for each deployment or scale-out, instead of a local image.
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
On the Stateless page, find the target application and click Details in the Actions column.
On the application details page, click the Triggers tab and then click Create Trigger.
In the Create Trigger dialog box, set Trigger Action to Redeploy and click OK.
NoteCurrently, Trigger Action only supports the redeploy action.
After the trigger is created, a trigger URL is displayed in the Triggers section. Copy the URL.

Step 3: Attach the repository trigger
Log on to the Container Registry console.
In the top navigation bar, select a region.
In the left-side navigation pane, click Instances.
-
In the left-side navigation pane, choose
Repository >Namespace . -
On the
Repositories page, find the image repository you created and clickManage in theActions column. -
In the left-side navigation pane, click
Build . In theBuild Rules section, clickAdd Build Rule . In the navigation pane on the left, click Event Notifications. On the Event Notifications page, click the Triggers tab. On the Triggers tab, click Create. Set the parameters as described in the following table and click OK.
Parameter
Description
Example
Name
You can set the trigger name.
test
Trigger URL
Enter the trigger URL that you obtained in Step 2: Get the Container Service trigger.
https://cs.console.aliyun.com/hook/trigger?token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbHVzdGVySWQiOiJjNmQxYjQxZDE3OWI2NDA2M2IyMTg2YmNhMGZkZjQ0MjIiLCJpZCIXXXXXXXXXXXXXXXXXXXXXXXXX.f80ZGxQBADnFEa7I
Trigger Method
The method to trigger the action. The following three methods are supported:
All: The trigger is fired whenever an image version is updated.
Expression: The trigger is fired only for image versions that match the specified regular expression.
Tag: The trigger is fired only for image versions that are in the specified list of tags.
Run All
Step 4: Trigger a build and view the information
When you modify and commit code to the master branch of your Codeup project, the build rule for the image repository is triggered to automatically build a new image version. The stateless deployment then automatically redeploys and pulls the latest image version.
-
In the left-side navigation pane, click
Build . In theBuild Rules section, clickAdd Build Rule . In the navigation pane on the left, click Event Notifications. On the Event Notifications page, click the Triggers tab. On the Triggers tab, find the target trigger and click Access Records in the Actions column. In the Access Records dialog box, find the target Access Record ID and click Details in the Actions column.
You can view the request time, request parameters, and the response. The request parameters contain information about the repository.