文档说明: 只是记录关键点
基础软件: docker + gitlab
目标:自建代码仓库,并实现自动构建
自建gitlab
| version: "3" |
| services: |
| gitlab-ce: |
| image: gitlab/gitlab-ce:15.5.1-ce.0 |
| |
| 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-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
| |
| external_url 'https://gitlab.xiaoshuogeng.com' |
| |
| |
| gitlab_rails['gitlab_shell_ssh_port'] = 30022 |
| |
| |
| 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
参考文档
- docker hub gitlab
- docker hub gitlab tag
- gitlab docker 安装文档
- Registering runners
- gitlab 升级路径
- gitlab 升级参考
- 备份与恢复参考
- gitlab /etc/gitlab/gitlab.rb 配置
- 自建拉取registry.k8s.io、k8s.gcr.io、gcr.io、quay.io、ghcr.io 容器镜像的服务
- .gitlab-ci.yaml
- gitea with docker
- Drone 是一款开源的 CI/CD 工具
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术