Skip to content

部署 GitLab

介绍

GitLab 是一个基于 Git 版本控制系统的仓库管理工具,它提供了代码托管、问题跟踪、持续集成和部署等功能。GitLab 可以帮助团队协作开发,并提供了一套完整的工具来管理软件开发项目。简单点来说就是开源版本的 github, 你可以用它部署一个专你自己的 gitlab

docker-compose.yml

注: 因为gitlab是一个比较耗费资源的项目, 所以可能需要等一会才能访问成功, 可能会 502, 这是正常的

注: 部署成功后, 一定要修改root账户密码, 具体操作看这里

yml
services:
  gitlab-web:
    image: 'gitlab/gitlab-ce:15.11.13-ce.0' # v15.11.13
    container_name: gitlab-web
    restart: always
    hostname: 'gitlab.example.com'
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'https://gitlab.example.com'
        gitlab_rails['gitlab_shell_ssh_port'] = 2233
    ports:
      - '4433:443'
      - '2233:22'
    volumes:
      - './config:/etc/gitlab'
      - './logs:/var/log/gitlab'
      - './data:/var/opt/gitlab'
    shm_size: '256m'

  gitlab-runner:
    image: 'gitlab/gitlab-runner:ubuntu-v15.11.0'
    restart: always
    container_name: gitlab-runner
    depends_on:
      - gitlab-web
    privileged: true
    volumes:
      - ./gitlab-runner-config:/etc/gitlab-runner
      - /var/run/docker.sock:/var/run/docker.sock
      - /bin/docker:/bin/docker

部署 Gitea

介绍

功能小巧, 但是性能非常好, 占用资源少, 适合个人和小团队

docker-compose.yml

gitea 默认是没有 https 的, 可以使用 nginx proxy manager 来代理, 如果不想使用 postgres, 也可以 看文档修改对应配置

注: gitea 自带的cicd工具还在开发中, 如果要继承cicd的话, 还是建议使用第三方开源的工具, 如: drone, 如果不需要cicd, 只需要代码管理工具, 那么可以注释这个

yml
networks:
  gitea:
    external: false

services:
  gitea_server:
    image: gitea/gitea:1.21.2
    container_name: gitea
    hostname: giteaserver
    environment:
      USER_UID: 1000
      USER_GID: 1000
      GITEA__database__DB_TYPE: postgres
      GITEA__database__HOST: 'db:5432'
      GITEA__database__NAME: gitea          # 注意要和 db 的 environment 中填的一样
      GITEA__database__USER: admin
      GITEA__database__PASSWD: admin1314  
    restart: always
    depends_on:
      - db
    networks:
      - gitea
    volumes:
      - ./gitea_data:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "8000:3000"
      - "8001:22"

  db:
    image: postgres:14
    container_name: postgres
    restart: always
    hostname: postgres_server
    environment:
      POSTGRES_DB: gitea           # 数据库名
      POSTGRES_USER: admin         # 数据库用户
      POSTGRES_PASSWORD: admin1314 # 数据库密码
    networks:
      - gitea
    ports:
      - "8002:5432"
    volumes:
      - ./postgres_conf:/etc/postgresql
      - ./postgres_data:/var/lib/postgresql/data
yml
networks:
  gitea:
    external: false

