📌  相关文章
📜  在GitLab上使用Shell和Docker Executor在C / C++应用程序(Linux)中实现CI / CD

📅  最后修改于: 2021-05-30 11:13:29             🧑  作者: Mango

有许多执行程序可用于通过GitLab Runner实施CI / CD。但是,Shell和Docker在其中更受欢迎,并且我们可以轻松地使用这些运行器配置存储库。可以根据资源的需求和可用性来选择这些运行程序。本文主要关注C / C++ Linux应用程序的Shell和Docker执行器,并且代码以bash脚本编写。该应用程序可以使用bash脚本进行构建和测试。

Shell Executor: Shell Executor是一个非常简单的执行程序,可帮助在安装了GitLab Runner的计算机上本地构建解决方案。在这种情况下,GitLab Runner安装在Linux机器上,因此需要在同一系统中安装所需的软件。

Docker Executor:这是一个功能强大的工具,其中包含许多软件,并且可以通过映像进行访问。该执行程序的优点是,我们不需要手动安装任何软件,所有内容都将通过docker处理,并且所需的映像将从docker hub下载。但是,不利的是,出于安全目的,此通信在某些组织中被阻止。因此,如果是这种情况,Shell Executor是最好的选择。

Shell Executor上C / C++的实现

要求:这些是需要在Linux机器上安装的基本软件。但是,可以根据编译脚本进行更改,并且如果需要,还需要下载其他软件。

Software  

Description  

Git        This is the first requirement, to commit the changes on GitLab. It is a version control software which tracks the changing set of files
cmake  To build automation, testing, and packaging c/c++application, need to install cmake in Linux machine.
gcc It is a compiler that needs to compile the c/c++ programs 
g++ It is also a compiler that needs to compile the c/c++ programs. It can be chosen based on the written script.
grep Install it if the program is searching plain-text. 

路径配置:上面的安装成功后,需要在计算机中设置此已安装软件的路径(如果未设置)。在计算机上运行以下命令。

Variable /File                                   

Path 

G++ export GCC=/usr/bin/g++
CC export CC=/usr/bin/gcc
GREP export GREP=/usr/bin/grep
Permission  Give Permission to script before it run: the chmod -R 777 *
.gitlab-ci.yml This file should be inside root directory of the project which contains all the CI/CD configuration including software and script path. Here, you can mention how this repository should run. Before adding this file to the root directory, should check it is a valid yml file or not.

GitLab Runner设置:按照以下步骤下载并配置GitLab Runner。

1.使用以下命令在Linux计算机上下载GitLab Runner

sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

2.使用以下命令授予其执行权限

sudo chmod +x /usr/local/bin/gitlab-runner

3.使用以下命令创建一个GitLab CI

sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

4.使用以下命令安装并作为服务运行

sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner

5.使用以下命令启动GitLab Runner。

sudo gitlab-runner start

6.在注册存储库之前,停止GitLab Runner。

sudo gitlab-runner stop

7.成功停止GitLab Runner之后,在终端中输入以下命令进行存储库注册。

sudo gitlab-runner register

8.当您使用GitLab Runner注册存储库时,以下问题必须回答。

  • 输入您的GitLab实例网址:每个组织的网址可能不同,格式类似于http://gitlab.example.com
  • 路径:转到GitLab帐户→选择要向运行器注册的存储库→设置→CI / CD→扩展运行器
  • 输入该跑步者的gitlab-ci令牌:它将是注册时需要的每个项目的唯一令牌,并且可以找到它:路径:转到GitLab帐户→选择要向跑步者注册的存储库→设置→CI / CD→扩展亚军
  • 输入该跑步者的gitlab-ci描述:输入跑步者名称(任何名称),这将帮助您记住哪个跑步者正在跑步
  • 输入该运行程序的gitlab-ci标记:如果要在yml文件中有特定标记的情况下启动GitLab运行程序,则这是可选的。
  • 输入执行程序:将有几个执行程序的列表,并键入shell(因为GitLab Runner将运行我们的系统)

