目录
概述
cicd引文目录是想通过dockerfile
构建 maven、jdk、docker环境的 gitlab-runner
运行环境。但docker最后测试的时候有点问题,且最后使用 kubectl
时有麻烦,所以放弃。但有参考意义,记录一下,后续有时间完善。
gitlab-runner使用 rpm
离线安装的方式来进行 CICD 实现,相关文章参考 此篇文章
cicd完成以下目标
- 所有环境和代码使用同一个仓库,将软件包纳入版本管理
- 团队共同决定发布流程
- 保持 DEV、TEST、PRODUCTION 环境的一致性
- 自动化回归测试,回归测试是指修改了旧代码后,重新进行测试以确认修改没有引入新的错误或导致其他代码产生错误。
- 小步提交,每日部署;而不是一次部署大量变更
- 更快、更频繁发布
离线资源
gitlab-runner可以去 hub 上拉取最新版本,想自制离线安装镜像,请稳步参考 docker镜像的导入导出 ,无兴趣的直接使用在此提供离线资源 百度网盘。
docker-compose
yaml
FROM gitlab/gitlab-runner:v11.11.2
MAINTAINER Lusifer <632105841@qq.com>
# 修改软件源
RUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse' > /etc/apt/sources.list && \
echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse' >> /etc/apt/sources.list && \
echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse' >> /etc/apt/sources.list && \
echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse' >> /etc/apt/sources.list && \
apt-get update -y && \
apt-get clean
# 安装 Docker
RUN apt-get -y install apt-transport-https ca-certificates curl software-properties-common && \
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add - && \
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" && \
apt-get update -y && \
apt-get install -y docker-ce
COPY daemon.json /etc/docker/daemon.json
# 安装 Docker Compose
WORKDIR /usr/local/bin
RUN wget https://raw.githubusercontent.com/topsale/resources/master/docker/docker-compose
RUN chmod +x docker-compose
# 安装 Java
RUN mkdir -p /usr/local/java
WORKDIR /usr/local/java
COPY jdk1.8.0_261.tar.gz /usr/local/java
RUN tar -zxvf jdk1.8.0_261.tar.gz && \
rm -fr jdk1.8.0_261.tar.gz
# 安装 Maven
RUN mkdir -p /usr/local/maven
WORKDIR /usr/local/maven
#RUN wget https://raw.githubusercontent.com/topsale/resources/master/maven/apache-maven-3.5.3-bin.tar.gz
COPY apache-maven-3.8.1-bin.tar.gz /usr/local/maven
RUN tar -zxvf apache-maven-3.8.1-bin.tar.gz && \
rm -fr apache-maven-3.8.1-bin.tar.gz
COPY settings.xml /usr/local/maven/apache-maven-3.8.1/conf/settings.xml
# 配置环境变量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_261
ENV MAVEN_HOME /usr/local/maven/apache-maven-3.8.1
ENV PATH $PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
# 解决 非 root 用户时,环境变量找不到
RUN echo export JAVA_HOME=/usr/local/java/jdk1.8.0_261 >> /etc/profile
RUN echo export PATH=$PATH:$JAVA_HOME/bin >> /etc/profile
RUN echo export MAVEN_HOME=/usr/local/maven/apache-maven-3.8.1 >> /etc/profile
RUN echo export PATH=$PATH:$MAVEN_HOME/bin >> /etc/profile
WORKDIR /
问题
在构建镜像的时候,发现 mvn
命令不能找到,但执行 docker exec
的时候是可以的,后经测试发现,使用 gitlab-runner
用户来操作 gitlab-runner。
解决方案
bash
# 解决 非 root 用户时,环境变量找不到
RUN echo export JAVA_HOME=/usr/local/java/jdk1.8.0_261 >> /etc/profile
RUN echo export PATH=$PATH:$JAVA_HOME/bin >> /etc/profile
RUN echo export MAVEN_HOME=/usr/local/maven/apache-maven-3.8.1 >> /etc/profile
RUN echo export PATH=$PATH:$MAVEN_HOME/bin >> /etc/profile
docker-compose
docker-compose.yml
yaml
# 直接启动
version: '3.1'
services:
gitlab-runner:
restart: always
image: gitlab/gitlab-runner:v11.11.2.2
privileged: true
volumes:
- /usr/local/docker/runner/config:/etc/gitlab-runner
- /var/run/docker.sock:/var/run/docker.soc
- /root/gitlab-runner/settings.xml:/usr/local/maven/apache-maven-3.8.1/conf/settings.xml
问题1
定制镜像的时候,maven源是不固定的,所以写死不好,改成动态配置。
方案如下
bash
- /root/gitlab-runner/settings.xml:/usr/local/maven/apache-maven-3.8.1/conf/settings.xml
问题2
错误日志如下,后续注册至 gitlab 会自动生成
bash
ERROR: Failed to load config stat /etc/gitlab-runner/config.toml: no such file or directory builds=0
gitlab-runner集成gitlab
执行如下命令
bash
# 操作
# 进入容器交互
[root@hadoop01 gitlab-runner]# docker exec -it gitlab-runner-gitlab-runner-1 /bin/bash
# 开始注册
root@1cdd1fbf2a66:/# gitlab-runner register
Runtime platform arch=amd64 os=linux pid=65 revision=ac2a293c version=11.11.2
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://10.32.36.142:8088/
Please enter the gitlab-ci token for this runner:
zTmKegWEfStFWa5hCbhR
Please enter the gitlab-ci description for this runner:
[1cdd1fbf2a66]: 测试
Please enter the gitlab-ci tags for this runner (comma separated):
test,dev,prod
Registering runner... succeeded runner=zTmKegWE
Please enter the executor: parallels, shell, ssh, virtualbox, docker+machine, docker-ssh+machine, kubernetes, docker, docker-ssh:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!