InstallSSHKey@0 - 安裝 SSH 金鑰 v0 工作

在管線中使用這項工作,在建置或發行步驟之前安裝 SSH 金鑰。

Syntax

# Install SSH key v0
# Install an SSH key prior to a build or deployment.
- task: InstallSSHKey@0
  inputs:
    knownHostsEntry: # string. Alias: hostName. Required. Known Hosts Entry. 
    #sshPublicKey: # string. SSH Public Key. 
    #sshPassphrase: # string. SSH Passphrase. 
    sshKeySecureFile: # string. Required. SSH Key. 
  # Advanced
    #addEntryToConfig: false # boolean. Add entry to SSH config. Default: false.
    #configHostAlias: # string. Required when addEntryToConfig = true. Alias. 
    #configHostname: # string. Required when addEntryToConfig = true. Host name. 
    #configUser: # string. Optional. Use when addEntryToConfig = true. User. 
    #configPort: # string. Optional. Use when addEntryToConfig = true. Port.
# Install SSH key v0
# Install an SSH key prior to a build or deployment.
- task: InstallSSHKey@0
  inputs:
    knownHostsEntry: # string. Alias: hostName. Required. Known Hosts Entry. 
    #sshPublicKey: # string. SSH Public Key. 
    #sshPassphrase: # string. SSH Passphrase. 
    sshKeySecureFile: # string. Required. SSH Key.
# Install SSH key v0
# Install an SSH key prior to a build or deployment.
- task: InstallSSHKey@0
  inputs:
    knownHostsEntry: # string. Alias: hostName. Required. Known Hosts Entry. 
    sshPublicKey: # string. Required. SSH Public Key. 
    #sshPassphrase: # string. SSH Passphrase. 
    sshKeySecureFile: # string. Required. SSH Key.
# Install SSH Key v0
# Install an SSH key prior to a build or release.
- task: InstallSSHKey@0
  inputs:
    hostName: # string. Required. Known Hosts Entry. 
    sshPublicKey: # string. Required. SSH Public Key. 
    #sshPassphrase: # string. SSH Passphrase. 
    sshKeySecureFile: # string. Required. SSH Key.

輸入

knownHostsEntry - 已知主項目目
輸入別名: hostNamestring. 必要。

指定known_hosts檔案的 SSH 金鑰專案。


sshPublicKey - SSH 公開金鑰
string.

選擇性。 指定公開金鑰的內容。


sshPublicKey - SSH 公開金鑰
string. 必要。

指定公開金鑰的內容。


sshPassphrase - SSH 複雜密碼
string.

選擇性。 指定 SSH 金鑰的複雜密碼,如果有的話。


sshKeySecureFile - SSH 金鑰
string. 必要。

指定上傳至 以 Secure Files 在代理程式上安裝的 SSH 金鑰。


addEntryToConfig - 將專案新增至 SSH 組態
boolean. 預設值:false

選擇性。 新增與安裝至 SSH 組態檔之金鑰相關的專案。 金鑰檔案將適用于所有後續工作。


configHostAlias - 別名
string. 當 時為 必要 addEntryToConfig = true 專案。

指定 SSH 組態專案的名稱。


configHostname - 主機名稱
string. 當 時為 必要 addEntryToConfig = true 專案。

指定 SSH 組態專案的主機名稱屬性。


configUser - 使用者
string. 選擇性。 使用 時機 addEntryToConfig = true

指定 SSH 組態專案的使用者名稱屬性。


configPort - 港口
string. 選擇性。 使用 時機 addEntryToConfig = true

指定 SSH 組態專案的埠。


hostName - 已知主項目目
string. 必要。

指定known_hosts檔案的 SSH 金鑰專案。


工作控制選項

除了工作輸入之外,所有工作都有控制選項。 如需詳細資訊,請參閱 控制項選項和一般工作屬性

輸出變數

無。

備註

在管線中使用這項工作,在建置或發行步驟之前安裝 SSH 金鑰。

注意

此工作需要代理程式上的 Git Bash for Windows。

使用方式和最佳做法

如果您在 託管集區中安裝 SSH 金鑰,請在管線的後續步驟中連線到已就緒相符公開金鑰的遠端系統。 例如,您可以連線到 Git 存放庫或 Azure 中的 VM。

我們建議您不要將公開金鑰當做純文字傳遞至工作組態。 相反地,請在管線中設定檔案內容的 mykey.pub秘密變數。 然後,將管線定義中的變數呼叫為 $(myPubKey) 。 針對金鑰的秘密部分,請使用 Azure Pipelines 中的 安全檔案程式庫

若要建立工作,請使用下列設定良好安裝 SSH 金鑰工作的範例:

steps:
- task: InstallSSHKey@0
  displayName: 'Install an SSH key'
  inputs:
    knownHostsEntry: 'SHA256:1Hyr55tsxGifESBMc0s+2NtutnR/4+LOkVwrOGrIp8U johndoe@contoso'
    sshPublicKey: '$(myPubKey)'
    sshKeySecureFile: 'id_rsa'

注意

您的公開金鑰應新增至存放庫\組織;否則,將會發生存取問題。 針對 GitHub,請遵循 上述指南。 針對Azure DevOps Services,請使用[新增公開金鑰] 來Azure DevOps Services/TFS

在相同的管線作業中安裝多個 SSH 金鑰

在相同的管線作業中使用多個索引鍵時,預設會使用第一個金鑰。 若要能夠在建立 SSH 連線時使用所需的金鑰,您可以使用 Advanced 工作的 區段 InstallSSHKey 來設定下列參數: addEntryToConfigconfigHostAliasconfigHostnameconfigUserconfigPort

