使用 Git 建立工具鏈

Continuous Delivery 將於 2027 年 2 月 12 日在以下地區停止提供服務:au-sydca-torus-east。 Code Risk Analyzer 也將於該日於所有地區停止提供服務。 若某個區域未實際使用這些功能,該區域中的功能可能會提前停用,並停止接受新的實例。 了解更多

您可以使用包含 Git Repos and Issue Tracking 或 GitHub 工具整合的範本為起點,建立工具鏈,並將 Git 儲存庫 (repos) 加入其中。 或者,您可以從一個空的工具鏈或現有的工具鏈開始,然後在其中加入 Git Repos and Issue Tracking 或 GitHub 工具整合。

若要查看哪些工具鏈範本包含 Git Repos and Issue Tracking 或 GitHub 工具整合,請參閱 工具鏈可用性、範本及指導教學

使用主控台從具有 Git Repos and Issue Tracking 或 GitHub 的範本建立工具鏈

您可以使用範本作為起點來 建立工具鏈,其中包含 Git Repos and Issue Tracking 或 GitHub 工具整合。 進一步了解如何使用「IBM Cloud Garage Method」中的範本。

  1. 登入 IBM Cloud

  2. 在IBM Cloud控制台中,點選選單圖示漢堡圖示>平台自動化>工具鏈

  3. 在 DevOps 儀表板的工具鏈頁面上,按一下建立工具鏈

  4. 建立工具鏈頁面上,按一下工具鏈範本。

  5. 檢閱即將建立之工具鏈的圖表。 此圖會顯示每個工具整合在工具鏈中的生命週期階段。

  6. 檢閱工具鏈設定的預設資訊:

    • 在 IBM Cloud 中,可透過工具鏈名稱來識別工具鏈。 如果您要使用不同的名稱,請變更工具鏈的名稱。
    • 要在其中建立工具鏈的地區。 如果您要使用不同的地區,請從可用地區清單中選取它。
    • 用於建立工具鏈的資源群組。 若要使用其他資源群組,請從「可用資源群組」清單中選取。
    • 您的原始碼儲存庫的服務提供者,例如 GitHub 或 GitLab。 若要使用不同的來源提供者,請從可用儲存庫清單選取。
  7. 在「工具整合」區段中,選取您要配置給工具鏈的每一個工具整合。 如需配置工具整合的相關資訊,請參閱配置工具整合

  8. 按一下建立。 會自動執行數個步驟來設定工具鏈。 根據您選取的工具鏈範本,所設定的工具整合會有所不同。 例如,當您在 IBM Cloud Public 上建立「微服務」工具鏈時,會執行下列步驟:

    • 建立工具鏈。
    • 如果您已配置 Delivery Pipeline,則會建立及觸發管線。
    • 如果您已配置 Sauce Labs,則會設定工具鏈將 Sauce Labs 測試工作新增至管線。
    • 若您已設定「Event Notifications」,則工具鏈將設定為將事件通知傳送至您指定的 Event Notifications 服務。
    • 如果您已配置 PagerDuty,則會設定工具鏈將警示通知傳送至所指定的 PagerDuty 服務。
    • 如果您已配置 Slack,則會設定工具鏈將部署狀態通知傳送至所指定的 Slack 頻道。
    • 如果您已配置原始碼工具整合(例如 GitHub),則會將範例 GitHub 儲存庫複製到 GitHub 帳戶。

    現在您可以透過使用 Event Notifications 工具集成。IBM Cloud Event Notifications 是向 PagerDuty 和其他通訊渠道(如 Slack、電子郵件、SMS、推送通知、webhook、Microsoft® Teams, ServiceNow, 和 IBM Cloud Functions )發佈通知的首選方法。 有關使用 Event Notifications 的詳細資訊,請參閱 啟用工具鏈的事件通知

使用 Git Repos and Issue Tracking 或使用 API GitHub 從範本建立工具鏈

您只能使用主控台從範本建立工具鏈。 若要檢視使用主控台的步驟,請切換至 使用者介面 指示。

如需如何使用 API 而非使用範本來建立工具鏈的相關資訊,請參閱 使用 API 將 Git 工具整合新增至現有工具鏈

使用 Git Repos and Issue Tracking 或使用 Terraform GitHub 從範本建立工具鏈

您只能使用主控台從範本建立工具鏈。 若要檢視使用主控台的步驟,請切換至 使用者介面 指示。

