[僅限PaaS]{class="badge informative" title="僅適用於雲端專案(Adobe管理的PaaS基礎結構)和內部部署專案的Adobe Commerce 。"}

GitLab整合

您可以設定GitLab存放庫,以便在推送程式碼變更時自動建置和部署環境。 這項整合會在雲端基礎結構帳戶上,將您的GitLab存放庫與Adobe Commerce同步。

NOTE
Adobe強烈建議您在雲端基礎結構專案上為Adobe Commerce使用私人存放庫,以保護任何專屬資訊或開發工作,例如擴充功能和敏感設定。

此整合可讓您:

  • 建立分支時建立環境
  • 合併提取請求時重新部署環境
  • 刪除分支時刪除環境

您必須取得GitLab權杖和webhook才能繼續此程式。

先決條件

  • 雲端基礎結構專案上Adobe Commerce的管理員存取權
  • 本機環境中的magento-cloud CLI工具
  • GitLab帳戶
  • 具有GitLab存放庫寫入存取許可權的GitLab個人存取權杖,選取範圍必須至少為: apiread_repository

準備您的存放庫

從現有環境複製雲端基礎結構專案上的Adobe Commerce,並將專案分支移轉至新的空GitLab存放庫,保留相同的分支名稱。 關鍵 ​在於要保留相同的Git樹狀結構,這樣您就不會遺失雲端基礎結構專案中Adobe Commerce的任何現有環境或分支。

  1. 從終端機,在雲端基礎結構專案上登入您的Adobe Commerce。

    code language-bash
    magento-cloud login
    
  2. 列出您的專案並複製專案ID。

    code language-bash
    magento-cloud project:list
    
  3. 將專案複製到您的本機環境。

    code language-bash
    magento-cloud project:get <project-id>
    
  4. 將您的GitLab存放庫新增為遠端(假設在其SaaS版本中使用GitLab)。

    code language-bash
    git remote add origin git@gitlab.com:<user-name>/<repo-name>.git
    

    遠端連線的預設名稱可以是originmagento。 如果origin存在,您可以選擇不同的名稱,或者重新命名或刪除現有的參照。 請參閱git-remote檔案

  5. 確認您已正確新增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)
    
  6. 將專案檔案推送至新的GitLab存放庫。 請記得保持所有分支名稱相同。

    code language-bash
    git push -u origin master
    

    如果您從新的GitLab存放庫開始,則可能必須使用-f選項,因為遠端存放庫不符合您的本機復本。

  7. 確認您的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>
雲端基礎結構專案ID上的Adobe Commerce
<your-GitLab-token>
您為GitLab產生的個人存取權杖
--base-url
GitLab的URL (如果其SaaS版本中使用GitLab,則為https://gitlab.com/
--server-project
GitLab中的專案名稱(基底URL之後的部分)
--build-merge-requests
選用 ​引數可指示Adobe Commerce在雲端基礎結構上為每個合併請求(預設為true)建立新環境
--merge-requests-clone-parent-data
選用 ​引數會指示Adobe Commerce在雲端基礎結構上複製合併請求(預設為true)的父系環境資料
--fetch-branches
一個​ 選用 ​引數,可讓雲端基礎結構上的Adobe Commerce從遠端(以非使用中環境的形式)擷取所有分支(預設為true
--prune-branches
選用 ​引數會指示雲端基礎結構上的Adobe Commerce刪除遠端上不存在的分支(預設為true
WARNING
magento-cloud integration命令會使用GitLab存放庫中的程式碼覆寫Adobe Commerce雲端基礎結構專案中的​_所有_​程式碼。 這包括所有分支,包括production分支。 此動作會立即發生且無法還原。 依據最佳做法的要求,在新增GitLab整合之前,請務必先在雲端基礎結構專案上複製Adobe Commerce中的所有分支,並將其推送至GitLab存放庫。

若要啟用GitLab整合

  1. 從終端機,將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
    
  2. 出現提示時,請輸入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
    
  3. 複製傳回輸出顯示的​ 連結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

  1. 在您的GitLab存放庫中,按一下「設定」標籤。

  2. 在左側導覽列中,按一下​ Webhooks

  3. 在​ Webhooks ​表單中,編輯下列欄位:

    • URL:請輸入啟用GitLab整合時傳回的Hook URL
    • 密碼權杖:視需要輸入驗證密碼。
    • 觸發器:視需要檢查Merge request events和/或Push events
    • 啟用SSL驗證:您必須選取此選項。
  4. 按一下​ 新增webhook

測試整合

設定GitLab整合後,您可以使用magento-cloud CLI來驗證整合運作中:

magento-cloud integration:validate

或者,您也可以推送簡單的變更至您的GitLab存放庫以進行測試。

  1. 建立測試檔案。

    code language-bash
    touch test.md
    
  2. 認可變更並將其推播至您的GitLab存放庫。

    code language-bash
    git add . && git commit -m "Testing GitLab integration" && git push
    
  3. 登入Cloud Console並確認您的認可訊息已顯示,且您的專案已部署。

建立雲端分支

使用magento-cloud CLI environment:push命令來建立和啟用新環境。 請參閱建立雲端分支

移除整合

使用magento-cloud CLI integration:delete命令移除整合。 請參閱移除整合

recommendation-more-help
7c2b03ac-000c-497d-aba3-2c6dc720a938