9.成功注册后,使用以下命令启动GitLab Runner。

sudo gitlab-runner start

10.确认GitLab Runner已经注册了各自的存储库,并且启动器已经启动。转到GitLab帐户→选择要向运行程序注册的存储库→设置→CI / CD→扩展运行程序,将有绿色圆圈可用,并且将为该项目激活运行程序。注意:如果圆圈为灰色,则表示跑步者尚未启动,请重新启动。

Linux GitLab Runner命令

按照其他一些GitLab Runner命令进行熟悉。

Command

Description 

sudo gitlab-runner register Register the project with GitLab Runner 
sudo gitlab-runner start Start the runner 
sudo gitlab-runner stop Stop the runner
sudo gitlab-runner status To know the status of gitlab-runner 
sudo gitlab-runner unregister –name  test-runner   Unregister the Runner of a project and replace the test-runner with your runner name and this name can be found inside the config.toml file (where your gitlab-runner ) available.
sudo gitlab-runner unregister –url http://gitlab.example.com/ –token t0k3n Remove Runner by URL and token 
sudo gitlab-runner unregister –all-runners To unregister All Runners
sudo gitlab-runner restart This command stops and then starts the GitLab Runner service
sudo gitlab-runner uninstall This command stops and uninstalls the GitLab Runner from being run as a service
sudo gitlab-runner exec To see a list of available executors, run
sudo gitlab-runner –help Check a recent list of commands by executing
sudo gitlab-runner run –help Can see the name of the environment variable
sudo gitlab-runner –debug To run a command in debug mode
sudo gitlab-runner exec shell To see a list of all available options for the shell executor, run

.gitlab-ci.yml_ shell执行器:

以下是Shell执行程序模式下.gitlab-ci.yml的内容。但是,如果需要,请更改它。

stages:
 - build
 - test

build_job:
 stage: build
 only:
   - master
 script:  
    - cd sourcecode
    - export G++=/usr/bin/g++  //if not set manually path of g++
    - export GCC=/usr/bin/gcc //if not set manually path of gcc
    - chmod -R 777 *
    - ./BuildPackage.sh
    - pwd  
     
 artifacts:
   expire_in: 365 days   //save the binary which needed while test the application, and it can be downloaded from GitLab
   paths:
      - sourcecode/binaryfolder_name  // save the binary  
       
     
test_job:
 stage: test
 only:
   - master
 script:
    - pwd  
    - cd testdir       //go to test directory for run the test case script  
    - chmod -R 777 *
    - ./tests.sh
   
 dependencies:
   - build_job
   - build_job

在Docker Executor上实现C / C++:无需手动安装任何软件,所有内容都将从Docker容器中获取。但是,您可以安装所需的软件,在yml文件中输入名称,也可以导出路径。要在docker executor模式下运行gitlabRunner,请转到GitLab Runner设置(上),然后选择docker而不是shell。

.gitlab-ci.yml_ Docker执行器

以下是docker executor模式下.gitlab-ci.yml的内容。但是,如果需要,请更改它。

image: ubuntu:latest
 
stages:
 - build
 - test
 
before_script:
 - echo "Before script install this on ubuntu image "
 - apt-get update && apt-get -y install cmake && apt-get -y install gcc &&  apt-get -y install g++
 

build_job:
 stage: build
 only:
   - master
 script:  
    - cd sourcecode
    - export G++=/usr/bin/g++  //if not set manually path of g++
    - export GCC=/usr/bin/gcc //if not set manually path of gcc
    - chmod -R 777 *
    - ./BuildPackage.sh
    - pwd  
     
 artifacts:
   expire_in: 365 days   //save the binary which needed while test the application, and it can be downloaded from GitLab
   paths:
      - sourcecode/binaryfolder_name  // save the binary  
       
     
test_job:
 stage: test
 only:
   - master
 script:
    - pwd  
    - cd testdir       //go to test directory for run the test case script  
    - chmod -R 777 *
    - ./tests.sh
   
 dependencies:
   - build_job
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”