如需如何使用 Terraform 而非使用範本來建立工具鏈的相關資訊,請參閱 將 Git 工具整合新增至使用 Terraform 的現有工具鏈

使用主控台將 Git 工具整合新增至現有工具鏈

您可以使用主控台,將 Git 工具整合新增至任何現有工具鏈。

  1. 登入 IBM Cloud
  2. 在 IBM Cloud 控制台中,點擊選單圖示(漢堡圖示),然後選擇「資源清單」。
  3. 從「資源」清單中,展開 開發人員工具 區段。
  4. 按一下您要新增 Git 工具整合的工具鏈,或建立工具鏈。 如需建立工具鏈的相關資訊,請參閱 建立工具鏈
  5. 在工具鏈的「概觀」頁面上,按一下 新增工具,然後選取 GitHubGit Repos and Issue Tracking,將這些工具整合新增至工具鏈。
  6. 視您選取的 Git 工具整合而定,您可以 配置 GitHub 工具整合,也可以配置 Git Repos and Issue Tracking 工具整合

使用 API 將 Git 工具整合新增至現有工具鏈

您可以使用 API 將 Git 工具整合新增至任何現有工具鏈。

  1. 取得 IAM 持有人記號。 或者,如果您使用 SDK,請 取得 IAM API 金鑰,並使用環境變數來設定用戶端選項。

    export CD_TOOLCHAIN_AUTH_TYPE=iam && \
    export CD_TOOLCHAIN_APIKEY={iam_api_key} && \
    export CD_TOOLCHAIN_URL=https://api.{region}.devops.cloud.ibm.com/toolchain/v2
    
  2. 判定您要在其中新增 Git 工具整合之工具鏈 的地區及 ID。

  3. 將 Git 工具整合新增至工具鏈。 下列範例顯示如何新增 Git 工具整合,以鏈結至 github.com上的現有儲存庫。 您也可以使用指示來新增 Git 工具整合,以建立、分出或複製新的儲存庫。 您也可以為其他GitHub, GitLab,或 Bitbucket 伺服器新增工具整合。

    curl -X POST \
      https://api.{region}.devops.cloud.ibm.com/toolchain/v2/toolchains/{toolchain_id}/tools \
      -H 'Authorization: Bearer {token}' \
      -H 'Accept: application/json` \
      -H 'Content-Type: application/json' \
        -d '{
        "tool_type_id": "githubconsolidated",
        "name": "{tool_integration_name}",
        "parameters": {
          "type": "link",
          "git_id": "github",
          "repo_url": "https://github.com/{git_org}/{git-repo}.git",
          "has_issues": true
        }
      }'
    
    const CdToolchainV2 = require('@ibm-cloud/continuous-delivery/cd-toolchain/v2');
    const toolchainService = CdToolchainV2.newInstance();
    ...
    (async() => {
       const gitToolModel = {
          toolchainId: {toolchain_id},
          toolTypeId: 'githubconsolidated',
          name: {tool_integration_name},
          parameters: {
             type: 'link',
             git_id: 'github',
             repo_url: 'https://github.com/{git_org}/{git-repo}.git',
             has_issues: true,
          }
       };
       const gitTool = await toolchainService.createTool(gitToolModel);
    })();
    
    import (
    	   "github.com/IBM/continuous-delivery-go-sdk/cdtoolchainv2"
    )
    ...
    toolchainClientOptions := &cdtoolchainv2.CdToolchainV2Options{}
    toolchainClient, err := cdtoolchainv2.NewCdToolchainV2UsingExternalConfig(toolchainClientOptions)
    createGitToolOptions := toolchainClient.NewCreateToolOptions({toolchain_id}, "githubconsolidated")
    gitParameters := map[string]interface{}{
     "type": "link",
     "git_id": "github",
     "repo_url": "https://github.com/{git_org}/{git-repo}.git",
     "has_issues": true,
    }
    createGitToolOptions.SetName({tool_integration_name})
    createGitToolOptions.SetParameters(gitParameters)
    gitTool, response, err := toolchainClient.CreateTool(createGitToolOptions)
    
    from ibm_continuous_delivery.cd_toolchain_v2 import CdToolchainV2
    ...
    toolchain_service = CdToolchainV2.new_instance()
    git_parameters = {
       "type": "link",
       "git_id": "github",
       "repo_url": "https://github.com/{git_org}/{git-repo}.git",
       "has_issues": True
    }
    git_tool = toolchain_service.create_tool(
       name = {tool_integration_ame},
       toolchain_id = {toolchain_id},
       tool_type_id = "githubconsolidated",
       parameters = git_parameters
    )
    
    import com.ibm.cloud.continuous_delivery.cd_toolchain.v2.CdToolchain;
    import com.ibm.cloud.continuous_delivery.cd_toolchain.v2.model.*;
    ...
    CdToolchain toolchainService = CdToolchain.newInstance();
    HashMap<String, Object> gitParameters = new HashMap<>();
    gitParameters.put("type", "link");
    gitParameters.put("git_id", "github");
    gitParameters.put("repo_url", "https://github.com/{git_org}/{git-repo}.git");
    gitParameters.put("has_issues", true);
    CreateToolOptions createGitToolOptions = new CreateToolOptions.Builder()
       .name({tool_integration_name})
       .parameters(gitParameters)
       .toolchainId({toolchain_id})
       .toolTypeId("githubconsolidated")
       .build();
    Response<ToolchainToolPost> response = toolchainService.createTool(createGitToolOptions).execute();
    ToolchainToolPost gitTool = response.getResult();
    

