Updated on 2024-11-21 GMT+08:00

Customizing a Build Environment

CodeArts Build provides a large number of build tools. If necessary dependency packages and tools are missing, you can create a custom image from a Dockerfile and push the image to SWR. For details about how to use the pushed image, see Using a Custom Build Environment.

This section uses a Maven build as an example to describe how to customize an environment by modifying the Dockerfile.

Preparations

  • You have created an organization in SWR. For details about organization restrictions, see notes and constraints of SWR.
  • If you want to push the created image to SWR of other Huawei Cloud users, perform the following operations.
    1. Access the CodeArts Build Homepage from the project list.
    2. In the navigation pane, choose Settings > General > Service Endpoints.
    3. Select IAM user from the Create Endpoint drop-down list box. In the displayed dialog box, enter the following information and click Confirm.
      • Service Endpoint Name: Assign a custom name to the endpoint. Enter a maximum of 256 characters, including letters, digits, hyphens (-), underscores (_), periods (.), and spaces.
      • Access key ID (AK) and secret access key (SK) are used like passwords to authenticate users who make API requests.

        On the CodeArts Build homepage, click Console, hover the cursor on the username in the upper right corner, and choose My Credentials from the drop-down list. In the navigation pane on the left, choose Access Keys to create a user key.

  • If you want to push the created image to other image repositories, perform the following operations.
    1. Access the CodeArts Build Homepage from the project list.
    2. In the navigation pane, choose Settings > General > Service Endpoints.
    3. Select Docker repository from the Create Endpoint drop-down list box. In the displayed dialog box, enter the following information and click Confirm.
      • Service Endpoint Name: Assign a custom name to the endpoint. Enter a maximum of 256 characters, including letters, digits, hyphens (-), underscores (_), periods (.), and spaces.
      • Repository Address: Enter the address of the target image repository.
      • Username: Enter the username for logging in to the repository.
      • Password: Enter the password used for logging in to the repository.

Customizing the Dockerfile

  1. Access the CodeArts Build Homepage from the project list.
  2. In the upper right corner of the CodeArts Build homepage, click More and select Custom Build Environments from the drop-down list.
  3. On the Custom Build Environments page, click a base image to download the Dockerfile template.
    Figure 1 Dockerfile templates
  4. Edit the downloaded Dockerfile.
    You can add other dependencies and tools required by the project to customize the Dockerfile. The following figure shows an example of adding JDK and Maven tools.
    RUN yum install -y java-1.8.0-openjdk.x86_64
    RUN yum install -y maven
    RUN echo 'hello world!'
    RUN yum clean all
  5. In the navigation pane, choose Code > Repo. Click the name of the code repository to enter its details page.
  6. On the Code tab page, choose Create > Upload File to upload the Dockerfile and all files required for image creation to the root directory of the code repository.