這些參數可讓您將主機新增至 SSH 組態檔 (例如,例如 Linux /root/.ssh/config) ,以便透過別名在自訂腳本中使用。

建置完成後,工作會嘗試還原原始的 SSH 組態檔。 如果一開始沒有 SSH 組態檔,則會從代理程式中移除主機。

多個 SSH 金鑰安裝的範例。 案例包含數個 GitHub 存放庫,以及每個存放庫的專屬金鑰:

pool: <Some Agent Pool>

steps:
- task: InstallSSHKey@0
  inputs:
    knownHostsEntry: $(known_host)
    sshPublicKey: $(first_public_key)
    sshKeySecureFile: $(first_private_key)
    addEntryToConfig: true
    configHostAlias: <first-host-alias>
    configHostname: github.com
    configUser: git
  displayName: Install First Repo SSH Key

- task: InstallSSHKey@0
  inputs:
    knownHostsEntry: $(known_host)
    sshPublicKey: $(second_public_key)
    sshKeySecureFile: $(second_private_key)
    addEntryToConfig: true
    configHostAlias: <second-host-alias>
    configHostname: github.com
    configUser: git
  displayName: Install Second Repo SSH Key

- bash: git clone git@<first-host-alias>:<owner>/<first-repo>.git
  displayName: Clone First Repo

- bash: git clone git@<second-host-alias>:<owner>/<second-repo>.git
  displayName: Clone Second Repo

相關的 GitHub 檔

範例

使用 GitHub 的範例設定

本節說明如何在 Azure Pipelines 內搭配 YAML 使用私人 GitHub 存放庫。

如果您有不想向開放原始碼社群公開的存放庫,常見的作法是將存放庫設為私人。 不過,如果您想要使用工具來管理存放庫,Azure DevOps 之類的 CI/CD 工具需要存取存放庫。 若要授與 Azure DevOps 存取權,您可能需要 SSH 金鑰來驗證 GitHub 的存取權。

以下是使用 SSH 金鑰來驗證 GitHub 存取權的步驟:

  1. 產生金鑰組,以用來驗證從 GitHub 到 Azure DevOps 的存取:

    1. 在 GitBash 中,執行下列命令:

      ssh-keygen -t rsa
      
    2. 輸入 SSH 金鑰組的名稱。 在我們的範例中,我們使用 myKey

      GitBash 提示輸入 SSH 金鑰組名稱的螢幕擷取畫面。

    3. (選擇性) 您可以輸入複雜密碼來加密私密金鑰。 此為選用步驟。 使用複雜密碼比不使用複雜密碼更安全。

      GitBash 提示輸入 SSH 金鑰組複雜密碼的螢幕擷取畫面。

      ssh-keygen 會建立 SSH 金鑰組,並出現下列成功訊息:

      顯示已建立 SSH 金鑰組的 GitBash 訊息螢幕擷取畫面。

    4. 在 Windows 檔案總管中,檢查新建立的金鑰組:

      Windows 檔案總管中金鑰組檔案的螢幕擷取畫面。

  2. 將公開金鑰新增至 GitHub 存放庫。 (公開金鑰結尾為 「.pub」) 。 若要這樣做,請在瀏覽器中移至下列 URL: https://github.com/(organization-name)/(repository-name)/settings/keys

    1. 選取 [新增部署金鑰]。

    2. 在 [ 新增 ] 對話方塊中,輸入標題,然後複製並貼上 SSH 金鑰:

      [新增] 對話方塊的螢幕擷取畫面。

    3. 選取 [新增金鑰]。

  3. 將您的私密金鑰上傳至 Azure DevOps:

    1. 在 Azure DevOps 的左側功能表中,選取[管線連結>]。

      Azure Pipelines 功能表的螢幕擷取畫面。

    2. 選取[安全檔案>+ 安全檔案]:

      [安全檔案] 功能表的螢幕擷取畫面。

    3. 選取 [流覽],然後選取您的私密金鑰:

      [上傳檔案] 對話方塊和 [流覽] 按鈕的螢幕擷取畫面。

  4. 復原您的「已知主項目目」。 在 GitBash 中,輸入下列命令:

    ssh-keyscan github.com
    

    您的「已知主項目目」是 GitBash 結果中未以 # 開頭的顯示值:

    GitBash 中金鑰搜尋結果的螢幕擷取畫面。

  5. 建立 YAML 管線。

    若要建立 YAML 管線,請在 YAML 定義中新增下列工作:

    - task: InstallSSHKey@0
     inputs:
       knownHostsEntry: #{Enter your Known Hosts Entry Here}
       sshPublicKey: #{Enter your Public key Here}
       sshKeySecureFile: #{Enter the name of your key in "Secure Files" Here}
    

現在已安裝 SSH 金鑰,而且您可以使用 SSH 繼續執行腳本來連線,而不是預設 HTTPS。

規格需求

需求 描述
管線類型 YAML、傳統組建、傳統版本
執行于 Agent、DeploymentGroup
要求
Capabilities 此工作不符合作業中後續工作的任何需求。
命令限制 此工作會使用下列 命令限制來執行:受限制
Settable 變數 此工作具有 設定下列變數的許可權:SSH_AGENT_PID、SSH_AUTH_SOCK、INSTALL_SSH_KEY_CONFIG_LOCATION INSTALL_SSH_KEY_KNOWN_HOSTS_LOCATION
代理程式版本 2.182.1 或更新版本
工作類別 公用程式
需求 描述
管線類型 YAML、傳統組建、傳統版本
執行于 Agent、DeploymentGroup
要求
Capabilities 此工作不符合作業中後續工作的任何需求。
命令限制 任意
Settable 變數 任意
代理程式版本 2.117.0 或更新版本
工作類別 公用程式