使用docker部署编译环境并使用gitlab-ci实现代码自动打包

一、需求

1、需求描述

实现代码git仓库提交能够自动编译出结果

①需要一个编译环境能够共享编译:使用docker创建镜像来维护,创建完毕无需频繁修改

②使用gitlab的ci进行持续继承,代码提交自动打包,ci中会使用①中的镜像作为运行环境并自动挂载代码

2、基础说明

编译环境:ubuntu

代码构建工具:cmake

语言:c++

使用gitlab作为仓库管理:30天试用期

docker镜像存储再官方仓库:dockerhub上

二、预研

1、gitlab-ci运行机制

参考:https://zhuanlan.zhihu.com/p/184936276

参考:https://www.cnblogs.com/mrxccc/p/16504723.html

(1) .gitlab-ci.yml文件:通过在项目根目录下配置.gitlab-ci.yml文件,可以控制ci流程的不同阶段,例如install/检查/编译/部署服务器。gitlab平台会扫描.gitlab-ci.yml文件,并据此处理ci流程

(2) 触发:ci流程在每次团队成员push/merge后之后触发。每当你push/merge一次,gitlab-ci都会检查项目下有没有.gitlab-ci.yml文件,如果有,它会执行你在里面编写的脚本,并完整地走一遍从intall => eslint检查=>编译 =>部署服务器的流程

(3)gitlab-runner:gitlab-ci提供了指定ci运行平台的机制,它提供了一个叫gitlab-runner的软件,只要在对应的平台(机器或docker)上下载并运行这个命令行软件,并输入从gitlab交互界面获取的token,就可以把当前机器和对应的gitlab-ci流程绑定,也即:每次跑ci都在这个平台上进行

(4)可视化:.gitlab-ci的所有流程都是可视化的,每个流程节点的状态可以在gitlab的交互界面上看到,包括执行成功或失败。如下图所示,因为它的执行看上去就和多节管道一样,所以我们通常用“pipeLine”来称呼它

(5).不同push/merge所触发的CI流程不会互相影响,也就是说,你的一次push引发的CI流程并不会因为接下来另一位同事的push而阻断,它们是互不影响的。这一个特点方便让测试同学根据不同版本进行测试。

(6)pipeline不仅能被动触发,也是可以手动触发的。

三、实现

在虚拟机的64位ubuntu环境中搭建docker

镜像:ubuntu-18.04.6-desktop-amd64.iso

1、使用docker构建编译环境基础镜像

1.1 安装docker

此部分参照官方文档:https://docs.docker.com/engine/install/ubuntu/

1.1.1 设置Docker's apt repository

让apt源中能有包

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

1.1.2 安装最新版

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

若出现“gsettings-desktop-schemas : 破坏"问题,执行以下命令再重新安装

sudo apt install gsettings-desktop-schemas

1.1.3 查看运行情况

systemctl status docker 或 docker -v

1.2 构建编译环境镜像

在docker中配置c++编译环境

思路:在docker中拉取ubuntu基础镜像,在基础镜像中安装gcc/g++/cmake等需要的工具

1.2.1 docker pull

拉取镜像,此处指定了版本

docker pull ubuntu:18.04

1.2.2 构建镜像

通过Dockerfile配置文件创建镜像

①Dockerfile内容

FROM ubuntu:18.04
# 配置源
# COPY /etc/apt/sources.list /etc/apt/sources.list
RUN apt-get -y update && \
apt-get install -y build-essential g++ libssl-dev && \
apt-get install -y cmake && \
apt-get install -y zip

②使用以下命令构造镜像

docker build -t myubuntu .

注释:可利用挂载目录和交互方式导入代码并测试是否能够编译成功

以交互方式运行容器,--rm表示运行结束删除,-v表示目录挂载,--name命名容器

docker run -it --rm --name myubuntu-container -v /home/work:/app myubuntu /bin/bash

或,运行后,再使用另一个命令进入容器

docker run -it --name my-ubuntu-container myubuntu
docker exec -it my-ubuntu-container bash

1.3 上传镜像

上传gitlab镜像仓库可供gitlab ci获取

# 登陆
docker login -u username
# 新建tag
docker tag test:v1 username/test:v1
# 推上去
docker push username/test:v1
# 部署试下
docker run username/test:v1

2、使用gitlab-ci实现持续集成

效果:代码上传仓库能够自动触发构建生成制品

思路:gitlab-ci需要安装一个环境上runner,runner会作为一个work取执行编译的工作,如果指定了镜像,会以此为运行环境,其中需要在代码仓库根目录下创建一个.gitlab-ci.yml文件,并编写信息以联动代码变动和构建。

