有许多执行器可用于使用 GitLab Runner 实现 CI/CD。但是,Shell 和 Docker 在其中更受欢迎,我们可以使用这些运行器轻松配置存储库。可以根据资源的要求和可用性来选择这些运行器。本文主要关注 C/C++ Linux 应用程序的 Shell 和 Docker 执行器,代码是用 bash 脚本编写的。该应用程序可以使用 bash 脚本进行构建和测试。
Shell Executor: Shell Executor 是一个非常简单的执行器,有助于在安装了 GitLab Runner 的机器上本地构建解决方案。在这种情况下,GitLab Runner 安装在 Linux Machine 上,因此需要在同一系统中安装所需的软件。
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 实例 URL :每个组织的 URL 可能不同,格式类似于 http://gitlab.example.com
- 路径:转到 GitLab 帐户 → 选择要向 runner 注册的存储库 → 设置 → CI/CD → 展开 Runner
- 输入此运行程序的 gitlab-ci 令牌:它将是每个项目的唯一令牌,注册时需要,可以找到路径:转到 GitLab 帐户 → 选择要向运行程序注册的存储库 → 设置 → CI/ CD → 展开转轮
- 输入此跑步者的 gitlab-ci 描述:输入跑步者名称(任何名称),这将帮助您记住哪个跑步者正在运行
- 输入此运行程序的 gitlab-ci 标签:如果您想在 yml 文件中提供特定标签时启动 GitLab 运行程序,则此选项是可选的。
- 进入执行器:会有几个执行器的列表,输入shell(因为GitLab Runner会运行我们的系统)
9.注册成功后,使用以下命令启动 GitLab Runner。
sudo gitlab-runner start
10.验证 GitLab Runner 是否已注册相应的存储库并且 runner 已启动。转到 GitLab 帐户 → 选择您要向 runner 注册的存储库 → 设置 → CI/CD → 展开 Runner,将有绿色圆圈可用,并显示消息将为该项目的 Runners 激活。注意:如果圆圈为灰色,则表示跑步者尚未开始并重新开始。
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
C/C++ 在 Docker Executor 上的实现:无需手动安装任何软件,一切都将从 docker 容器中获取。但是,您可以安装所需的软件,在 yml 文件中输入名称,也可以导出路径。要在 docker executor 模式下运行 gitlab runner,请转到 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