Help Center/ CodeArts Build/ Best Practices/ Code-based Build/ Uploading Software Packages with CMake
Updated on 2023-11-28 GMT+08:00

Uploading Software Packages with CMake

Background

CodeArts Build allows you to configure build scripts using YAML files. You can use YAML syntax to compile the build environment, parameters, commands, and steps into a build.yml file, which can be stored in a code repository with the built code. The system uses the build.yml file as the build script to execute the build task, making the build process traceable, recoverable, secure, and reliable. The build with CMake is used as an example in this section.

Prerequisites

A project is available. If no project is available, create one.

Creating a Code Repository

  1. Log in to CodeArts using the Huawei Cloud account.
  2. Click the name of the project to create a repository for it.
  3. In the navigation pane, choose Code > Repo, as shown in Figure 1.

    Figure 1 CodeArts Repo

  4. Click New Repository.
  5. Set parameters based on Table 1 and click OK.

    Table 1 Creating a code repository

    Parameter

    Description

    Repository Name

    Customize the name for the code repository, for example, CppDemo_build.

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

    Description

    Describe the code repository.

    .gitignore Programming Language

    Select .gitignore based on the programming language, for example, Java.

    Permissions

    Select all.

    • Make all project developers automatic repository members: A project manager is automatically set as the repository administrator, and a developer is set as a common repository member. When the two roles are added to the project, they will be automatically synchronized to existing repositories.
    • Generate README: You can edit the README file to record information such as the project architecture and compilation purpose, which is similar to a comment on the entire repository.
    • Automatically create Check task (free of charge): After the repository is created, you can view the code check task of the repository in the check task list.

    Visibility

    Set this parameter to Private.

    • Private: Only repository members can access and commit code.
    • Public: The repository is open and read-only to all guests, but is not displayed in their repository list or search results. You can select an open-source license as the remarks.

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 Code Repository).
  3. Choose Create > Create Directory, as shown in Figure 2.

    Figure 2 Creating a directory

  4. Set parameters based on Table 2 and click OK.

    Table 2 Creating a directory

    Parameter

    Description

    Directory Name

    You can customize the value, for example, .cloudbuild.

    Commit Message

    Enter remarks of the directory. The message is used to record the description of the files in the folder.

  5. Click the name of the directory created in Step 4.
  6. Choose Create > Create File, as shown in Figure 3.

    Figure 3 Creating a file

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

    # This YAML is the default template and can be modified based on this 
    ---
    version: 2.0
    steps:
      PRE_BUILD:
      - checkout:
          name: "checkout"
          inputs:
            scm: "codehub"
            url: "git@codehub.devcloud.cn-north-4.huaweicloud.com:cszl00001/cppDemo.git"
            branch: "master"
            lfs: false
            submodule: false
      BUILD:
      - cmake:
          name: "CMake Build"
          inputs:
            command: |
                # Create the build directory and switch to the build directory.
                mkdir build && cd build 
                # Generate makefiles for the Unix platform and perform the build.
                cmake -G 'Unix Makefiles' ../ && make -j
      - upload_artifact:
          inputs:
            path: "build/*"
            version: 2.1
            name: packageName

  8. Click OK.

Creating a CMakeLists.txt File

  1. Create a file named CMakeLists.txt in the root directory by referring to Step 6 and Step 7. The code in the file is as follows:

    cmake_minimum_required (VERSION 2.5)
    
    project (HolleWorld)
    AUX_SOURCE_DIRECTORY(. DIR_SRCS)
    
    add_executable(bin ${DIR_SRCS})

  2. Click OK.

Creating a helloworld.cpp File

  1. Create a file named helloworld.cpp in the root directory by referring to Step 6 and Step 7. The code in the file is as follows:

    #include <iostream>
    int main()
    {
        std::cout << "Hello World !"  << std::endl;
        return 0;
    }

  2. Click OK.

Creating a Build Task

  1. In the navigation pane, choose CICD > Build, as shown in Figure 4.

    Figure 4 Accessing the CodeArts Build homepage

  2. Click Create Task.
  3. Set parameters based on Table 3.

    Table 3 Basic information

    Parameter

    Description

    Task Name

    Enter a custom task name, for example, CppDemo_build.

    Code Source

    Select Repo.

    Source Code Repository

    Select the code repository you created (see Creating a Code Repository).

    Branch

    Select the branch created when you create the repository in Creating a Code Repository. If no branch is available, select the default master.

    Description

    Describe the build task.

  4. Click Next.
  5. Select Blank Template and click Next.
  6. Click the Code tab to view the imported build script, as shown in Figure 5.

    Figure 5 Code tab

  7. Click Create and Run in the upper right corner.

Viewing and Verifying the Build Result

  1. In the navigation pane, choose Artifact > Release Repos.
  2. Go to the release repos to view the released software package. The software package name is the same as the task name when you create a build task.