Building an Image and Pushing It to SWR

  • Build on GUI

    Add Build Image and Push to SWR after Build with Maven, when configuring build actions.

    Retain the default values for the Build with Maven action. If the current parameter settings do not meet your requirements, modify the parameter settings by referring to Building with Maven. For details about the parameters for the Build Image and Push to SWR action, see Table 1.

    Table 1 Parameters for creating an image and pushing it to SWR

    Parameter

    Description

    Action Name

    Assign a custom name to the build action. The name can contain:

    • Letters, digits, hyphens (-), underscores (_), commas (,), semicolons (;), colons (:), periods (.), slashes (/), and parentheses.
    • 1 to 128 characters.

    Tool Version

    Select the Docker version, or use the default one.

    Currently, CodeArts Build supports Docker 18.03 and Docker 20.10.

    Image Repository

    Select the target image repository. You can push the image to Huawei Cloud SWR and other image repositories.

    Authorized User

    Specify the user to which the target image repository belongs. You can push the image to the current user or other user's image repository.

    Ensure that you have permissions to edit or manage all images in the organization. For details, see User Permissions.

    IAM Account

    Expand the drop-down list and select the service endpoint created in Preparations for the specific IAM account. Then, use the service endpoint to push the files to the user's SWR.

    This parameter is mandatory when Authorized User is set to Other.

    Push Region

    Select the target region of your push. The built image will be pushed to the SWR repository in this region.

    Docker Repository Endpoint

    Select the Docker repository service endpoint created in Preparations and push the image to the corresponding repository through the service endpoint.

    Organization

    Select the organization created in Preparations from the drop-down list box. The image will be placed in this organization after being pushed to SWR.

    Image Name

    Enter the name of the created image.

    The value must start with a digit or letter and can contain 1 to 255 characters, including only lowercase letters, digits, underscores (_), and hyphens (-).

    Image Tag

    Specify the image tag, which can be customized. You can use Image name:Tag to uniquely specify an image.

    The value can contain 1 to 128 characters, including only letters, digits, periods (.), underscores (_), and hyphens (-). It cannot start with periods or hyphens.

    Working Directory

    Optional.

    The context path parameter in the docker build command is the relative path of the root directory of the repository.

    When Docker builds an image, the docker build command packs all content under the context path and sends it to the container engine to help build the image.

    Dockerfile Path

    Optional.

    Path of the Dockerfile. Set this parameter to a path relative to the working directory. For example, if the working directory is the root directory and the Dockerfile is in the root directory, set this parameter to ./Dockerfile.

    Add Build Metadata to Image

    Specify whether to add the build information to the image. After the image is created, run the docker inspect command to view the image metadata.

    Continue After Failure

    Specify whether to proceed after the current action fails by setting the parameter to either Yes or No.

  • Build with Code

    Modify the code in the BUILD block in Creating a YAML File for Your Code-based Build by referring to the following sample code:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    version: 2.0 # The value must be 2.0.
    steps:
      BUILD:
        - build_image:
            name: buildImage
            inputs:
              regions: ["x-x-x", "x-x-xxx"]
              organization: codeci_test
              image_name: demo
              image_tag: ${GIT_COMMIT}
              dockerfile_path: dockerfile/Dockerfile
              # set_meta_data: true
              ignore_fail: true
    
    Table 2 Parameters in the sample code for creating an image and pushing it to SWR

    Parameter

    Type

    Description

    regions

    List

    Optional.

    Select the region of SWR where the image is to be uploaded to. By default, the image is uploaded to SWR in the region where the current task is located. If multiple regions are configured, the built image will be pushed to SWR in each region in sequence after the image is created.

    organization

    String

    Enter the name of the organization to which the image belongs after being pushed to SWR. The organization name is the name of the organization created in Preparations.

    image_name

    String

    Optional.

    Enter the name of the created image.

    The value must start with a digit or letter and can contain 1 to 255 characters, including only lowercase letters, digits, underscores (_), and hyphens (-).

    The default value is demo.

    image_tag

    String

    Optional.

    Specify the image tag, which can be customized. You can use Image name:Tag to uniquely specify an image.

    The value can contain 1 to 128 characters, including only letters, digits, periods (.), underscores (_), and hyphens (-). It cannot start with periods or hyphens.

    The default value is v1.1.

    context_path

    String

    Optional.

    The context path parameter in the docker build command is the relative path of the root directory of the repository.

    When Docker builds an image, the docker build command packs all content under the context path and sends it to the container engine to help build the image.

    The default value is ..

    dockerfile_path

    String

    Optional.

    Path of the Dockerfile. Set this parameter to a path relative to the working directory. For example, if the working directory is the root directory and the Dockerfile is in the root directory, set this parameter to ./Dockerfile.

    The default value is ./Dockerfile.

    set_meta_data

    Bool

    Optional.

    Specify whether to add the build information to the image. After the image is created, run the docker inspect command to view the image metadata.

    • true: Add the build information to the image.
    • false: Do not add the build information to the image.

    The default value is false.

    ignore_fail

    String

    Whether to proceed after the current action fails.
    • true: Yes
    • Empty: No