Automatic Image Building

Last updated: 2024-03-25 10:49:50

Scenario

Tencent Container Registry (TCR) offers image building and delivery pipeline features based on Tencent Cloud CODING DevOps, meeting the needs of container users for quick configuration and application of continuous integration and continuous deployment. For more flexible and powerful continuous compilation, building, and delivery pipeline features, please consider using Container DevOps.
Both TCR Enterprise Edition and Personal Edition services support image building features, with shared source code authorization information. The image building feature supports compiling and building source code hosted on GitHub, GitLab.com, private GitLab, Gitee, Tencent Work, and CODING. For users hosting code on CODING.net, it is recommended to configure image building directly within the CODING DevOps product using its "Continuous Integration" feature, which currently includes built-in templates for "Build Image and Push to TCR Enterprise/Personal Edition." TCR supports four types of trigger mechanisms, such as triggering image building by pushing to a specified branch. It allows custom configuration of Dockerfile paths and build directories. Flexible image version naming rules can be configured, such as custom prefix-branch-build time-commit number, to synchronize image version management with code updates.

Preparations

Before configuring automatic image building, make sure that the following conditions are met:
You have purchased an Enterprise Edition instance or initialized an Individual Edition instance.
You have activated the CODING DevOps service and completed the authorization process. For more information, please refer to Activating the Service.
If you are using a sub-account, please refer to Enterprise Edition Authorization Scheme Example or Individual Edition Authorization Scheme Example to grant the sub-account the necessary operation permissions for the corresponding instance.

Instructions

Create an image repository

Refer to Create Image Repository to complete the creation of the image repository, selecting "Platform-built Image" as the "Image Source."

Creating Image Building Rules

1. After successfully creating an image repository, the page will automatically redirect to the Building images tab of the repository, as shown in the figure below:


2. Click "Create image building rules". In the "Create image building rules" pop-up window, refer to the following information to configure automatic image building and pushing for this image repository, as shown in the figure below:


Code source: The hosting platform for the source code used in image building. Currently supported platforms include GITHUB, GITLAB, private GITLAB, GITEE, TGit, and CODING. It is recommended for CODING users to directly access the image building module within the CODING DevOps platform and use the built-in Build Image and Push to TCR Enterprise Edition or Build Image and Push to TCR Personal Edition templates to configure image building. The image building service needs to pull the corresponding source code, so please authorize the specified code source for the first-time use. After authorization, you can normally list code repositories, branches, and other information. If you encounter any issues with source code authorization, please submit a ticket for consultation.
Code repository: After specifying the code source and completing its authorization, you can select a code repository under the source code hosting platform. When the image building service is triggered, it will pull the source code from this repository for compilation and building.
Trigger rule: The rule conditions for automatically triggering image building. Currently, the following four scenarios are supported:
Push to Specified Branch Trigger: A specific branch must be designated.
Trigger build when pushing a new tag: Triggered when creating and pushing a new tag.
Trigger build on branch push: Triggered when pushing to any branch, without specifying a particular branch.
Build when the branch or tag rule is matched: You need to enter a regular expression, such as ^refs/heads/master$, which can match the master branch to trigger the build.
Dockerfile path: The image building operation is based on the Dockerfile within the code repository, and the path to this Dockerfile file must be specified. If not specified, the default is a file named Dockerfile in the root directory of the code repository.
Building directory: The working directory for image building, also known as the context environment (context), which defaults to the root directory of the code repository.
Tag rule: Define the name of the image generated by the image build, i.e., the image tag. Custom prefixes can be configured, combined with the three environment variables: "Branch/Tag," "Update Time," and "Commit Number." The update time is the system time of the build service when executing the docker tag command.
Building parameters: Configure the --build-arg in docker build, ensuring compatibility with the Dockerfile.
3. Click Confirm to create the image build rule.
If the above configuration parameters do not meet your requirements, please go directly to the CODING DevOps platform and use the Continuous Build feature.

Managing Image Building Rules

After completing the creation of the image building rules, you can view the build configuration and build logs under the Image Building tab of the repository. You can perform the following management operations on the current build rules:
Trigger Build Now Manually trigger image building within the console, specifying a branch or code version.
Edit You can edit the current build rule, and all parameters that can be configured during the creation process can be re-edited.
Delete Remove the current build rule.
Automatic Trigger You can enable or disable the automatic trigger for this rule. When the automatic trigger is disabled, pushing code that meets the trigger criteria will not initiate image building, but you can still use the "Build Now" feature for manual triggering.

Triggering image building and viewing build logs

After configuring the image building rules, an automatic build and image push will be triggered when there is a source code operation that meets the trigger rules. This article takes pushing new code updates to a specified branch in a GitHub repository as an example. For existing build logs, you can view the execution details or delete the log record.

Troubleshooting

If you encounter any of the following issues while using the image building feature, please refer to the corresponding methods to retry. If the issue persists, submit a ticket for assistance.

Source Code Authorization Failed

You can click "Authorize Source Code" again in the "Create Image Build Rule" window, or go to the corresponding source code hosting platform to view third-party authorization, delete "CODING DevOps," and try re-authorizing. For example, on GitHub, the third-party authorization location is: Settings > Applications > Authorized OAuth Apps.

Image Building Failure

You can view the execution details of the build task in the build log and make adjustments based on the error information in the execution details. For example, you can reconfigure the parameters in the image build configuration or modify the Dockerfile.