下表列出並說明先前步驟中使用的每一個變數。

用於新增Git工具與 API 整合的變數
變數 說明
{git_org} github.com 上包含儲存庫的組織。
{git_repo} github.com 上要整合至工具鏈的儲存庫。
{iam_api_key} 您的 IAM API 金鑰。
{region} 工具鏈所在的地區。 例如,us-south
{tool_integration_name} 工具整合的名稱。
{toolchain_id} 要新增工具整合的工具鏈 ID。
{token} 一個有效的 IAM 承載者憑證。

如需每一個 Git 工具整合的相關資訊 (包括您可以配置的其他參數),請參閱下列主題:

使用 Terraform 將 Git 工具整合新增至現有工具鏈

您可以使用 Terraform 將 Git 工具整合新增至任何現有工具鏈。

  1. 若要安裝 Terraform CLI 並配置 Terraform 的 IBM Cloud 提供者外掛程式,請遵循 開始使用 Terraform on IBM Cloud 的指導教學。

  2. 找出包含您要新增 Git 工具整合之工具鏈的資源區塊的 Terraform 檔案 (例如,main.tf)。 在此檔案中,新增配置以建立工具整合。

    下列範例會建立工具鏈 (如果它不存在的話),然後使用 ibm_cd_toolchain_tool_githubconsolidated 資源來新增鏈結至 github.com 上現有儲存庫的 Git 工具整合。 您也可以使用指示來新增 Git 工具整合,以建立、分出或複製新的儲存庫。 您也可以為其他GitHub, GitLab,或 Bitbucket 伺服器新增工具整合。

    data "ibm_resource_group" "group" {
      name = "default"
    }
    resource "ibm_cd_toolchain" "cd_toolchain" {
      name              = "my toolchain"
      resource_group_id = data.ibm_resource_group.group.id
    }
    resource "ibm_cd_toolchain_tool_githubconsolidated" "my-github-tool-integration" {
      toolchain_id = ibm_cd_toolchain.cd_toolchain.id
      initialization {
        git_id = "github"
        repo_url = "https://github.com/my-org/my-repo.git"
        type = "link"
        auth_type = "pat"
        api_token = "<TOKEN>"
      }
      parameters {
        has_issues = true
      }
    }
    

    其中 <TOKEN> 代表屬於 GitHub 帳戶的「個人存取記號 (PAT)」,該帳戶會授與對目標儲存庫的存取權。

  3. 起始設定 Terraform CLI。

    terraform init
    
  4. 建立 Terraform 執行計劃。 此計劃彙總必須執行以將 DevOps Insights 工具整合新增至工具鏈的所有動作。

    terraform plan
    
  5. 套用 Terraform 執行計畫。 Terraform 會採取所有必要動作,以將 DevOps Insights 工具整合新增至工具鏈。

    terraform apply
    

如需每一個 Git 工具整合資源 (包括您可以配置的其他參數) 的相關資訊,請參閱 Terraform Registry 文件中的引數參照詳細資料:

如需搭配使用 Terraform 與 Continuous Delivery的相關資訊,請參閱 設定 Continuous Delivery