Help Center/ CodeArts Build/ Getting Started/ Building with Maven, Uploading the Software Package, and Pushing the Image (x86, Preset Image, Code)
Updated on 2024-11-21 GMT+08:00

Building with Maven, Uploading the Software Package, and Pushing the Image (x86, Preset Image, Code)

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 section, you define your Maven build as code, build your project using the x86 server and the preset image, upload the resulting software package to the release repo, and push the image to SoftWare Repository for Container (SWR).

Prerequisites

  • You have registered with Huawei Cloud and completed real-name authentication. If you do not have a HUAWEI ID yet, follow these steps to create one:
    1. Visit Huawei Cloud official website.
    2. Click Sign Up and create your account as instructed.

      Once your account is created, the system automatically redirects you to your personal information page.

    3. Complete individual or enterprise real-name authentication. For details, see Real-Name Authentication.
  • You have enabled CodeArts Free Edition. If not, enable it by referring to Purchasing a CodeArts Package.
  • You have created an organization in SWR. If no organization is available, create one by referring to Creating an Organization.

Preparing 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 Build from the service list.
  3. Click Access Service. The homepage of CodeArts Build is displayed. On the navigation bar, click Homepage.
  4. Click Create Project, and select the Scrum template. Set the project name to Scrum01 and keep the default values for other parameters. 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 Common, and click Next.
  3. Set parameters according to Table 1 and click OK.

    Table 1 Creating a code repository

    Parameter

    Description

    Repository Name

    Assign a custom name to the code repository, for example, maven_yml_build.

    • The name starts with a digit, letter, or underscore (_).
    • The name can contain periods (.) and hyphens (-).
    • The name cannot end with .git, .atom, or a period (.).

    Description

    (Optional) Enter additional information to describe the code repository. Max. 2,000 characters.

    .gitignore Programming Language

    Select the appropriate programming language, such as Java, for the .gitignore file.

    Initial Settings

    Select all.

    • Allow project members to access the repository: Select this option to auto-assign a project manager as the repository administrator, and a developer as a common repository member. Once these roles are added to the project, they will be automatically synced with existing repositories.
    • Generate README: Select this option to create a README file where you can add details about the project's architecture and purpose, similar to a repository-wide comment.
    • Automatically create check task (free of charge): Select this option to auto-generate a code check task for the repository upon creation. The check task will appear in the check task list.

    Visibility

    Select Private to make the repository visible only to its members. These members can access the repository and commit code.

Creating a build.yml File

  1. In the navigation pane, choose Code > Repo.
  2. Click the name of the code repository you created (see Creating a CodeArts Repo Repository).
  3. Click Create and select Create Directory from the drop-down list, as shown in Figure 1.

    Figure 1 Creating a directory

  4. Set parameters according to Table 2 and click OK.

    Table 2 Creating a directory

    Parameter

    Description

    Directory Name

    Assign a custom name to the code directory, 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.

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

    Figure 2 Creating a file

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

  8. Click OK.

Creating a Java File

  1. Create a directory named src/main/java by referring to 4.
  2. Create a file named HelloWorld.java in the src/main/java directory by referring to 6 and 7. The code in the file is as follows:

    /**
     * Hello world
     *
     */
    
    public class HelloWorld {
    
      public static void main(String[] args) {
        System.out.println("Hello World!");
      }
    
    }

  3. Click OK.

Creating a Dockerfile

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

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

  2. Click OK.

Creating a pom.xml File

  1. In the root directory, create a file named pom.xml by following 6 and 7. The code in the file is as follows:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.huawei.demo</groupId>
      <artifactId>server</artifactId>
      <packaging>jar</packaging>
      <version>1.0</version>
      <name>server</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
      </dependencies>
    
    
      <build>
        <pluginManagement>
          <plugins>
            <plugin>  
              <groupId>org.apache.maven.plugins</groupId>  
              <artifactId>maven-jar-plugin</artifactId>  
              <version>2.6</version>  
              <configuration>  
                <archive>  
                  <manifest>  
                    <addClasspath>true</addClasspath>  
                  </manifest>  
                  <manifestEntries>  
                    <Main-Class>  
                      HelloWorld
                    </Main-Class>  
                  </manifestEntries>  
                </archive>  
              </configuration>  
            </plugin> 
          </plugins>
        </pluginManagement>
      </build>
    </project>

  2. Click OK.

Creating a Build Task

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

    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 section "Creating a CodeArts Repo Repository".

    Default Branch

    Select the branch created in section "Creating a CodeArts Repo Repository". Keep the default value master.

  3. Select Blank Template and 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.

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 a 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, as shown in Figure 5.
      Figure 5 Filtering images