Gitlab Runner Docker模式使用代理

构建代理镜像供runner使用

定义Dockerfile

1
2
3
4
FROM laoyutang/clash-and-dashboard:latest

RUN wget -O /root/.config/clash/config.yaml [订阅地址]
RUN sed -i 's/^mixed-port:.*$/mixed-port: 7890/' /root/.config/clash/config.yaml

构建镜像

1
docker build -t myclash .

在cicd脚本中使用代理镜像

通过service的方式启动myclash容器

1
2
3
4
5
6
7
8
9
10
11
12
13
build:
stage: build
services:
- name: myclash:latest
alias: clash
before_script:
# 设置代理环境变量
- export http_proxy="http://clash:7890"
- export https_proxy="http://clash:7890"
- export HTTP_PROXY="http://clash:7890"
- export HTTPS_PROXY="http://clash:7890"
script:
- xxx

拓展:script中docker build使用代理

1
2
3
4
5
6
7
8
9
10
11
build:
stage: build
services:
- name: myclash:latest
alias: clash
before_script:
# 获取clash ip
- export CLASH_IP=$(grep 'clash' /etc/hosts | awk '{print $1}' | head -n 1)
script:
# 通过build-arg将clash的IP传入
- docker build --network host --no-cache --build-arg CLASH_IP=$CLASH_IP -t 10.0.24.9:5000/recite:latest .

在使用Dockerfile使用传入的CLASH_IP参数

1
2
3
4
5
6
7
8
9
10
FROM haskell:9.6.5 as build

ARG CLASH_IP

ENV http_proxy "http://${CLASH_IP}:7890"
ENV HTTP_PROXY "http://${CLASH_IP}:7890"
ENV https_proxy "http://${CLASH_IP}:7890"
ENV HTTPS_PROXY "http://${CLASH_IP}:7890"

xxxx