Help Center/ CodeArts Build/ Best Practices/ Building with Maven, Uploading the Software Package, and Pushing the Image to SWR (Built-in Executors, Code)
Updated on 2024-11-21 GMT+08:00

Building with Maven, Uploading the Software Package, and Pushing the Image to SWR (Built-in Executors, Code)

Scenario

CodeArts Build allows you to define your build as code using YAML. Your configurations, such as build environments, parameters, commands, and actions, reside in a YAML file (named build.yml in this practice). After creating this file, add it along with the source code to a code repository. The file will be used as a script by the system to run a build, making the process traceable, recoverable, secure, and reliable. In this practice, you build with Maven, upload the resulting software package to the release repo, and push the image to SWR.

These steps depend on the following services:

Requirements

  • You have created an organization named codeci_gray in SWR.
  • You have permissions for CodeArts Artifact.
  • You have permissions for CodeArts Repo.
  • You can only use the code hosted in CodeArts Repo for YAML builds.

Procedure

Table 1 Steps

Step

Description

Creating a Project

Create a project.

Creating a CodeArts Repo Repository

Create a CodeArts Repo repository to store code files.

Creating a build.yml File

Define the entire build process in build.yml.

Creating a Dockerfile

Customize an image by modifying the Dockerfile.

Creating a Build Task

Create a build task.

Viewing and Verifying the Build Results

View and verify the build results.

Creating a Project

  1. Log in to the Huawei Cloud console with your Huawei Cloud account.
  2. Click in the upper left corner and choose Developer Services > CodeArts from the service list.
  3. Click Access Service. The homepage of CodeArts is displayed.
  4. Click Create Project, and select the Scrum template.
  5. Set the project name to build-bestpractice, and leave the other parameters as default.
  6. Click OK to access the project.

Creating a CodeArts Repo Repository

  1. In the navigation pane, choose Code > Repo.
  2. On the displayed page, click New Repository. Select Template, and click Next.
  3. On the template selection page, select the Java Maven Demo template and click Next.
  4. On the repository creation page, type maven_yml_build in the Repository Name field, and leave the other parameters as default.
  5. Click OK. The repository details page is displayed.

Creating a build.yml File

  1. On the code repository details page, click Create and select Create Directory from the drop-down list, as shown in Figure 1.

    Figure 1 Creating a directory

  2. On the Create Directory page, set parameters based on Table 2 and click OK.

    Table 2 Creating a directory

    Parameter

    Description

    Directory Name

    Enter a directory name, for example, .cloudbuild. Use 1 to 100 characters, including letters, digits, slashes (/), underscores (_), hyphens (-), and periods (.).

    Commit Message

    Describe the files within the directory. Use 1 to 2,000 characters.

  3. Click the name of the directory created in 2.
  4. Click Create and select Create File from the drop-down list, as shown in Figure 2.

    Figure 2 Creating a file

  5. Name the file build.yml and copy the following code to the file:

    # The YAML provided is a default template that can be edited as needed.
    ---
    version: 2.0
    steps:
      BUILD:
      - maven:
          image: cloudbuild@maven3.5.3-jdk8-open # The image path can be customized.
          inputs:
            settings:
              public_repos:
                - https://mirrors.huawei.com/maven
            cache: true # Determine whether to enable caching.
            command: mvn package -Dmaven.test.failure.ignore=true -U -e -X -B
      - upload_artifact:
          inputs:
            path: "**/target/*.?ar"
      - build_image:
          inputs:
            organization: codeci_gray # Organization name
            image_name: maven_demo # Image name
            image_tag: 1.0 # Image tag
            dockerfile_path: ./Dockerfile

  6. Click Submit.

Creating a Dockerfile

  1. In the root directory, create a file named Dockerfile by following 4. The code in the file is as follows:

    FROM swr.regionID.myhuaweicloud.com/codeci/special_base_image:centos7-base-1.0.2
    MAINTAINER <devcloud@demo.com> 
    USER root 
    RUN mkdir /demo 
    COPY ./target/server-1.0.jar /demo/app.jar

    server-1.0.jar combines the values of artifactId, packaging, and version in the pom.xml file.

  2. Click Submit.

Creating a Build Task

  1. In the navigation pane, choose CICD > Build.
  2. Click Create Task. On the displayed Basic Information page, set parameters according to Table 3.

    Table 3 Basic information

    Parameter

    Description

    Name

    Assign a custom name to the build task, for example, maven_yml_build.

    Code Source

    Select Repo.

    Repository

    Select Repo01, the code repository created in Creating a CodeArts Repo Repository.

    Default Branch

    Keep the default value master.

    Description

    Enter additional information to describe the build task.

  3. Click Next. On the displayed page, select Blank Template. Click OK. The Build Actions page is displayed.
  4. Click the Code tab. Then you can view the imported build script, as shown in Figure 3.

    Figure 3 Code tab

  5. Click Save and Run in the upper right corner.

Viewing and Verifying the Build Results

  • Checking the uploaded the software package
    1. In the navigation pane, choose Artifact > Release Repos.
    2. On the displayed page, find the folder that shares the same name as the build task (the name you specify when creating the build task), as shown in Figure 4. The software package can be found within this folder.
      Figure 4 Checking the software package
  • Checking the pushed image
    1. Go to the SWR console.
    2. In the navigation pane, choose My Images. In the search box, select Organization as the filter field, and type the organization name (codeci_gray is used in this example) you configure when creating a build.yml file.
    3. In the filtered results, click the image name (maven_demo is used in this example) you configure when creating a build.yml file.