GItlab-ci CI/CD部署C语言helloworld项目
安装Gitlab-runner命令行
- 根据所有的系统类型参考Install GitLab Runner官网的文档进行安装;
- 我使用的是Ubuntu系统的安装方式install;
- 添加官方 GitLab 存储库:
$ curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
- 安装最新版本的 GitLab Runner,或跳到下一步安装特定版本:
$ sudo apt-get install gitlab-runner
- 要安装特定版本的 GitLab Runner:
$ apt-cache madison gitlab-runner $ sudo apt-get install gitlab-runner=10.0.0
- deb文件安装;
$ curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/deb/gitlab-runner_amd64.deb" $ ls -ltr # 安装 $ dpkg -i gitlab-runner_amd64.deb
Runner 的注册
Runner 安装完毕,在真正使用之前需要先进行注册。注册的目的是让 Runner 和GitLab 实例建立链接通道,当GitLab 实例中的项目有 CI/CD Pipeline 需要执行的时候,就会通过这个注册的 Runner 来执行。极狐GitLab Runner 的注册很简单,通过gitlab-runner register
命令即可。
- 创建一个CICD_HelloWold的项目:
- 获取Gitlab实例的
URL
和Token
,这些内容可以通过项目的 Setting –> CI/CD –> Runner 选项来获取,如下图所示:
- 检查是否安装了Runner
$ gitlab-runner list 1 ↵
Runtime platform arch=amd64 os=linux pid=1125737 revision=98daeee0 version=14.7.0
Listing configured runners ConfigFile=/home/lemu-devops/.gitlab-runner/config.toml
- 可以看到,当前没有可使用的 Runner,接下来开始注册
$ gitlab-runner register 126 ↵
Runtime platform arch=amd64 os=linux pid=1132198 revision=98daeee0 version=14.7.0
WARNING: Running in user-mode.
WARNING: The user-mode requires you to manually start builds processing:
WARNING: $ gitlab-runner run
WARNING: Use sudo for system-mode:
WARNING: $ sudo gitlab-runner...
Enter the GitLab instance URL (for example, https://gitlab.com/):
https://gitlab.com/ # 从gitlab界面的Setting中获取url
Enter the registration token:
hvuZUyUtuvyuSAnStkqw # 从gitlab界面的Setting中获取url
Enter a description for the runner:
[lemudevops]: test runner # runner描述
Enter tags for the runner (comma-separated):
build # 表明这个runner只能执行tag为build的jod。
Registering runner... succeeded runner=hvuZUyUt
Enter an executor: docker-ssh, shell, ssh, virtualbox, docker+machine, docker-ssh+machine, docker, parallels, kubernetes, custom:
shell # 按实际平台填写,linux这里选shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
- 激活Runner
注册完了可能还需要激活,这时我们可以看下面板,如果有个黑色的感叹号,这说明Runner注册成功了,但是尚未激活(如果是绿色的说明已经激活,本步骤跳过)。
激活方法是本地运行的:
$ gitlab-runner verify
Runtime platform arch=amd64 os=linux pid=1137496 revision=98daeee0 version=14.7.0
WARNING: Running in user-mode.
WARNING: The user-mode requires you to manually start builds processing:
WARNING: $ gitlab-runner run
WARNING: Use sudo for system-mode:
WARNING: $ sudo gitlab-runner...
Verifying runner... is alive runner=G34swjUP
再刷新下网页,黑色的变绿色就说明激活成功了。
如果没有激活成功需要运行一下命令重新启动Runner.
$ gitlab-runner verify
$ gitlab-runner restart
Runner的使用
- 创建YAML的配置文件,不用手动创建,回到Gitlab项目根目录,选择配置CI/CD-编辑-创建新的CI/CD流水线-浏览模板(新的窗口打开)-选择C++.gitlab-ci.yml-复制配置内容-回到根项目的配置粘贴复制的配置内容。
- 拉取cicd_helloworld的项目到本地,创建helloworld.cpp
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
- 创建test的测试脚本:
#!/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
- 提交(PUSH))代码触发流水线自动运行
git add .
git commit -m "helloworld"
git push origin main
- 回到网页打开流水线,会看到流水线正在执行,如果build没有完成的话,还没有到test的步骤。
查看build的信息
Build编译通过后会自动运行test测试。
test的信息
以上只是CI/CD的一个测试Demo,主要是让入门开发者了解CI/CD的一个自动流水线测试的一个过程,方便大家去理解。