使用excetor:shell
docker run -d --name gitlab-runner --restart always -v $PATH/config:/etc/gitlab-runner gitlab/gitlab-runner
使用数据卷PATH/config将gitlab-runner的配置文件保存到本机,可以动态修改
需要注意的是gitlab-runner部署前端项目可能需要执行npm install等命令,可以自己基于gitlab-runner基础镜像,安装相应的环境并打包新的镜像,用于gitlab-runner的构建。
ssh免密登录
在gitlab-runner里面,可能涉及到通过rsync、ssh连接远程服务器。需要配置免密登录。
生成RSA格式的公钥和密钥
cd /root/.ssh
ssh-keygen -m PEM -t rsa -b 4096 -f '/root/.ssh/id_rsa' -N ''
在A服务器上面执行命令ssh-coyp-id B服务器:
ssh-copy-id name@targetIp
会让你输入密码。
这一步会将公钥传递到targetIp,建立A和B机器的信任。
机器之间的信任实际是通过公钥和密钥的匹配来建立的。因此只要任意一台机器存有相同的密钥信息,就可以建立连接。
在gitlb-ci的Variable里面保存公钥密钥的相关信息,这个信息在A机器的~/.ssh/目录下面
在gitlab-ci里面配置信息
before_script:
- "which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )"
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
现在gitlab-runner和B机器就是互相信任的了
具体可以参考(152条消息) Gitlab配置CI/CD 使用ssh完成自动化部署_Hannnnnnnnnnnnn的博客-CSDN博客
高级篇:将gitlab-runner部署在k8s里面
采用exector:kubernetes
基于gitlab-runner的官方helm chart文件构建。
基于kubernetes的gitlab-runner本身是一个pod,基于gitlab-runner基础镜像运行。
每收到一个新的构建任务的时候,会创建一个新的pod,基于其它镜像运行(例如ubuntu)。这个镜像应该具有前端构建需要的相关环境(如果不需要环境的话,可以跳过下一步)
基于ubu镜像,构建一个新的镜像ubuntu-front
这个新的镜像主要是安装了npm、python和ssh(python是下载node-sass需要的环境)。里面涉及的2个压缩包,去官网上面下就可以了
FROM ubuntu:20.04
# 作者个人信息
MAINTAINER zhangxh
# 环境变量
ENV MYPATH /usr/local
ENV DEBIAN_FRONTEND noninteractive
# 设置工作目录,Dockerfile中的RUN、CMD、ENTRYPOINT、ADD、COPY等命令都会在这个目录中执行
WORKDIR $MYPATH
# 执行这几条命令,安装一些东西
RUN apt-get update -y
RUN apt-get install -y vim
RUN apt-get install -y net-tools
RUN apt-get install -y make
#python所需环境
RUN apt-get install -y gcc g++ zlib* build-essential libncurses5-dev libgdbm-dev libbz2-dev libssl-dev libnss3-dev libssl-dev libreadline-dev libffi-dev
RUN apt-get install --reinstall zlibc zlib1g zlib1g-dev
RUN apt-get install -y libc6-dev libsqlite3-dev openssl
#gitlab-ci所需环境
RUN apt-get install -y ssh rsync openssh-client
ADD id_rsa /root/.ssh/
ADD id_rsa.pub /root/.ssh/
ADD known_hosts /root/.ssh/
#安装node
ADD node-v14.16.1-linux-x64.tar.gz /usr/local/node/
RUN ln -s /usr/local/node/node-v14.16.1-linux-x64/bin/npm /usr/local/bin/npm
RUN ln -s /usr/local/node/node-v14.16.1-linux-x64/bin/node /usr/local/bin/node
#安装python
ADD Python-3.7.1.tgz /usr/local/Downloads
RUN /usr/local/Downloads/Python-3.7.1/configure --prefix=/usr/local/python3
RUN make
RUN make install
RUN rm -rf /usr/bin/python /usr/bin/pip
RUN ln -s /usr/local/python3/bin/python3.7 /usr/bin/python
RUN ln -s /usr/local/python3/bin/pip3.7 /usr/bin/pip
ADD pip.conf /root/.pip/
#暴露80端口
EXPOSE 80
# 指定在容器启动时需要执行的命令
CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash
下载最新的gitlab-runner Chart 修改里面的values.yaml文件
本文用的chart 0.51.0版本
image:
registry: registry.gitlab.com
image: gitlab-org/gitlab-runner
imagePullPolicy: IfNotPresent
gitlabUrl: https://gitlab.XX.XX.edu.cn/ #改成相应的url
runnerRegistrationToken: "GR1348941ejD4m-CvuWCzDHswBskw" #在相应的代码仓库里面找
rbac:
create: true
## Define specific rbac permissions.
## DEPRECATED: see .Values.rbac.rules
resources: ["pods", "pods/exec", "secrets", "configmaps", "pods/attach"] #注意以上都要
verbs: ["get", "list", "watch", "create", "patch", "delete", "update"]
runners:
config: |
[[runners]]
[runners.kubernetes]
namespace = "{{.Release.Namespace}}"
image = "ubuntu-frontend" #使用前面打包的镜像
network_mode = "host"
privileged = true #特权模式
locked: true #非共享girlab-runner
tags: "test"
runUntagged: false
name: "test"'
protected: false
helm install -n gitlab-runner gitlab-runner .
报错排查
下面讲了在过程中遇到的坑,目前网上的资料也比较老,大多数官方都有相应的issue没有解决
1.unable to access 'https://gitlab.XXXX': Could not resolve host: gitlab.xxxx
gitlab-runner无法正确解析域名。
部分博客说是gitlab-runner alpine镜像和kubernetes不匹配的问题。需要降级成alpinev12.0。但最新的gitlab-runner-chart已经升级到0.51版本,不再适配。降级gitlab-runner镜像会报错,将chart版本降级也不太合适。
但尝试了一下,目前最新的gitab-runner15.1版本已经不再有这个问题。作者仍然出现这个问题是因为k8s集群DNS出现问题。换了个k8s集群就不会报错了。
当然也有解决方法,在config里面添加一行
config: |
[[runners]]
pre_clone_script = "echo 'XX.XX.XX.XX gitlab.XXXXX' >> /etc/hosts"
针对其它域名无法解析的,可以用同样的方法在ci脚本里面,将域名注入到hosts文件
2.
ERROR: Error cleaning up secrets: resource name may not be empty
ERROR: Job failed (system failure): prepare environment: setting up build pod: error setting ownerReferences: secrets "runner-fczfho1a-project-525-concurrent-0x4b48" is forbidden: User "system:serviceaccount:gitlab-runner-frontend:gitlab-runner" cannot update resource "secrets" in API group "" in the namespace "gitlab-runner-frontend". Check https://docs.gitlab.com/runner/shells/index.html#shell-profile-loading for more information
是因为rbac权限配的有问题
rbac:
create: true
## Define specific rbac permissions.
## DEPRECATED: see .Values.rbac.rules
resources: ["pods", "pods/exec", "secrets", "configmaps", "pods/attach"] #添加configmap等
verbs: ["get", "list", "watch", "create", "patch", "delete", "update"]
3.npm install 报错
stack Error: EACCES: permission denied, mkdir '/builds/cloud-robot/chameleon/node_modules/node-sass/.node-gyp'
解决办法:
npm --unsafe-perm=true install
4.gitlab-runner启动报错
PANIC: creating directory: mkdir /nonexistent/.gitab-runner: permission denied
这也是因为用了较老的gitlab-runner镜像版本的原因。目前最新的镜像已经不会报错了
也有解决办法,修改values.yaml 配置数据卷(虽然不是很合适)
volumeMounts:
- name: root-gitlab-runner
mountPath: /nonexistent/.gitlab-runner
volumes:
- name: root-gitlab-runner
emptyDir:
medium: "Memory"
5.gitlab-runner基础镜像和chart的版本有对应关系,随意切换基础镜像的版本很容易报错。尽量就用最新的版本。
比如报错:flag provided but not defined: -template-config
在gitlab-runner里面使用docker(excetor:kubernetes)
1.docker in docker
这个不太好用。但配起来也比较简单
修改values.yaml文件
config: |
[[runners.kubernetes.volumes.empty_dir]]
name = "docker-certs"
mount_path = "/certs/client"
medium = "Memory"
修改ci
image: docker:20.10.16
variables:
# When using dind service, you must instruct Docker to talk with
# the daemon started inside of the service. The daemon is available
# with a network connection instead of the default
# /var/run/docker.sock socket.
DOCKER_HOST: tcp://docker:2376
#
# The 'docker' hostname is the alias of the service container as described at
# https://docs.gitlab.com/ee/ci/services/#accessing-the-services.
# If you're using GitLab Runner 12.7 or earlier with the Kubernetes executor and Kubernetes 1.6 or earlier,
# the variable must be set to tcp://localhost:2376 because of how the
# Kubernetes executor connects services to the job container
# DOCKER_HOST: tcp://localhost:2376
#
# Specify to Docker where to create the certificates. Docker
# creates them automatically on boot, and creates
# `/certs/client` to share between the service and job
# container, thanks to volume mount from config.toml
DOCKER_TLS_CERTDIR: "/certs"
# These are usually specified by the entrypoint, however the
# Kubernetes executor doesn't run entrypoints
# https://gitlab.com/gitlab-org/gitlab-runner/-/issues/4125
DOCKER_TLS_VERIFY: 1
DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"
services:
- docker:20.10.16-dind
注意 ,这里用到了一个镜像docker:20.10.16
这个会代替先前打包好的ubu镜像,作为当前任务的镜像。最好是将该镜像作为局部镜像。
2,使用Kaniko构建Docker镜像
还没学。
配置docker仓库
1.
before_script:
- docker info
- echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin $CI_REGISTRY
2.以上方法会警告,建议采用安全存储的方法,可以进一步改进