{"id":13813450,"url":"https://github.com/huiyonghkw/gitlab-ci-docker","last_synced_at":"2026-02-14T16:14:29.502Z","repository":{"id":39811535,"uuid":"90933945","full_name":"huiyonghkw/gitlab-ci-docker","owner":"huiyonghkw","description":"Quickly build a set of Gitlab CI / CD services based on Docker","archived":false,"fork":false,"pushed_at":"2019-10-31T04:20:48.000Z","size":1452,"stargazers_count":302,"open_issues_count":0,"forks_count":61,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-08-04T04:04:07.152Z","etag":null,"topics":["aliyun","aliyun-docker","docker","docker-gitlab","gitlab"],"latest_commit_sha":null,"homepage":"","language":"Dockerfile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/huiyonghkw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-11T03:36:44.000Z","updated_at":"2024-08-04T04:04:07.153Z","dependencies_parsed_at":"2022-09-13T17:14:27.814Z","dependency_job_id":null,"html_url":"https://github.com/huiyonghkw/gitlab-ci-docker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huiyonghkw%2Fgitlab-ci-docker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huiyonghkw%2Fgitlab-ci-docker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huiyonghkw%2Fgitlab-ci-docker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huiyonghkw%2Fgitlab-ci-docker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/huiyonghkw","download_url":"https://codeload.github.com/huiyonghkw/gitlab-ci-docker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225319230,"owners_count":17455730,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aliyun","aliyun-docker","docker","docker-gitlab","gitlab"],"created_at":"2024-08-04T04:01:18.196Z","updated_at":"2026-02-14T16:14:29.466Z","avatar_url":"https://github.com/huiyonghkw.png","language":"Dockerfile","funding_links":[],"categories":["Dockerfile"],"sub_categories":[],"readme":"# 基于Docker快速搭建Gitlab与Gitlab CI/CD服务\n\n`更新推荐` [使用Gitlab Runner 部署Laravel 项目](https://segmentfault.com/a/1190000020868208)\n\n此文档基于现有项目运行实践整理，其中包含了许多Gitlab、CI/CD相关概念，阅读需要有一定的Gitlab、Docker、CI/CD基础知识。\n\n## 目录\n- [此项目能做什么](#此项目能做什么)\n- [Git-Flow/Github-Flow/Gitlab-Flow工作流](#Git-Flow/Github-Flow/Gitlab-Flow工作流)\n- [快速运行Gitlab](#快速运行Gitlab)\n- [持续集成/持续交互](#持续集成/持续交互)\n- [Gitlab-Runner自动构建与部署的执行](#Gitlab-Runner自动构建与部署的执行)\n    - [获取注册参数 ](#获取注册参数 )\n    - [注册Gitlab-Runner](#注册Gitlab-Runner)\n- [CI/CD运行流程](#CI/CD运行流程)\n- [CI/CD分布式](#CI/CD分布式)\n\n\n此项目能做什么\n------\n\n1. 分享一些关于团队Git版本控制使用心得。\n2. 提供一份Docker版本的Gtilab私有化仓库应用实现，让你在开发团队内部快速构建一套Git托管仓库系统。\n3. 提供一份Gitlab Docker容器编排实现参考，分离Gitlab/Redis/PostgreSQL/Gitlab-CI和运行时数据。\n4. 提供一份Gitlab Runner Docker版本实现，在Runner容器内部增加了对PHP的支持，利用[Laravel Envoy](https://github.com/laravel/envoy)实现远程操作多台主机，实现分布式自动构建与交付。\n5. 加深对软件从编码 =》 构建 =》 测试 =》 部署的整个生命周期的认知，\n6. 为Web/Web API类型互联网产品自动化提供参考，适合中小型创业研发团队快速迭代版本。\n\nGit-Flow/Github-Flow/Gitlab-Flow工作流\n------\n\n\n\nGit团队开发中，大家都在向仓库中提交功能代码，时间久了，分支与版本就逐渐增多而变得复杂，因此，一个合理的版本管理流程在项目初期就非常重要。\n\n使用Git，多数时候推荐流行的git-flow工作流程 [git-flow 备忘清单](https://danielkummer.github.io/git-flow-cheatsheet/index.zh_CN.html)。通常，develop分支作为测试版本使用，所有开发者都能够向该分支合并代码，以保证代码经过评审和功能验证。master分支作为线上版本使用，只有仓库管理员才能向该分支合并代码，以确保线上版本稳定。\n\n![](https://docs.gitlab.com/ce/workflow/gitdashflow.png)\n\n+ **feature branches**  功能特性分支，基于develop分支开发，在功能开发完成后，代码会被合并到develop分支，此分支也自动删除。\n+ **develop** 测试分支，一般基于该分支构建应用的测试环境。\n+ **release branches**  发布分支。新功能开发完成，经过了代码评审和功能验证后，仓库管理员使用`git flow release start 1.0.0` 命令方式合并develop分支到master分支。使用该命令，需要确保master分支与develop分支基础版本一致。\n+ **hotfixes** 修复分支，一般而言，在线上环境出现问题，需要紧急修复时，需创建hotfix修复补丁分支，它基于master分支开发，补丁修复代码会被合并至master与develop分支。\n+ **master** 生产分支，一般基于该分支中构建应用的生产环境。\n\n![](https://danielkummer.github.io/git-flow-cheatsheet/img/git-flow-commands.png)\n\n\n\n在单人Git开发场景下，此流程足以满足需求，但是在多人的开发团队中，有时候稍显\"[无力](https://about.gitlab.com/2014/09/29/gitlab-flow/)\"，而Gitlab/Github推荐使用PR/MR的方式，开发者需要主动发起分支合并申请，在得到确认后，分支开发才算完成。\n\n开发者基于主干分支先创建新的功能性分支，在功能开发完成后，需要作Merge Request申请，也即是主动请求合并功能分支代码到主干分支。仓库管理员会评审每一次MR，只有评审通过，新功能才被提交上线。\n\n![](https://about.gitlab.com/images/git_flow/github_flow.png)\n\n快速运行Gitlab\n-----------\n\n\n\n\nGitlab提供社区与企业版本，[官网](https://about.gitlab.com/installation/)提供多种Gitlab安装方式。此项目是基于Docker版本的Gitlab实现，开发者无需更多关心Gitlab安装过程，Docker镜像已做好了环境和依赖。\n\n该项目基于[Docker GitLab](http://www.damagehead.com/docker-gitlab/) 镜像构建，它将Gitlab/PostgreSQL/Redis容器拆分，分离了运行时数据。同时，提供一套容器编排配置参考，支持Docker Compose快速构建与维护。\n\n确保你的主机已经安装好了Docker与Docke-Compose，按照下面的方式快速运行Gitlab。\n\n1. 克隆仓库\n\n   ```sh\n   $ git clone https://github.com/bravist/gitlab-docker\n   $ cd ~/gitlab-docker\n   ```\n\n2. 配置环境变量，推荐使用默认配置\n\n   ```sh\n   $ cp .env.example .env\n   ```\n\n3. 使用`docker-compose`构建镜像和启动容器\n\n   ```sh\n   $ docker-compose build \u0026\u0026 docker-compose up -d\n   ```\n\n4. 确认容器构建成功，Gitlab Web控制台访问：[http://localhost:10080/](http://localhost:10080/)，如果出现502错误，请稍后再次访问，容器构建Gitlab需要等待依赖安装完成。\n\n   ```bash\n   CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS              PORTS                                                   NAMES\n   8827363f45d8        sameersbn/gitlab:10.3.3           \"/sbin/entrypoint.sh…\"   4 seconds ago       Up 4 seconds        443/tcp, 0.0.0.0:10022-\u003e22/tcp, 0.0.0.0:10080-\u003e80/tcp   gitlabdocker_gitlab_1\n   db0dd3b3d23f        bravist/gitlab-ci-php-envoy:1.0   \"/usr/bin/dumb-init …\"   7 seconds ago       Up 6 seconds                                                                gitlab-ci-php-envoy-runner\n   f598844c561c        sameersbn/redis:latest            \"/sbin/entrypoint.sh…\"   7 seconds ago       Up 6 seconds        6379/tcp                                                gitlab-redis\n   73d93b7cd110        sameersbn/postgresql:9.6-2        \"/sbin/entrypoint.sh\"    7 seconds ago       Up 5 seconds        0.0.0.0:5432-\u003e5432/tcp                                  gitlab-postgresql\n   ```\n   项目内置了Gitlab Runner镜像，默认会启动一台Gitlab Runner容器，让你在本地可以快速搭建CI/CD服务。\n\n持续集成/持续交互\n------\n\n\nCI/CD 的全称是 Continuous Integration \u0026 Deployment (持续集成/部署)，是 extreme programming (极限编程) 的一部分。我们常用 CI 来做一些自动化工作，这种自动化工作会运行在一台集中的机器上，比如程序的打包，单元测试，部署等。 [维基百科  - 持续集成](https://zh.wikipedia.org/wiki/%E6%8C%81%E7%BA%8C%E6%95%B4%E5%90%88)\n\n![Gitlab CI](https://images-cdn.shimo.im/I2kB4mczcgYkeQSB/cicd_pipeline_infograph.png!thumbnail)\n\nGitlab 从 8.* 版本后支持CI/CD，要使用CI/CD，先理解一些概念\n\n**Pipeline**\n\n一次[Pipeline](https://docs.gitlab.com/ce/ci/pipelines.html)就是一次完整的构建任务，里面可以包含多个阶段（[stages](https://docs.gitlab.com/ce/ci/yaml/README.html#stages)）。\n\n**Stage**\n\nStages表示任务构建的阶段。一次Pipeline中允许定义多个 Stages，这些 Stages 会有以下特点：\n\n- 所有 Stages 会按照顺序运行，即当一个 Stage 完成后，下一个 Stage 才会开始\n- 只有当所有 Stages 完成后，该构建任务 (Pipeline) 才会成功\n- 如果任何一个 Stage 失败，那么后面的 Stages 不会执行，该构建任务 (Pipeline) 失败\n\n**Job**\n\nJobs 表示构建工作，表示某个 Stage 里面执行的工作。\n我们可以在 Stages 里面定义多个 Jobs，这些 Jobs 会有以下特点：\n\n- 相同Stage中的Jobs会并行执行\n- 相同Stage中的Jobs都执行成功时，该 Stage 才会成功\n- 如果任何一个Job失败，那么该 Stage 失败，即该构建任务 (Pipeline) 失败\n\n我把[Pipelines](https://docs.gitlab.com/ce/ci/pipelines.html)理解为流水线，流水线包含有多个阶段（[stages](https://docs.gitlab.com/ce/ci/yaml/README.html#stages)），每个阶段包含有一个或多个工序（[jobs](https://docs.gitlab.com/ce/ci/yaml/README.html#jobs)），比如先购料、组装、测试、包装再上线销售，每一次Push或者MR都要经过流水线之后才可以合格出厂。\n\n![Gitlab-CI](Gitlab-CI.png)\n\nGilab中，仓库的.gitlab.ci.yml文件负责定义项目具体工作任务和流水线，Gitlab Runner基于配置好的流水线逐步执行任务，直到任务完成，如果中途错误，它会立即停止后续的任务。如果项目使用Gitlab CI，需要提前添加该文件。\n\n```shell\nstages:\n  - pull_code_test\n  - pull_code_production\n  - install_deps\n  - test\n  - build\n  - deploy_test\n  - deploy_production\nvariables:\n  PHP_FPM_CONTAINER: lnmp-php-fpm\n  WORK_DIR: /usr/share/nginx/html/\n  PROJECT: laravel-demo\n  GIT_DIR: /mnt/lnmp-docker\n# 拉取代码\npull_code_test:\n  stage: pull_code_test\n  only:\n    - develop\n  script:\n     - cd ${GIT_DIR}/${PROJECT}\n     - git pull origin develop\npull_code_production:\n  stage: pull_code_production\n  only:\n    - master\n  script:\n    - cd ${GIT_DIR}/${PROJECT}\n    - git pull origin master\n# 安装依赖\ninstall_deps:\n  stage: install_deps\n  script:\n    - docker exec -w ${WORK_DIR}/${PROJECT} ${PHP_FPM_CONTAINER} composer install\nbuild:\n  stage: build\n  script:\n    # Run migrations\n    - docker exec -w ${WORK_DIR}/${PROJECT} ${PHP_FPM_CONTAINER} php artisan migrate\n    # Cache clearing\n    - docker exec -w ${WORK_DIR}/${PROJECT} ${PHP_FPM_CONTAINER} php artisan cache:clear\n    # Create a cache file for faster configuration loading\n    - docker exec -w ${WORK_DIR}/${PROJECT} ${PHP_FPM_CONTAINER} php artisan config:cache\n    # Create a route cache file for faster route registration\n    - docker exec -w ${WORK_DIR}/${PROJECT} ${PHP_FPM_CONTAINER} php artisan route:clear\ndeploy_test:\n  stage: deploy_test\n  script:\n    - cd ${GIT_DIR}\n    - docker-compose down \u0026\u0026 docker-compose build \u0026\u0026 docker-compose up -d\ndeploy_production:\n  stage: deploy_production\n  script:\n    - cd ${GIT_DIR}\n    - docker-compose restart\n```\n\n\n\nGitlab-Runner自动构建与部署的执行\n------\n\n\nGitLab Runner 是一个开源项目， 它用来运行你定制的任务（jobs）并把结果返回给 GitLab。 GitLab Runner 配合[GitLab CI](https://about.gitlab.com/gitlab-ci)（GitLab 内置的持续集成服务） 协调完成任务。 [GitLab Runner](https://docs.gitlab.com.cn/runner/)\n\n简单的说，Gitlab Runner是独立运行配置好了Gitlab CI/CD项目的机器，它负责Gitlab项目的自动构建和部署的运行。\n\nGitlab Runner安装有多种方式，具体可以参考[官方文档](https://docs.gitlab.com.cn/runner/install/index.html)，这里推荐使用基于Docker的版本 [bravist/gitlab-ci-php-envoy](https://hub.docker.com/r/bravist/gitlab-ci-php-envoy/)，镜像源码已同步在此项目中，上面的章节也能看到运行的容器。\n\nGitlab Runner安装运行之后，需要[注册到Gitlab项目中去](https://docs.gitlab.com.cn/runner/register/index.html)，才能使用其“功效”。\n\n获取注册参数\n------\n\n\n登录Gitlab控制台，选择一个Gitlab仓库，进入 Settings =》CI / CD =》Runners settings =》Expand，获取Runners相关参数。\n\n![Gitlab Runner Register Parameters](./Gitlab-Runner-Register.png)\n\n注册Gitlab-Runner\n------\n\n\n```shell\n$ docker exec -it gitlab-ci-php-envoy-runner gitlab-ci-multi-runner register\n\nRunning in system-mode.\n\n#输入CI URL\nPlease enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):\nhttp://gitlab.weipeiapp.com:10080/\n\n#输入CI Token\nPlease enter the gitlab-ci token for this runner:\neU9zcqjReSozw6a1RLL8\n\n#输入描述信息\nPlease enter the gitlab-ci description for this runner:\n[f6c7de92c743]: Gitlab CI usage\n\n#输入标签信息\nPlease enter the gitlab-ci tags for this runner (comma separated):\nv1.0\n\n#是否运行未标记的版本\nWhether to run untagged builds [true/false]:\n[false]: true\n\n#是否运行当前项目\nWhether to lock the Runner to current project [true/false]:\n[true]: true\n\nRegistering runner... succeeded                     runner=eU9zcqjR\n\n#输入Runner的类型\nPlease enter the executor: shell, virtualbox, docker-ssh+machine, kubernetes, docker, docker-ssh, parallels, ssh, docker+machine:\nshell\n\nRunner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!\n```\n\n注册成功后，页面会多出一个正在运行的Runner绿色标志。\n\nCI/CD运行流程\n------\n\n\n如果你了解[bravist/lnmp-docker](https://github.com/bravist/lnmp-docker)，对于下面的图不会陌生，主要提供了一套Web应用自动构建与交付流程架构。\n\n通常情况下Gitlab Runner与Gitlab无需运行在同一台服务器，他们之间通信基于授信TOKEN，整个运行流程是：\n\n1. 基于Gitlab Flow 完成功能开发   \n2. 提交Merge Request，等待功能发布申请\n3. 评审Merge Request，允许合并分支请求\n4. 触发Gitlab Runner运行\n5. 执行项目 .gitlab.ci.yml 配置好的流水线任务\n6. 完成CI/CD流水线工作，工作结束\n\n以一个前端项目为例，构建过程会做以下任务\n\n+ 拉取Git代码\n+ 安装前端依赖\n+ 打包、编译\n\n![Usage-Docker](./Usage-Docker.png)\n\nCI/CD分布式\n------\n\n\n本项目Gitlab Runner默认采用Shell方式执行部署任务，也即是Runner使用SSH登录到应用主机。你可以在Runner容器中生成新的ssh秘钥对（或者使用能够登录到应用主机的其他ssh秘钥对），确保能成功SSH到应用主机。\n\n进入到Runner容器\n\n```shell\ndocker exec -it  gitlab-ci-php-envoy-runner sh\n```\n\n使用ssh登录远程应用主机\n\n```shell\nssh root@47.*.*.69\n```\n\n如果成功登录，表示自动部署链接工作没有问题。如果你的应用部署了多台服务器，推荐使用[Larave Envoy](https://laravel.com/docs/5.6/envoy)部署任务。Laravel Envoy 基于PHP实现，提供了灵活的配置方便在应用主机构建任务。 [bravist/gitlab-ci-php-envoy](https://hub.docker.com/r/bravist/gitlab-ci-php-envoy/) Runner基于gitlab/gitlab-runner镜像，又增加了对PHP的支持，完全支持Laravel Envoy部署。\n\n要使用Laravel Envoy，需要在仓库中增加Envoy.blade.php文件，定义好所有主机需要做的事情。\n\n```shell\n@servers(['sandbox' =\u003e 'root@47.*.*.45', 'production' =\u003e 'root@47.*.*.69'])\n\n@story('sandbox_deploy', ['on' =\u003e ['sandbox']])\n  git\n  logistics-debt\n@endstory\n\n@story('production_deploy', ['on' =\u003e ['production']])\n  git\n  logistics-debt\n@endstory\n\n\n@task('git')\n  cd /mnt/lnmp-docker/www/{{ $project }}/\n  git config --global user.email \"chenghuiyong1987@gmail.com\"\n  git config --global user.name \"Gitlab Runner\"\n  git pull origin {{ $branch }}\n@endtask\n\n@task('logistics-debt')\n  cd /mnt/lnmp-docker/www/{{ $project }}/logistics-debt\n  cnpm install\n  npm run build\n@endtask\n\n```\n\n同时，在仓库的.gitlab-ci.yml文件中定义使用Envoy执行构建任务。\n\n```shell\nstages:\n  - sandbox\n  - production\n\nvariables:\n  ENVOY: /root/.composer/vendor/bin/envoy\n\nsandbox:\n   stage: sandbox\n   script:\n     - cd $CI_PROJECT_DIR\n     - $ENVOY run sandbox_deploy --branch=develop --project=$CI_PROJECT_NAME\n   only:\n    - develop\nproduction:\n  stage: production\n  script:\n     - cd $CI_PROJECT_DIR\n     - $ENVOY run production_deploy --branch=master --project=$CI_PROJECT_NAME\n  only:\n    - master\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuiyonghkw%2Fgitlab-ci-docker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhuiyonghkw%2Fgitlab-ci-docker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuiyonghkw%2Fgitlab-ci-docker/lists"}