services:
  gitea_server:
    image: gitea/gitea:1.21.2
    container_name: gitea
    hostname: giteaserver
    environment:
      USER_UID: 1000
      USER_GID: 1000
      GITEA__database__DB_TYPE: postgres
      GITEA__database__HOST: 'db:5432'
      GITEA__database__NAME: gitea          # 注意要和 db 的 environment 中填的一样
      GITEA__database__USER: admin
      GITEA__database__PASSWD: admin1314  
    restart: always
    depends_on:
      - db
    networks:
      - gitea
    volumes:
      - ./gitea_data:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "8000:3000"
      - "8001:22"

  db:
    image: postgres:14
    container_name: postgres
    restart: always
    hostname: postgres_server
    environment:
      POSTGRES_DB: gitea           # 数据库名
      POSTGRES_USER: admin         # 数据库用户
      POSTGRES_PASSWORD: admin1314 # 数据库密码
    networks:
      - gitea
    ports:
      - "8002:5432"
    volumes:
      - ./postgres_conf:/etc/postgresql
      - ./postgres_data:/var/lib/postgresql/data

  # for cicd
  drone:
    image: drone/drone:2
    container_name: drone
    restart: always
    hostname: droneserver
    depends_on:
      - db
    networks:
      - gitea
    ports:
      - '8003:80'
    volumes:
      - ./drone_config:/var/lib/drone
    environment:
      TZ: Asia/Shanghai
      DRONE_GITEA_SERVER: https://gitea.examples.cn                                       # gitea 部署的域名
      DRONE_GITEA_CLIENT_ID: c4d3610e-f3bb-4224-857f-fb27c7712345                         # gitea client id
      DRONE_GITEA_CLIENT_SECRET: gto_spqtexjlzwgyqx77pyq57hmye2r5qakeq6uug6uqliocxjm12345 # gitea client secret
      DRONE_RPC_SECRET: cbce82a5d5e5772b8daf59202c612345                                  # 与 runner 通信的 secret
      DRONE_SERVER_HOST: https://dronecicd.examples.cn                                    # drone 部署的域名
      DRONE_SERVER_PROTO: https                                                           # 注意协议
      DRONE_USER_CREATE: username:secret,admin:true                                       # 超级管理员账号
      DRONE_DATABASE_DRIVER: postgres                                                     # 使用这个文件定义的服务, 而不是镜像内部的 pgsql
      DRONE_DATABASE_DATASOURCE: postgres://admin:admin1314@postgres_server:5432/postgres?sslmode=disable

  # cicd runner
  drone-runner:
    image: drone/drone-runner-docker:1
    restart: always
    container_name: drone-runner
    depends_on:
      - drone
    networks:
      - gitea
    ports:
      - '8004:3000'
    environment:
      DRONE_RPC_HOST: droneserver                        # 注意需要在同一 network 下才可以使用 hostname
      DRONE_RPC_PROTO: http                              # 注意协议是 http, 因为现在走的是 hostname 所以不是 https
      DRONE_RPC_SECRET: cbce82a5d5e5772b8daf59202c612345 # runner 与 drone server 通信的 secret
      DRONE_UI_USERNAME: root
      DRONE_UI_PASSWORD: root
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
yml
networks:
  gitea:
    external: false

services:
  gitea_server:
    image: gitea/gitea:1.21.2
    container_name: gitea
    hostname: giteaserver
    environment:
      USER_UID: 1000
      USER_GID: 1000
      GITEA__database__DB_TYPE: postgres
      GITEA__database__HOST: 'db:5432'
      GITEA__database__NAME: gitea          # 注意要和 db 的 environment 中填的一样
      GITEA__database__USER: admin
      GITEA__database__PASSWD: admin1314  
    restart: always
    depends_on:
      - db
    networks:
      - gitea
    volumes:
      - ./gitea_data:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "8000:3000"
      - "8001:22"

  db:
    image: postgres:14
    container_name: postgres
    restart: always
    hostname: postgres_server
    environment:
      POSTGRES_DB: gitea           # 数据库名
      POSTGRES_USER: admin         # 数据库用户
      POSTGRES_PASSWORD: admin1314 # 数据库密码
    networks:
      - gitea
    ports:
      - "8002:5432"
    volumes:
      - ./postgres_conf:/etc/postgresql
      - ./postgres_data:/var/lib/postgresql/data

  # 生成配置文件 docker run --rm -it --entrypoint=""  gitea/act_runner:latest act_runner generate-config > ./config.yaml
  gitea-runner:
    image: gitea/act_runner:latest
    container_name: gitea-runner
    restart: always
    depends_on:
      - gitea_server
    networks:
      - gitea
    volumes:
      # 注意需要先生成配置文件,参考: https://docs.gitea.com/usage/actions/act-runner#register-the-runner
      - ./gitea_runner/config.yaml:/config.yaml
      - ./gitea_runner/data:/data
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      CONFIG_FILE: /config.yaml
      GITEA_INSTANCE_URL: https://gitlab.example.cn
      # 注意需要获取注册令牌,参考: https://docs.gitea.com/usage/actions/act-runner#obtain-a-registration-token
      GITEA_RUNNER_REGISTRATION_TOKEN: MpVnALiLOGPnqaJduHdPSc7Zjh2dm4XtmNi12345
      GITEA_RUNNER_NAME: default-runner

Released under the MIT License.