Container Registry automates the building and delivery of images from source code repositories to Container Registry repositories based on Dockerfiles. When the source code is updated, Container Registry automatically builds an image by using a Dockerfile and uploads the image to a Container Registry repository. This topic describes how to configure image build rules.

Prerequisites

A repository is created and images are built. For more information, see Create an image repository.

Background information

For more information about Dockerfiles, see Dockerfile.

Configure a build rule

  1. Log on to the Container Registry console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, click Instances.
  4. On the Instances page, click the required Container Registry Enterprise Edition instance.
  5. On the management page of the Container Registry Enterprise Edition instance, choose Repositories > Repositories in the left-side navigation pane.
  6. On the Repositories page, find the created image repository and click Manage in the Actions column.
  7. In the left-side navigation pane, click Build. In the Build Rules section, click Add Build Rule. In the Add Build Rule dialog box, configure the build rule and click Confirm.
    Parameter Description
    Type Set the type of the source code repository. Valid values: Branch and Tag.
    Branch/Tag Select or enter a branch or a tag. Regular expressions are supported. If you specify the release-(?<imageTag>\w*) regular expression, the system automatically builds an image of V1 when the source code under the release-v1 branch is updated. The image cannot be built immediately. For more information about how to specify regular expressions, see Use regular expressions in named capturing groups.
    Note After you specify regular expressions, images can be built only by the system. You cannot manually build images.
    Dockerfile Directory Set the directory where the Dockerfile resides. You must specify a relative directory. The parent directory is the root directory of the code branch.
    Dockerfile Filename Set the name of the Dockerfile. Default value: Dockerfile.
    Image Tag Set the tag of the image, for example, latest.
    • Image Tag: the tag of the image, for example, latest. You can enable named capturing groups. For example, you can use the captured content if you specify a named capturing group for Branch/Tag.
    • Build Time: the time when source code is pushed in UTC+8 format, for example, 20201015 or 202010151613.
      Note This parameter is optional. If you set this parameter, images can be built only by the system. You cannot manually build images.
    • Commit ID: the number of characters to be obtained from the commit ID of the most recently pushed code. By default, the first six characters are used. You can drag the slider to change the number of characters.
      Note This parameter is optional. If you set this parameter, images can be built only by the system. You cannot manually build images.

Example 1: Create a build rule based on the main branch (you can manually build images by using this build rule)

Configure the build rule based on the following settings:
  • Type: Branch
  • Branch/Tag: main
  • Dockerfile Directory: /
  • Dockerfile Filename: Dockerfile
  • Image Tag: latest
When you click Build or the source code in the main branch is updated, the system builds an image. The file named Dockerfile in the / directory of the main branch is used to build the image. The tag of the created image is latest. The following figure shows the configuration of the build rule. 构建1

Example 2: Create a build rule based on the branch that matches a regular expression (you cannot manually build images by using this build rule)

Configure the build rule based on the following settings:
  • Type: Branch
  • Branch/Tag: release-(?<imageTag>\w*)
  • Dockerfile Directory: /
  • Dockerfile Filename: Dockerfile
  • Image Tag: ${imageTag}
  • Build Time: yyyyMMddHHmm
  • Commit ID: 30

If the source code is updated in the branch that starts with release-, the system automatically builds an image. The file named Dockerfile in the / directory of the branch is used to build the image.

For example, if the source code in the release-v1 branch is updated, the release-(?<imageTag>\w*) regular expression captures v1 from the name of the branch. The value v1 is passed to the image tag variable. This variable is used in the image tag. In this example, the tag of the created image is v1-202010151625-d4ef3dc3b77a011a5779eec7efdd45. The following figure shows the configuration of the build rule. 构建2