Build a container DevOps pipeline using triggers

更新时间:
复制 MD 格式

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

Step 1: Create an image build rule

Important

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.

  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. In the left-side navigation pane, choose Repository > Namespace .

  5. On the Repositories page, find the image repository you created and click Manage in the Actions column.

  6. In the left-side navigation pane, click Build . In the Build Rules section, click Add Build Rule .

  7. 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.

    Note

    If 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

  8. In the Tag step, configure the following parameters, click Save , and then click Next .

    Note

    Click 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

  9. In the Build Configurations step, configure the following parameters and then click Next .

    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

Important

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.

  1. Log on to the ACK console. In the left navigation pane, click Clusters.

  2. On the Clusters page, click the name of your cluster. In the left navigation pane, click Workloads > Deployments.

  3. On the Stateless page, find the target application and click Details in the Actions column.

  4. On the application details page, click the Triggers tab and then click Create Trigger.

  5. In the Create Trigger dialog box, set Trigger Action to Redeploy and click OK.

    Note

    Currently, Trigger Action only supports the redeploy action.

    After the trigger is created, a trigger URL is displayed in the Triggers section. Copy the URL.

    Trigger connector

Step 3: Attach the repository trigger

  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. In the left-side navigation pane, choose Repository > Namespace .

  5. On the Repositories page, find the image repository you created and click Manage in the Actions column.

  6. In the left-side navigation pane, click Build . In the Build Rules section, click Add Build Rule .

  7. 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.

  1. In the left-side navigation pane, click Build . In the Build Rules section, click Add Build Rule .

  2. 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.