2.1. 下载、创建和注册gitlab-runner

可在平台上直接安装或者使用docker管理,其实编译环境也可以直接部署,但是使用docker能够标准化共享化轻量化。

2.1.1 安装

以下方式可择其一

平台直接安装

官方参考:https://docs.gitlab.com/runner/install/

ubuntu上的安装

注释:若安装失败需要配置下源

在Ubuntu中配置软件镜像源可以帮助加快软件包的下载速度,并且从可靠的镜像服务器获取软件更新。

备份原配置文件:

sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup

编辑/etc/apt/sources.list文件,删除原有内容,填入以下内容后保存

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

使用apt-get update更新

①添加仓库

Add the official GitLab repository

添加官网GitLab 仓库

curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
②安装

Install the latest version of GitLab Runner, or skip to the next step to install a specific version

安装GitLab Runner的最新版本,或跳到下一步安装特定版本,此处跳到下一步下载特定版本(gitlab的ci明确要求)

sudo apt-get install gitlab-runner
③ 安装特定版本
apt-cache madison gitlab-runner
sudo apt-get install gitlab-runner=16.5.0
④查看版本
gitlab-runner

11.2.0 (11.2.0)

注释:也可直接使用deb包安装

docker镜像安装
①docker pull

获取镜像

docker pull gitlab/gitlab-runner:latest

docker images:查看镜像

②docker run

启动镜像

docker run -d --name gitlab-runner --restart always -v /home/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner

d: 后台运行容器,并返回容器ID

–name: 指定容器名称为 gitlab-runner

-v: 主机目录映射到容器目录,主机目录 : 容器目录

gitlab/gitlab-runner: 镜像名称

docker ps:查看容器

③进入交互界面
docker exec -it gitlab-runner bash

2.1.2 创建runner

官方参考:https://docs.gitlab.com/ee/tutorials/create_register_first_runner/

Runner 安装完毕,在真正使用之前需要先进行注册。注册的目的是让 Runner 和GitLab 实例建立链接通道,当GitLab 实例中的项目有 CI/CD Pipeline需要执行的时候,就会通过这个注册的 Runner 来执行.

①创建项目

登陆https://docs.gitlab.com/runner/register/index.html创建一个项目

②创建项目的runner

通过项目的 Setting –> CI/CD –> Runner 中新建选项创建一个runner,填写tags,此处获取Gitlab实例的URL和Token

获取Gitlab实例的URLToken,这些内容可以通过项目的 Setting –> CI/CD –> Runner 选项来获取

2.1.3 运行注册命令

# 此处token替换成实际的
gitlab-runner register --url https://gitlab.com --token glrt-_sQ***W4zM

交互界面如下

Runtime platform                                    arch=amd64 os=linux pid=15627 revision=853330f9 version=16.5.0
Running in system-mode.

