如何在私有 GitLab 上搭建 Satis 仓库并为 Magento 模块做版本分发?
解读
面试官真正想考察的是:
- 你是否理解 Composer 私有包分发的完整链路(生成元数据 → 仓库托管 → 认证 → CI 集成)。
- 能否把 Satis 与 GitLab 的 私有群组/项目、Deploy Token、CI Pipeline 结合起来,解决 Magento 多模块、多版本、多环境(dev/test/stage/prod)的依赖隔离问题。
- 是否具备落地细节意识:国内网络、GitLab Runner 缓存、Satis 增量更新、Composer 2 性能、Composer auth.json 加密存储、以及 Magento 2.4+ 对 Composer 2 的硬性要求。
知识点
- Satis 静态仓库原理:satis.json 声明仓库源,build 命令扫描 Git 标签 → 生成 packages.json 及 dist zip,纯静态 HTTP 即可服务。
- GitLab 私有项目访问协议:HTTPS(推荐,走 Deploy Token)与 SSH(需额外 key)。
- Deploy Token 与 Project Access Token:只读、不过期、可 IP 白名单,适合 CI 拉取;不可使用个人账号密码,避免离职风险。
- CI 触发策略:GitLab Pipeline 的 tag push 事件 → 自动执行 satis build → 把生成的 public/ 目录作为 GitLab Pages 或 Generic Package 发布。
- Composer 2 优化:satis 2.x 支持 “require-dependencies” 与 “archive” 并行生成,配合 “twig-template” 可自定义 packages.json 结构,减少 Magento 2 的 2000+ 包索引时间。
- 缓存与增量:satis build 加 “–skip-errors –repository-url” 参数,配合 GitLab Runner 的 cache:paths: – satis-cache/,实现分钟级增量。
- 国内加速:GitLab 服务器若在内网,可关闭 SSL 自签名校验(composer config secure-http false),或把 Satis 静态页托管到 阿里云 OSS + CDN,通过 私有 Bucket 签名 授权访问。
- Magento 侧接入:在 composer.json 新增 repositories.satis.type=“composer”,url 指向 Satis 的 packages.json 地址;auth.json 里配置 http-basic.{satis-domain} 的 Deploy Token,文件权限 600,并纳入 .gitignore。
- 多仓库合并:若公司同时用 Packagist 中国全量镜像 与私有 Satis,需调整 repositories 顺序,Satis 放首位,避免同名包被公网覆盖。
答案
-
准备 Satis 项目
在 GitLab 新建 空项目 satis-mirror,克隆到本地后 composer create-project composer/satis . 安装 2.x 版本。 -
编写 satis.json
{ "name": "company/satis", "homepage": "https://git.company.com/satis-mirror", "repositories": [ { "type": "git", "url": "https://git.company.com/magento-modules/product-bundles" }, { "type": "git", "url": "https://git.company.com/magento-modules/theme-hyva" } ], "require-all": false, "require": { "company/product-bundles": "*", "company/theme-hyva": "*" }, "archive": { "directory": "dist", "format": "zip", "skip-dev": true } } -
生成 Deploy Token
在 satis-mirror 及各模块项目里,Settings → Repository → Deploy Tokens,创建 read_repository 权限的令牌,记录用户名与密码(形如 satis-user → gldt-xxx)。 -
创建 .gitlab-ci.yml
stages: - build - deploy variables: SATIS_CACHE_DIR: "$CI_PROJECT_DIR/satis-cache" cache: paths: - satis-cache/ build: stage: build image: composer:2.2 script: - composer install --no-dev - bin/satis build satis.json public/ --skip-errors artifacts: paths: - public expire_in: 1 day pages: stage: deploy script: - echo 'Deploying to GitLab Pages' artifacts: paths: - public expire_in: never only: - tags打标签 v1.0.0 推送后,GitLab Pages 地址即为 **https://git.company.com/satis-mirror**,packages.json 可访问。
-
Magento 项目接入
在 Magento 根目录执行composer config repositories.satis composer https://git.company.com/satis-mirror composer config --auth http-basic.git.company.com satis-user gldt-xxx确认 auth.json 权限 600,随后
composer require company/product-bundles:^2.3若出现 “Could not authenticate”,检查 Deploy Token 是否启用 read_repository 及 IP 白名单。
-
增量更新与回滚
模块项目打新标签 v2.0.0 后,Satis CI 自动运行,packages.json 里新增版本;若需回滚,删除 Git 标签并重新触发 Satis Pipeline 即可。
拓展思考
- 多环境隔离:为 dev/stage/prod 分别建 satis-dev、satis-stage 项目,satis.json 的 require 字段用 @dev、@stable 区分,实现 “同一代码库,不同通道”。
- GraphQL 元数据:Satis 仅支持静态 JSON,若未来需要 动态搜索版本,可调研 Private Packagist 或 Repman.io,但需注意 成本与出口带宽。
- 安全加固:把 Satis 生成的 dist zip 上传到 阿里云 OSS 私有 Bucket,利用 STS 临时凭证 授权 Composer 下载,避免 GitLab Pages 公网泄露。
- CI 模板复用:将 satis.gitlab-ci.yml 做成 GitLab CI Component,所有 Magento 模块仓库 include 即可,一处更新、处处生效。
- 性能极限:当私有包数量 > 5000 时,Satis 全量扫描耗时 10 分钟以上,可改用 “providers-api” 模式,把 packages.json 拆分为 provider-includes,实现 懒加载,这是 Magento Commerce Cloud 官方推荐的 “Split Satis” 方案。