CICD实例:C++程序的自动化构建

本文描述一个 C++ 程序的自动化构建过程,涉及:
1、制作适用于 c++ 简单程序的基础镜像。
2、使用 CICD 进行编译和构建,并发布。在此过程,涉及到邮件通知。

本文示例在简单应用场合中有实践意义,即不使用如 jenkins 这样重型工具,如果信任现成免费的私有的服务,可将自动化工作迁移到公网,否则内建局域网服务或使用公有云搭建。

制作镜像

注:源码为 C++,使用 C++11 特性,选 alpine,其 C 库不是 glibc。选 busybox 64版本。在 64 位系统直接执行:

1
docker pull busybox

即可得到 64 位的版本。
运行 busybox:

1
docker run -it bar busybox sh

在宿主机拷贝动态库:

1
2
3
4
5
6
7
8
9
10
11
12
docker cp /usr/bin/file bar:/bin
docker cp /usr/lib64/libmagic.so.1 bar:/lib64
docker cp /usr/lib64/libmagic.so.1.0.0 bar:/lib64
docker cp /usr/lib64/libz.so.1 bar:/lib64
docker cp /usr/lib64/libz.so.1.2.7 bar:/lib64
docker cp /usr/share/misc/magic bar:/usr/share/misc // 要自己创建目录

docker cp /usr/lib64/libstdc++.so.6 bar:/lib64
docker cp /usr/lib64/libstdc++.so.6.0.19 bar:/lib64
docker cp /usr/lib64/libgcc_s.so.1 bar:/lib64
docker cp /usr/lib64/libgcc_s-4.8.5-20150702.so.1 bar:/lib64
docker cp /usr/lib64/libpthread.so bar:/lib64

重新制作基础镜像:

1
docker commit bar registry.cn-shenzhen.aliyuncs.com/latelee/busybox:64

上传:

1
docker push registry.cn-shenzhen.aliyuncs.com/latelee/busybox:64

CICD 配置

选用 CircleCI 执行 CI 脚本。

工程仓库

问题及解决

不能按一般的登陆方式登陆,否则提示:

1
2
3
4
docker login --username=$ali_docker_name -p $ali_docker_passwd registry.cn-shenzhen.aliyuncs.com
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: EOF

使用官方推荐方式:

1
2
echo "$ali_docker_passwd" | docker login --username $ali_docker_name --password-stdin  registry.cn-shenzhen.aliyuncs.com
Error: Cannot perform an interactive login from a non TTY device

此提示真正原因未知,在调试发现环境变量未生效,调整后,登陆成功。

参考

circleci文档:
使用docker命令:https://circleci.com/docs/2.0/building-docker-images/
配置选项参考说明:https://circleci.com/docs/2.0/configuration-reference/
环境变量:https://circleci.com/docs/2.0/env-vars/
自定义docker:https://circleci.com/docs/2.0/custom-images/
预置docker镜像:https://circleci.com/docs/2.0/custom-images/
注:免费情况下, circleci 私有仓库构建次数有限,术语为 Credits,每周共 2500 个,一次构建消耗数量为数个到数十个。