本文描述一个 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 | docker cp /usr/bin/file bar:/bin |
重新制作基础镜像:
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 | docker login --username=$ali_docker_name -p $ali_docker_passwd registry.cn-shenzhen.aliyuncs.com |
使用官方推荐方式:
1 | echo "$ali_docker_passwd" | docker login --username $ali_docker_name --password-stdin registry.cn-shenzhen.aliyuncs.com |
此提示真正原因未知,在调试发现环境变量未生效,调整后,登陆成功。
参考
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 个,一次构建消耗数量为数个到数十个。