gitlab之cicd的gitlab-runner集成-dockerfile构建环境

目录

概述

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! 


相关推荐
bitcsljl26 分钟前
Linux系统中卸载GitLab
linux·运维·gitlab
Apifox.8 小时前
如何将 Apifox 的自动化测试与 Jenkins 集成?
功能测试·测试工具·ci/cd·jenkins·devops
猴哥聊项目管理1 天前
持续部署的7个陷阱及其避免方法
测试工具·ci/cd·产品经理·敏捷开发·devops·敏捷流程
krb___1 天前
python拉取gitlab项目以及拉取报错处理
gitlab
周其军2 天前
npm ci vs npm i
前端·javascript·vue.js·ci/cd·npm·node.js
Apifox2 天前
Apifox 自动化测试与 Jenkins 集成指南
ci/cd·jenkins·测试
天草二十六_简村人2 天前
nexus未开启匿名访问Anonymous Access,访问maven元数据maven-metadata,报401未授权Unauthorized错误
xml·java·ci/cd·jdk·maven·devops
dami_king3 天前
新手怎么使用GitLab?
运维·git·svn·云原生·gitlab·github
天亮有惊喜3 天前
Git、Gerrit的使用记录(待完善)
git·ci/cd·gerrit
linyb极客之路3 天前
聊聊gitlab ci如何构建以时间为版本号的docker镜像
ci/cd·docker·gitlab