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 Dockerfiles and upload the image to Container Registry repositories. This topic describes how to configure image build rules for Container Registry repositories.

Prerequisites

A Container Registry repository and an image are created. For more information, see Build a repository and images.

Background information

For more information about Dockerfiles, see Dockerfile.

Configure image build rules

  1. Log on to the Container Registry console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, choose Enterprise Instances > Instances.
  4. On the Instance page, click the name of the Container Registry Enterprise Edition instance or click Manage in the Actions column for the instance.
  5. On the details page of the instance, choose Repositories > Repositories in the left-side navigation pane.
  6. Click Manage in the Actions column of the created repository to go to the details page.
  7. In the left-side navigation pane, click Build. In the Build Rules section of the page that appears, click Add Build Rule and click OK.
    Parameter Description
    Type Specify 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 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 The directory where the Dockerfile is stored. You must specify a relative directory. The parent directory is the root directory of the code branch.
    Dockerfile Filename The name of the Dockerfile. The default name is Dockerfile.
    Image Tag Set the tag of the image, for example, latest.
    • Image Tag: Set the tag of the image, for example, latest. You can enable name capturing. 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 commit ID of the most recent push. By default, the first six bits are used. You can drag the slider to change the bits.
      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 an image build rule based on the main branch (you can manually build images)

Create an image 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 under the main branch is updated, the system builds an image. The file named Dockerfile in the / directory under the main branch is used to build the image. After the image is built, the version of the created image is latest. The following figure shows an example of the image build rule.

Example 2: Create an image build rule based on the branch matched by the regular expression (you cannot manually build images)

Create an image 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 under the branch that starts with release-, the system automatically builds an image. The file named Dockerfile in the / directory under the branch is used to build the image.

For example, if the source code under 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 version. In this example, the version of the created image is v1-202010151625-d4ef3dc3b77a011a5779eec7efdd45. The following figure shows an example of the image build rule.