[仅限PaaS]{class="badge informative" title="仅适用于云项目(Adobe管理的PaaS基础架构)和内部部署项目上的Adobe Commerce 。"}
GitLab集成
您可以配置GitLab存储库,以便在推送代码更改时自动构建和部署环境。 此集成会将您的GitLab存储库与云基础架构帐户上的Adobe Commerce同步。
通过此集成,您可以:
- 创建分支时创建环境
- 合并拉取请求时重新部署环境
- 删除分支时删除环境
您必须获取GitLab令牌和Webhook才能继续此过程。
先决条件
- 对云基础架构项目上Adobe Commerce的管理员访问权限
- 本地环境中的
magento-cloudCLI工具 - GitLab帐户
- GitLab个人访问令牌具有对GitLab存储库的写访问权限,所选范围必须至少为:
api和read_repository。
准备存储库
从现有环境克隆Adobe Commerce on cloud infrastructure项目,并将项目分支迁移到空的新GitLab存储库,并保留相同的分支名称。 关键是 要保留相同的Git树,以便您不会丢失云基础架构项目上的Adobe Commerce中的任何现有环境或分支。
-
从终端,登录到您的Adobe Commerce on cloud infrastructure项目。
code language-bash magento-cloud login -
列出您的项目并复制项目ID。
code language-bash magento-cloud project:list -
将项目克隆到本地环境。
code language-bash magento-cloud project:get <project-id> -
将GitLab存储库添加为远程存储库(假设在其SaaS版本中使用GitLab)。
code language-bash git remote add origin git@gitlab.com:<user-name>/<repo-name>.git远程连接的默认名称可以是
origin或magento。 如果origin存在,则可以选择其他名称,也可以重命名或删除现有引用。 请参阅git-remote文档。 -
验证是否正确添加了GitLab遥控器。
code language-bash git remote -v预期响应:
code language-none origin git@gitlab.com:<user-name>/<repo-name>.git (fetch) origin git@gitlab.com:<user-name>/<repo-name>.git (push) -
将项目文件推送到新的GitLab存储库。 请记住保持所有分支名称相同。
code language-bash git push -u origin master如果您从新的GitLab存储库开始,则可能需要使用
-f选项,因为远程存储库与您的本地副本不匹配。 -
验证GitLab存储库是否包含所有项目文件。
启用GitLab集成
使用magento-cloud integration命令启用GitLab集成,并获取GitLab webhook的有效负荷URL,以将更新从GitLab发送到云基础架构项目上的Adobe Commerce。
magento-cloud integration:add --type=gitlab --project=<project-ID> --token=<your-GitLab-token> [--base-url=<GitLab-url> --server-project=<GitLab-project> --build-merge-requests={true|false} --merge-requests-clone-parent-data={true|false} --fetch-branches={true|false} --prune-branches={true|false}]
<project-ID><your-GitLab-token>--base-urlhttps://gitlab.com/,如果在其版SaaS中使用GitLab)--server-project--build-merge-requeststrue)构建一个新环境--merge-requests-clone-parent-datatrue)--fetch-branchestrue)--prune-branchestrue)magento-cloud integration命令使用GitLab存储库中的代码覆盖Adobe Commerce on cloud infrastructure项目中的_所有_代码。 这包括所有分支,包括production分支。 此操作立即发生且无法撤消。 作为最佳实践,在添加GitLab集成之前,请务必在云基础架构项目中克隆Adobe Commerce中的所有分支,并将其推送到GitLab存储库。启用GitLab集成:
-
从终端,将GitLab集成添加到云基础架构项目上的Adobe Commerce:
code language-bash magento-cloud integration:add --type gitlab --project=3txxjf32gtryos --token=qVUfeEn4ouze7A7JH --base-url=https://gitlab.com/ --server-project=my-agency/project-name --build-merge-requests=false --merge-requests-clone-parent-data=false --fetch-branches=true --prune-branches=true -
出现提示时,输入
y以添加集成。code language-none Warning: adding a 'gitlab' integration will automatically synchronize code from the external Git repository. This means it can overwrite all the code in your project. Are you sure you want to continue? [y/N] y -
复制返回输出显示的 挂接URL。
code language-none Hook URL: https://eu-3.magento.cloud/api/projects/3txxjf32gtryos/integrations/eolmpfizzg9lu/hook Created integration eolmpfizzg9lu (type: gitlab) +----------------------------------+---------------------------------------------------------------------------------------+ | Property | Value | +----------------------------------+---------------------------------------------------------------------------------------+ | id | <integration-id> | | type | gitlab | | token | ****** | | base_url | https://gitlab.com/ | | project | my-agency/project-name | | fetch_branches | true | | prune_branches | true | | build_merge_requests | false | | merge_requests_clone_parent_data | false | | hook_url | https://eu-3.magento.cloud/api/projects/<project-id>/integrations/<integration-id>/hook | +----------------------------------+---------------------------------------------------------------------------------------+
在GitLab中添加webhook
要通过Cloud Git服务器传达事件(例如推送或合并请求),您必须为GitLab存储库创建webhook
-
在GitLab存储库中,单击 设置 选项卡。
-
在左侧导航栏中,单击 Webhooks。
-
在 Webhooks 表单中,编辑以下字段:
- URL:输入启用GitLab集成时返回的
Hook URL。 - 密码令牌:如果需要,请输入验证密码。
- 触发器:根据需要检查
Merge request events和/或Push events。 - 启用SSL验证:必须选择此选项。
- URL:输入启用GitLab集成时返回的
-
单击 添加webhook。
测试集成
配置GitLab集成后,您可以使用magento-cloud CLI验证集成是否可正常工作:
magento-cloud integration:validate
或者,您也可以通过将简单的更改推送到GitLab存储库来进行测试。
-
创建测试文件。
code language-bash touch test.md -
提交更改并将其推送到您的GitLab存储库。
code language-bash git add . && git commit -m "Testing GitLab integration" && git push -
登录到Cloud Console并验证是否显示提交消息以及是否部署项目。
创建云分支
使用magento-cloud CLI environment:push命令创建和激活新环境。 请参阅创建云分支。
删除集成
使用magento-cloud CLI integration:delete命令删除集成。 请参阅删除集成。