Enter the GitLab instance URL (for example, https://gitlab.com/):
[https://gitlab.com]:
Verifying runner... is valid                        runner=_sQ1WZeWs
Enter a name for the runner. This is stored only in the local config.toml file:

[vm]: local

Enter an executor: custom, docker, parallels, ssh, docker+machine, kubernetes, docker-windows, shell, virtualbox, docker-autoscaler, instance:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

说明:

executor表示默认的运行环境,可以时shell,docker之类,之后在ci文件中可以再设置

2.1.4 结果

gitlab 显示连接成功,小绿原点

2.2 项目和ci配置文件

项目目录结构:gitlab的仓库

├── .gitlab-ci.yml
├── hellowold.sh
└── hellowold.cpp

hellowold.cpp

#include <stdio.h>
int main()
{
   printf("Hello, World!");
   return 0;
}

hellowold.sh:测试脚本

#!/bin/bash

g++ helloworld.cpp -o helloworld
wait
./helloworld

.gitlab-ci.yml

# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/C++.gitlab-ci.yml

# use the official gcc image, based on debian
# can use verions as well, like gcc:5.2
# see https://hub.docker.com/_/gcc/

image: gcc

build:
  stage: build
  # instead of calling g++ directly you can also use some build toolkit like make
  # install the necessary build tools when needed
  # before_script:
  #   - apt update && apt -y install make autoconf
  script:
    - echo "Compiling the code..."
    - g++ helloworld.cpp -o helloworld
    - echo "Compile complete."
  artifacts:
    paths:
      - helloworld
      # depending on your build setup it's most likely a good idea to cache outputs to reduce the build time
      # cache:
      #   paths:
      #     - "*.o"

# run tests using the binary built before
test:
  stage: test
  script:
    - chmod +x helloworld.sh
    - ./helloworld.sh

注释:gitlab-runner在docker中,需要使用宿主机上的另一个镜像编译得到制品:image标识

以上修改进行提交

2.3 效果

此后每次commit或者merge都会触发自动构建

四、使用

1、同一项目使用多个runner

1.1 设计

需求:需要同时打x86和arm的包,runner分别跑在两个环境中

思路:

问题:同一个的gitlab ci中的不同job运行在不同的runner上,job1运行在windows上,job2运行在linux上

解决:使用tags设定运行条件,满足tag才能被执行,ci中的tags可以和runner中的tags匹配

注意:这里的runner的tag不是runner的名字

测试:

使用两个不同的runner,设置不同的tag

测试环境:

在同一条机器上配置两个runner docker,每个runner分别进行注册,设置不同的tags信息

1.2 实施

1.2.1 准备

gitlab新建仓库,添加一个helloworld.cpp文件

内容如下

#include <stdio.h>
int main()
{
   printf("Hello, World!");
   return 0;
}

1.2.2 设置第一个runner

①代码仓库添加runner

路径:settings--CI/CD--Runners--点击New project runner

注释:This registration process is only supported in GitLab Runner 15.10 or later

填写相应信息:默认linux环境,Tags设置env1,可设置多个

点击提交后,保存以下token信息

gitlab-runner register  --url https://gitlab.com  --token glrt-qEzhV6***5XBaM
②docker安装runner
# 拉取镜像
docker pull gitlab/gitlab-runner:latest
# 启动镜像,注意不同的容器命名不同
docker run -d --name gitlab-runner1 --restart always -v /home/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner
# 进入容器进行交互
docker exec -it gitlab-runner1 bash
# 进入容器中的命令行,输入1中的注册命令
gitlab-runner register  --url https://gitlab.com  --token glrt-qEzhV6***5XBaM

交互信息如下:

root@f982cd2ba513:/# gitlab-runner register  --url https://gitlab.com  --token glrt-qEzhV6XKjFrXfQR5XBaM
Runtime platform                                    arch=amd64 os=linux pid=59 revision=c72a09b6 version=16.8.0
Running in system-mode.

Enter the GitLab instance URL (for example, https://gitlab.com/):
[https://gitlab.com]: https://gitlab.com
Verifying runner... is valid                        runner=qEzhV6XKj
Enter a name for the runner. This is stored only in the local config.toml file:
[f982cd2ba513]: runner1
Enter an executor: docker-autoscaler, custom, ssh, parallels, kubernetes, docker+machine, instance, shell, virtualbox, docker, docker-windows:
docker
Enter the default Docker image (for example, ruby:2.7):
g++
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

1.2.3 设置第二个runner

具体流程同上,简化版本

新建runner信息:tags设置env2

gitlab-runner register  --url https://gitlab.com  --token glrt-6ozF4VB***c63Xg7
# 启动镜像,注意不同的容器命名不同
docker run -d --name gitlab-runner2 --restart always -v /home/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner
# 进入容器进行交互
docker exec -it gitlab-runner2 bash
# 进入容器中的命令行,输入1中的注册命令
gitlab-runner register  --url https://gitlab.com  --token glrt-6oz***c63Xg7

交互信息如下:

root@8daca704d34e:/# gitlab-runner register  --url https://gitlab.com  --token glrt-6ozF4VB5UyUuGHc63Xg7
Runtime platform                                    arch=amd64 os=linux pid=35 revision=c72a09b6 version=16.8.0
Running in system-mode.

Enter the GitLab instance URL (for example, https://gitlab.com/):
[https://gitlab.com]: https://gitlab.com
Verifying runner... is valid                        runner=6ozF4VB5U
Enter a name for the runner. This is stored only in the local config.toml file:
[8daca704d34e]: runner2
Enter an executor: docker, docker+machine, instance, docker-windows, kubernetes, docker-autoscaler, custom, shell, ssh, parallels, virtualbox:
docker
Enter the default Docker image (for example, ruby:2.7):
username/myubuntu
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

1.2.4 编写ci脚本

仓库根目录下,新建.gitlab-ci.yml文件,填入以下内容

stages:
  - build  
  - test

build.macos:
  stage: build
  image: username/myubuntu
  # instead of calling g++ directly you can also use some build toolkit like make
  # install the necessary build tools when needed
  # before_script:
  #   - apt update && apt -y install make autoconf
  script:
    - echo "Compiling the code..."
    - pwd
    - g++ helloworld.cpp -o helloworld
    - mkdir build 
    - cd build
    - cmake -DPROJECT_VERSION_MAJOR=2 -DPROJECT_VERSION_MINOR=1 -DPROJECT_VERSION_PATCH=0 ..
    - make
    #- docker run --rm --name myubuntu-container -v /home/work:/app myubuntu
    - echo "Compile complete."
  artifacts:
    name: all
    paths:
      - ./helloworld
      - ./build/Tutorial

build.env1:
  stage: build
  image: gcc
  script:
    - echo "1Compiling the code..."
    - g++ helloworld.cpp -o helloworld1
    - echo "1Compile complete."
  artifacts:
    name: helloworld
    paths:
      - ./helloworld1
      
build.env2:
  stage: build
  image: username/myubuntu
  script:
    - echo "2Compiling the code..."
    - pwd
    - g++ helloworld.cpp -o helloworld2
    - echo "2Compile complete."
  artifacts:
    name: helloworld
    paths:
      - ./helloworld2

# run tests using the binary built before
test:
  stage: test
  script:
    - chmod +x helloworld.sh
    - ./helloworld.sh

1.2.5 效果

生成的制品会根据不同build标签打不同的包

2、版本号设置

2.1 描述

需要给制品可执行程序加上{版本号-hash}这样的标签。

考虑可否在build的cmake命令中增加控制由此增加版本号。

2.2 研究

Linux下可执行程序增加版本号的方式

2.2.1 显示:-v参数输出

版本号在程序代码文件中修改

思路:程序内部定义版本宏,然后 main 函数通过-v参数,打印版本号和编译时间

#include <stdio.h>
#include <unistd.h>

// 先定义宏
#define CC_APP_XXX_VERSION_NAME  "MY_PROGRAME"
#define CC_APP_XXX_MAJOR_VERSION "1"
#define CC_APP_XXX_MINOR_VERSION "0"
#define CC_APP_XXX_PATCH_VERSION "0"


int main(int argc, char** argv)
{
    int opt = -1;

    while ((opt = getopt(argc, argv, "vha:m:")) != -1) {
        switch (opt) {
            case 'v':
                printf("%s %s.%s.%s.%s %s %s released.\n", argv[0], CC_APP_XXX_VERSION_NAME, CC_APP_XXX_MAJOR_VERSION, CC_APP_XXX_MINOR_VERSION, CC_APP_XXX_PATCH_VERSION, __DATE__, __TIME__);
                return 0;
            // case 'h':
            //     printf("Usage: %s [-v] [-h] [-a para] [-m para]\n", argv[0]);
            //     return 0;
            // case 'a':
            //     printf("option [a] param: %s\n", optarg);
            //     break;
            // case 'm':
            //     printf("option [m] param: %s\n", optarg);
            //     break;
            default: /* '?' */
                break;
        }
    }

    // 正常运行打印一次版本号
    printf("Version: %s.%s.%s.%s, %s %s released.\n", CC_APP_XXX_VERSION_NAME, CC_APP_XXX_MAJOR_VERSION, CC_APP_XXX_MINOR_VERSION, CC_APP_XXX_PATCH_VERSION, __DATE__, __TIME__);
    /*自己的程序*/
}

2.2.2 自动化构建工具传入代码版本号

使用自动化构建工具

版本号在CMakeLists.txt中修改

思路:在CMakeLists.txt文件中定义一个变量来表示版本号,然后在编译过程中将该变量传递给代码

传递过程:cmake -D变量名=变量值--->CMakeLists.txt文件中将变量传给c/c++代码

主要方法:CMakeLists.txt

1、configure_file命令+配置文件生成定义宏的头文件,程序引用此头文件才能使用宏

2、add_definitions直接添加宏,程序可直接使用

①cmake添加
configure_file

为程序添加版本号和带有使用版本号的头文件

参考:https://www.cnblogs.com/codingbigdog/p/16858818.html

文件结构

├── CMakeLists.txt
├── TutorialConfig.h.in
└── tutorial.cpp

CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)
project(Tutorial)
# The version number.
set (PROJECT_VERSION_MAJOR 1)
set (PROJECT_VERSION_MINOR 0)
set (PROJECT_VERSION_PATCH 0)
 
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
  "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
  "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  )
 
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
 
# add the executable
add_executable(Tutorial tutorial.cpp)

CMakeLists.txt中的configure_file指令:将TutorialConfig.h.in中的内容复制到TutorialConfig.h(如果TutorialConfig.h不存在,那么会自动创建),并且将TutorialConfig.h.in中的@Tutorial_VERSION_MAJOR@和@Tutorial_VERSION_MINOR@替换为CmakeLists.txt中的1和0。 这样做就让TutorialConfig.h使用到了CmakeLists.txt中的变量。TutorialConfig.h.in内容如下:

// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @PROJECT_VERSION_MINOR@
#define Tutorial_VERSION_PATCH @PROJECT_VERSION_PATCH@

tutorial.cpp:

// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
 
int main (int argc, char *argv[])
{
  if (argc < 2)
    {
    fprintf(stdout,"%s Version %d.%d.%d\n",
            argv[0],
            Tutorial_VERSION_MAJOR,
            Tutorial_VERSION_MINOR,
            Tutorial_VERSION_PATCH);
    fprintf(stdout,"Usage: %s number\n",argv[0]);
    return 1;
    }
  double inputValue = atof(argv[1]);
  double outputValue = sqrt(inputValue);
  fprintf(stdout,"The square root of %g is %g\n",
          inputValue, outputValue);
  return 0;
}

编译:

cmake .
make
./Tutorial

结果:

./Tutorial Version 1.0
Usage: ./Tutorial number

此项可结合1进行-v版本输出

add_definitions--推荐

目录结构:

├── CMakeLists.txt
└── tutorial.cpp

CMakeLists.txt:此处使用一个版本号替代-多个点需要设置成字符串形式,并设置一个hash值

最终目标展示:version-hash,如2.2.2-sdilfhj12

cmake_minimum_required (VERSION 2.6)
project(Tutorial)

# The version number and hash
if (NOT DEFINED PROJECT_TUTORIAL_VERSION)  
  set(PROJECT_TUTORIAL_VERSION 2.2.2)  
endif()
add_definitions(-DTUTORIAL_VERSION=“${PROJECT_TUTORIAL_VERSION}”)

if (NOT DEFINED HASH_VALUE)  
  set(HASH_VALUE "1234567890")
endif()
add_definitions(-DHASH_VALUE="${HASH_VALUE}")
 
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
 
# add the executable
add_executable(Tutorial tutorial.cpp)

tutorial.cpp

#include <stdio.h>
#include <unistd.h>

int main (int argc, char *argv[])
{
  if (getopt(argc, argv, "v") != -1)
  {
    fprintf(stdout,"%s Version %s-%s\n",
            argv[0],
            TUTORIAL_VERSION,
            HASH_VALUE);
    return 0;
  }
}

使用

cmake -DPROJECT_TUTORIAL_VERSION=1.1.1 -DHASH_VALUE=05u14938uy698huifdsb .. 
# 或默认cmake ..
make
./Tutorial -v

显示

./Tutorial Version 1.1.1-05u14938uy698huifd
②配置可通过-D传入

通过cmake命令传入cmake -DVAR_NAME=value ..可以在命令行中设置变量

CMakeLists.txt相应修改:set设置后传入参数会被覆盖

解决:若变量未定义则再设置

set (PROJECT_VERSION_PATCH 0)
改成
if (NOT DEFINED PROJECT_VERSION_MAJOR)  
  set(PROJECT_VERSION_MAJOR 1)  
endif()

使用:

cmake -DPROJECT_VERSION_MAJOR=2 ..

2.2.3 使用版本控制系统标签

1、可以通过版本控制系统如Git标签来记录和管理版本号。当你准备发布新版本时,在代码仓库中打上一个带有对应版本号的标签

2、.gitlab-ci.yml中可使用CI_COMMIT_SHORT_SHA环境变量,它是依据是依据Git提交的SHA值设定的。当你在GitLab中创建一个新的提交时,Git会为该提交生成一个唯一的SHA值。

使用

CMakeLists.txt中修改

# 定义哈希值变量
if (NOT DEFINED HASH_VALUE)  
  set(HASH_VALUE "1234567890") # 此处为示例哈希值
endif()

# 将哈希值作为编译器选项传递给C++头文件
add_definitions(-DHASH_VALUE="${HASH_VALUE}")

注释:如果此处用add_definitions了就不需要用configure_file传递了

上面的版本号也可用此方法

c++文件中可直接使用此HASH_VALUE宏进行引用

3、自动发布

Gitlab CI构建的包推送到Nexus存储库或其他仓库

五、其他

1、docker容器可以针对不同的运行环境进行编译,但是并不能架构。
比如x86_64的宿主机,是无法做aarch64架构下的编译的,
当然如果能找到合适的交叉编译工具链,也可以做交叉编译

六、参考

1、GItlab-ci CI/CD部署C++ helloworld项目_gitlab-ce cicd c++-CSDN博客

2、Tutorial: Create, register, and run your own project runner | GitLab

3、Gitlab-ci:从零开始的前端自动化部署 - 知乎 (zhihu.com)

posted on 2024-01-26 10:04  circlelll  阅读(183)  评论(0编辑  收藏  举报