cicd引文目录是想通过dockerfile
构建 maven、jdk、docker环境的 gitlab-runner
运行环境。但docker最后测试的时候有点问题,且最后使用 kubectl
时有麻烦,所以放弃。但有参考意义,记录一下,后续有时间完善。
gitlab-runner使用 rpm
离线安装的方式来进行 CICD 实现,相关文章参考 此篇文章
cicd完成以下目标
gitlab-runner可以去 hub 上拉取最新版本,想自制离线安装镜像,请稳步参考 docker镜像的导入导出 ,无兴趣的直接使用在此提供离线资源 百度网盘。
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。
解决方案
# 解决 非 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.yml
# 直接启动
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
定制镜像的时候,maven源是不固定的,所以写死不好,改成动态配置。
方案如下
- /root/gitlab-runner/settings.xml:/usr/local/maven/apache-maven-3.8.1/conf/settings.xml
错误日志如下,后续注册至 gitlab 会自动生成
ERROR: Failed to load config stat /etc/gitlab-runner/config.toml: no such file or directory builds=0
执行如下命令
# 操作
# 进入容器交互
[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!