快速自建gitlab和gitlab-ci容器版

文档说明: 只是记录关键点
基础软件: docker + gitlab
目标:自建代码仓库,并实现自动构建

自建gitlab

version: "3"
services:
    gitlab-ce:
        image: gitlab/gitlab-ce:15.5.1-ce.0  # 指定版本比较好
        #    image:  gitlab/gitlab-ce:latest 
        restart: always
        hostname: gitlab
        container_name: gitlab
        volumes:
            - ./etc:/etc/gitlab
            - ./logs:/var/log/gitlab
            - ./data:/var/opt/gitlab
        ports:
            - "80:80"
            - "443:443"
            - "22:22"

自建gitlab-ci

version: "3"
services:
    gitlab-runner:
        image: gitlab/gitlab-runner:v15.5.0
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
            - ./config:/etc/gitlab-runner
        container_name: gitlab-runner
        restart: always

自建gitlab-ci 配置文件关键点

自动推送构建好的容器,并自动部署
原理 docker:dind ( Docker In Docker )
配置文件: /etc/gitlab-runner/config.toml

[runners.docker]
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache",  "/root/.kube/config:/root/.kube/config:ro","/data/:/data/","/root/.docker/config.json:/root/.docker/config.json:ro"]


启动、关闭、查看日志

# 启动
docker-compose -f docker-compose.yaml up -d 
# 关闭
docker-compose -f docker-compose.yaml down --remove-orphans
# 查看日志
docker-compose -f docker-compose.yaml logs -f 

gitlab修改root密码

# 进入容器
docker exec -it gitlab bash 

# 进入gitlab console 

gitlab-rails console

user = User.find_by_username 'root'

new_password = ::User.random_password

user.password = new_password
user.password_confirmation = new_password

user.send_only_admin_changed_your_password_notification!

user.save

gitlab gitlab.rb 配置

文件位置: /etc/gitlab/gitlab.rb

配置参考文档 https://docs.gitlab.com/omnibus/settings/nginx.html

# 外部使用https
external_url 'https://gitlab.xiaoshuogeng.com'

# 设置 git clone 端口(webIDE显示这个端口和clone时也是这个端口,容器内部实际还是使用22端口)
gitlab_rails['gitlab_shell_ssh_port'] = 30022

# gitlab 内部不启用https ,外部使用 https 
nginx['listen_port'] = 80
nginx['listen_https'] = false

gitlab-ci 注册

# 进入容器
docker exec -it gitlab-runner bash 

gitlab-runner register
# 后续按照提示操作即可

gitlab-ci CI/CD 应用到项目的配置文件例子:

.gitlab-ci.yml

image: alpine:latest

before_script:
    - uname -a
    - cat /etc/os-release
    - sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
    - apk update && apk add  ca-certificates
    - apk add python3 python3-dev python3-pip


stages:
    - code-format-check-stage-01
    - build-stage-02
    - deploy-stage-03

code-format-check:
    stage: code-format-check
    before_script:
        - pwd
    script:
        - pwd && WORKSPACE=`pwd`
        - date
        - mkdir /root/.pip/
        - cp pip.conf /root/.pip/
        - apk add python3
        - pip3 install requests
        - python3 -u check_code.py
        - echo '检查代码格式等操作'

build:
    stage: build-stage-02
    script:
        - pwd && WORKSPACE=`pwd`
        - date
        - mkdir /root/.pip/
        - cp pip.conf /root/.pip/
        - apk add python3
        - pip3 install requests
        - echo '执行构建构建'
    artifacts:
        name: "$CI_JOB_NAME"
        paths:
            - dist.zip
        when: on_success
        expire_in: 1 week
    except:
        - dev
deploy:
    stage: code-format-check
    dependencies: [ 'build-stage-02' ]
    script:
        - pwd && WORKSPACE=`pwd`
        - date
        - unzip -d dist dist.zip
        - cp -rf dist/* /usr/share/nginx/html/
        - echo '部署完成'




觉得以上方案过重,可以使用 gitea + drone

参考文档

  1. docker hub gitlab
  2. docker hub gitlab tag
  3. gitlab docker 安装文档
  4. Registering runners
  5. gitlab 升级路径
  6. gitlab 升级参考
  7. 备份与恢复参考
  8. gitlab /etc/gitlab/gitlab.rb 配置
  9. 自建拉取registry.k8s.io、k8s.gcr.io、gcr.io、quay.io、ghcr.io 容器镜像的服务
  10. .gitlab-ci.yaml
  11. gitea with docker
  12. Drone 是一款开源的 CI/CD 工具
posted @ 2022-10-25 16:39  jingjingxyk  阅读(107)  评论(0编辑  收藏  举报