Learn to build Ci / CD process on oneos Lite (gitlab)

Time:2021-12-1

1. What does gitlab CI / CD do

The development language frequently used in the field of embedded software development isC language, in large-scale project development, we often have the requirements of format review and compilation review. If you can helpGitLabReviewing the format and compilation of the platform implementation code will greatly improve the standardization of the combined code, control the impact of the new code on the original code, and improve the overall quality of the code.

This paper will discuss how to use hand-in-hand teachingCIHelp us do code format review and compilation review.

2. Gitlab CI / CD quick build

2.1 VC code setting

  1. takeVS CodeSet to auto format on save.

Learn to build Ci / CD process on oneos Lite (gitlab)

  1. Place in the code root directory.clang-formatand.clang-format-ignoreFile.

Learn to build Ci / CD process on oneos Lite (gitlab)

.clang-formatFile is a configuration file for code formatting and format checking, which can be downloaded fromClang-FormatDownload the official website to the template and adjust it according to your own needs. See the syntax for detailslink。 Edit it.clang-formatThis file, placed in the root directory of the code, isVS CodeAfter editing the code, execute the save operation, and the saved file will automatically format the code according to the format specification of the file.

.clang-format-ignoreFile is used to configureCIFiles or folders that ignore code format checking in.

2.2 gitlab configuration

Premise: you are alreadyGitLabThere is a code warehouse, and you are the owner of the warehouseOwnerorMaintainerIdentity.

How to let gitlab CI / CD do code format review and compilation review for us? To sum up, there are five points:

  1. Warehouse root configuration.gitlab-ci.ymlDocuments;
  2. Script for configuring code review in the warehouseclang-formatFormat review tools and compiled scripts;
  3. Configure a for compilationMDKEngineering;
  4. byGitLabA public warehouse is configuredRunner(generally server);
  5. byGitLabA personal warehouse is configuredRunner(usually this machine);
  6. RunnerEnvironment configuration.

.gitlab-ci.ymlCan be understood as pipeline file, usingYAMLSyntax description,.gitlab-ci.ymlThe document describes what you want to do. This requirement includes code pulling, code format review and code compilation. hold.gitlab-ci.ymlPut it in the root directory of the remote branch every time youpushorMergeCode toGitRemote warehouse,RunnerWill be triggered automaticallyCI pipeline, do it.gitlab-ci.ymlWhat is described in the pipeline file.

RunnerWell understood, it is a machine used to run format review and compilation review of warehouse code. Generally, the public warehouse will be a server and the personal warehouse will be a local machine.

3. Specific configuration process

3.1 .gitlab-ci.ymlfile

.gitlab-ci.ymlAs follows:

Learn to build Ci / CD process on oneos Lite (gitlab)

The file describes the code update method and two methodsJob。 OneJobyescheck, conduct code format review; the other oneJobyesbuild, do code compilation.scriptIt describes the execution of commands, that is, inRunnerCommands executed in.tagsThe description isRunnerIdentification of, andRunnerThe settings at the time of registration are consistent, which is used toCI pipelineThe host was successfully found in.

Command for code format review:python .workflow/ci/format_check/run-clang-format.py --clang-format-executable .workflow/ci/format_check/clang-format.exe -r components/。 Invoked in the commandPythonScripts andclang-format.exeExecute the file and specify the folder path to check.

Commands for code compilation:.workflow/ci/build/build_keil.bat, called the compilation script under the specified path.

3.2 Script and tool configuration

In the code warehouse .workflow > ci Place the script for format review in different folders under the pathclang-formatFormat review tool and compilation script:

Learn to build Ci / CD process on oneos Lite (gitlab)

format_checkFolder placementrun-clang-format.pyFormat review scripts andclang-format.exeExecution documents,clang-format.exeVersion is12.0.1

Learn to build Ci / CD process on oneos Lite (gitlab)

buildFolder placementbuild_keil.batCompile script:

Learn to build Ci / CD process on oneos Lite (gitlab)

3.3 Compile project

In the code warehouse .workflow > ci > projects_compilePlace one or more for compilation under the pathMDKProject, covering all codes through the configuration of one or more projects.

Learn to build Ci / CD process on oneos Lite (gitlab)

Learn to build Ci / CD process on oneos Lite (gitlab)

3.4 Configure runner for public warehouse

First, we need to find onewindowshost.

  1. Create a folder somewhere in the system, for example:F:\OneOS_Operate_System\GitLab-Runner
  2. download64 bitor32 bitBinaries and place them in the folder you created. Rename togitlab-runner.exe(optional).
  3. registerRunner

    openWindows PowerShell, enterF:\OneOS_Operate_System\GitLab-RunnerPath operation./gitlab-runner.exe registerCommand: required for intermediate stageURLTokenRunnerDescriptionRunnerIdentificationshell。 amongRunnerYour identification needs to be consistent with.gitlab-ci.ymlIn filetagsDescriptiveRunnerThe identity of is consistent, otherwise the specified cannot be foundRunner

    Learn to build Ci / CD process on oneos Lite (gitlab)

    Among themURLandTokenFrom the code warehouseRunnerSetting:

    Learn to build Ci / CD process on oneos Lite (gitlab)

  4. modifyconfig.toml

    Registration completedRunnerAfter, you canF:\OneOS_Operate_System\GitLab-RunnerSee under folderconfig.tomlFile, open and modify one place:shell = "pwsh"byshell = "powershell"

  5. start-upRunner

    Continue inWindows PowerShellExecute commands in sequence.\gitlab-runner.exe installand.\gitlab-runner.exe startYou can startRunner

    Learn to build Ci / CD process on oneos Lite (gitlab)

    After successful startup, you can seeRunnerThere is one more device in the next. The device ID iswin_ciTherefore.gitlab-ci.ymlIn filetagsIt should bewin_ci

    Learn to build Ci / CD process on oneos Lite (gitlab)

3.5 Personal warehouse configuration runner

Personal warehouseRunnerBuilt on the local machine, the configuration method is basically the same as that of the public warehouseRunnerConfiguration method, the only difference is registrationRunnerTimelyURLTokenIt needs to be obtained from the personal warehouse. Entered at registrationRunnerIdentification with public warehouseRunnerThe identification shall be consistent.

Learn to build Ci / CD process on oneos Lite (gitlab)

3.6 Runner environment configuration

To makeRunnerSuccessful format review command:python .workflow/ci/format_check/run-clang-format.py --clang-format-executable .workflow/ci/format_check/clang-format.exe -r components/And code compilation commands:.workflow/ci/build/build_keil.batYou also need toRunnerMake a certain configuration (both the server and the local computer must):

  1. installpython
  2. installMDK v5
  3. Add system environment variable(PythonandMDKBe sure to add)

    Learn to build Ci / CD process on oneos Lite (gitlab)

3.7 Restart runner

Enter command:./gitlab-runner.exe restartTo restartRunnerFor the first time, both the server and the machine should be restarted by executing this commandRunner。 After success, you do not need to execute this command. Even if the host is restarted, it can be used normallyRunnerThere is no need to do anything else.

3.8 trigger

Personal warehousepushOr from personal warehouse to public warehouseMergeThe request will trigger the pipeline and conduct code format review and compilation for the whole code warehouse. If any item fails, it will not be integrated into the public warehouse.

4. Focus on && contact

gitee: https://gitee.com/cmcc-oneos/OneOS-Lite

docs: https://oneos-lite.com/