docker部署gitlab,gitlab-runner

2023-09-27  本文已影响0人  风亡小窝

官方文档

https://docs.gitlab.cn/jh/install/docker.html

部署环境

Windows11 wsl2 ubuntu22.04
Docker Desktop

部署gitlab

docker compose 部署

version: '3.6'
services:
  web:
    image: 'registry.gitlab.cn/omnibus/gitlab-jh:latest'
    restart: always
    hostname: 'gitlab-example'
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://192.168.0.132' #关键字段,runner得能访问才行
        # Add any other gitlab.rb configuration here, each on its own line
    ports:
      - '80:80'
      - '443:443'
      - '22:22'
    volumes:
      - '$GITLAB_HOME/config:/etc/gitlab'
      - '$GITLAB_HOME/logs:/var/log/gitlab'
      - '$GITLAB_HOME/data:/var/opt/gitlab'
    shm_size: '256m'

启动实际会比较久
docker compose -f docker-compose.yaml up -d
加上-d后台启动,不然会看到一长串的日志
启动后访问 external_url:映射的port
如果是学习搭建,external_url 改成 http,不启用https,不然会遇到很多问题:git clone失败,git默认校验证书,因为gitlab使用的是自签证书,校验不通过

部署gitlab-runner

docker run -d --name gitlab-runner --restart always \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \ #windows wsl2没有这个文件
  gitlab/gitlab-runner:latest

注意虽然在windows wsl2下没有/var/run/docker.sock:/var/run/docker.sock,但似乎它已自动确定为Windows环境中Docker Machine的路径

上述命令中,-v /var/run/docker.sock:/var/run/docker.sock 部分将宿主机上的 /var/run/docker.sock 文件挂载到 GitLab Runner 容器内的相同路径。
通过这种方式,GitLab Runner 容器内的 Docker CLI 或 Docker SDK 将能够与宿主机上的 Docker 引擎进行通信。

注册runner

docker run --rm -it \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  gitlab/gitlab-runner:latest register
# 进入容器内部并执行注册命令
root@runner:~# docker run --rm -it \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  gitlab/gitlab-runner:latest register

Runtime platform                                    arch=amd64 os=linux pid=37 revision=bbcb5aba version=15.3.0
Running in system-mode.

# 填写gitlab的地址,确保容器内能够顺利访问到该地址
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://192.168.0.132
# 填写需要注册的runner token,此处选择shared runner token
Enter the registration token:
BHRagrsztQAxdMPPhxGm
# 描述信息随意填写,也可不填
Enter a description for the runner:
[caa2879d6833]: gitlab-runner-01
# tag随意填写,此处不填
Enter tags for the runner (comma-separated):
# 随意填写,此处不填
Enter optional maintenance note for the runner:

Registering runner... succeeded                     runner=BHRagrsz
# 选择执行器的种类,建议选择docker
Enter an executor: docker, shell, ssh, kubernetes, custom, docker-ssh, parallels, virtualbox, docker+machine, docker-ssh+machine:
docker
# 使用最新版本
Enter the default Docker image (for example, ruby:2.7):
alpine:latest
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

项目中的 .gitlab-ci.yaml 文件

stages:
  - build

build:
  stage: build
  image: golang:latest
  script:
    - export GOPROXY=https://goproxy.cn
    - go version
    - go build -o myapp main.go
  artifacts:
    paths:
      - myapp
  tags:
      - mytag   #使用标签将选择标签匹配的runner

一些小提示

  1. .gitlab-ci.yaml stage 使用标签将选择标签匹配的runner
  2. runner标签可以在gitlab上修改
  3. runner可以配置成为跑没有标签的job
  4. gitlab不会主动连接runner,所以ping不通runner没有问题
  5. runner需要主动连接gitlab,所以runner需要能ping通gitlab
  6. 在配置runner的gitlab地址时,需要runner能够访问到gitlab,配置成gitlab的external_url最好
上一篇下一篇

猜你喜欢

热点阅读