<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Using SSH keys with GitLab CI/CD > 原文:[https://docs.gitlab.com/ee/ci/ssh_keys/README.html](https://docs.gitlab.com/ee/ci/ssh_keys/README.html) * [How it works](#how-it-works) * [SSH keys when using the Docker executor](#ssh-keys-when-using-the-docker-executor) * [SSH keys when using the Shell executor](#ssh-keys-when-using-the-shell-executor) * [Verifying the SSH host keys](#verifying-the-ssh-host-keys) * [Example project](#example-project) # Using SSH keys with GitLab CI/CD[](#using-ssh-keys-with-gitlab-cicd "Permalink") GitLab 當前不支持在構建環境(運行 GitLab Runner 的環境)中管理 SSH 密鑰的內置支持. SSH 密鑰在以下情況下很有用: 1. 您想簽出內部子模塊 2. 您想使用包管理器(例如 Bundler)下載私有包 3. 您想要將應用程序部署到自己的服務器上,例如 Heroku 4. 您要執行從構建環境到遠程服務器的 SSH 命令 5. 您想將文件從構建環境同步到遠程服務器 如果上述任何事情都響了,那么您很可能需要 SSH 密鑰. 支持最廣泛的方法是通過擴展`.gitlab-ci.yml`來將 SSH 密鑰注入構建環境,該解決方案可與任何類型的[執行器](https://docs.gitlab.com/runner/executors/) (Docker,shell 等)一起使用. ## How it works[](#how-it-works "Permalink") 1. 使用[`ssh-keygen`](https://linux.die.net/man/1/ssh-keygen)在本地創建新的 SSH 密鑰對 2. 將私鑰作為[變量](../variables/README.html)添加到您的項目中 3. 在作業期間運行[`ssh-agent`](https://linux.die.net/man/1/ssh-agent)以加載私鑰. 4. 將公用密鑰復制到您要訪問的服務器上(通常在`~/.ssh/authorized_keys` ),如果要訪問私有的 GitLab 存儲庫,則將其添加為[部署密鑰](../../ssh/README.html#deploy-keys) . **注意:**除非您啟用[調試日志](../variables/README.html#debug-logging)記錄,否則私鑰將不會顯示在作業日志中. 您可能還需要檢查[管道](../pipelines/settings.html#visibility-of-pipelines)的[可見性](../pipelines/settings.html#visibility-of-pipelines) . ## SSH keys when using the Docker executor[](#ssh-keys-when-using-the-docker-executor "Permalink") 當您的 CI / CD 作業在 Docker 容器中運行(意味著包含環境)并且您想要在私有服務器中部署代碼時,您需要一種訪問它的方法. 這是 SSH 密鑰對派上用場的地方. 1. 您首先需要創建一個 SSH 密鑰對. 有關更多信息,請按照說明[生成 SSH 密鑰](../../ssh/README.html#generating-a-new-ssh-key-pair) . **不要**在 SSH 密鑰中添加密碼,否則`before_script`會提示您輸入密碼. 2. 創建一個新[變量](../variables/README.html#gitlab-cicd-environment-variables) . 在" **密鑰"中**輸入名稱`SSH_PRIVATE_KEY`然后在" **值"**字段中粘貼先前創建的*私鑰*的內容. 3. 使用`before_script`操作修改`.gitlab-ci.yml` . 在以下示例中,假定使用基于 Debian 的圖像. 根據需要進行編輯: ``` before_script: ## ## Install ssh-agent if not already installed, it is required by Docker. ## (change apt-get to yum if you use an RPM-based image) ## - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )' ## ## Run ssh-agent (inside the build environment) ## - eval $(ssh-agent -s) ## ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store ## We're using tr to fix line endings which makes ed25519 keys work ## without extra base64 encoding. ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556 ## - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - ## ## Create the SSH directory and give it the right permissions ## - mkdir -p ~/.ssh - chmod 700 ~/.ssh ## ## Optionally, if you will be using any Git commands, set the user name and ## and email. ## #- git config --global user.email "user@example.com" #- git config --global user.name "User name" ``` **注意:** [`before_script`](../yaml/README.html#before_script-and-after_script)可以全局設置或按作業設置. 4. 確保專用服務器的[SSH 主機密鑰已驗證](#verifying-the-ssh-host-keys) . 5. 作為最后一步,從添加您在第一步,你想擁有從構建環境中的接入服務創建一個*公共*密鑰. 如果要訪問私有的 GitLab 存儲庫,則需要將其添加為[部署密鑰](../../ssh/README.html#deploy-keys) . 而已! 現在,您可以在構建環境中訪問私有服務器或存儲庫. ## SSH keys when using the Shell executor[](#ssh-keys-when-using-the-shell-executor "Permalink") 如果您使用的是 Shell 執行程序而不是 Docker,則設置 SSH 密鑰會更加容易. 您可以從安裝了 GitLab Runner 的計算機生成 SSH 密鑰,并將該密鑰用于在該計算機上運行的所有項目. 1. 首先,登錄到運行您的作業的服務器. 2. 然后,從終端以`gitlab-runner`用戶身份登錄: ``` sudo su - gitlab-runner ``` 3. 按照說明生成 SSH 密鑰對,以[生成 SSH 密鑰](../../ssh/README.html#generating-a-new-ssh-key-pair) . **不要**在 SSH 密鑰中添加密碼,否則`before_script`會提示您輸入密碼. 4. 作為最后一步,加前面創建要具有從構建環境中的接入服務的一個*公共*密鑰. 如果要訪問私有的 GitLab 存儲庫,則需要將其添加為[部署密鑰](../../ssh/README.html#deploy-keys) . 完成后,嘗試登錄到遠程服務器以接受指紋: ``` ssh example.com ``` 要訪問 GitLab.com 上的存儲庫,可以使用`git@gitlab.com` . ## Verifying the SSH host keys[](#verifying-the-ssh-host-keys "Permalink") 最好檢查私有服務器自己的公用密鑰,以確保您不會受到中間人攻擊的攻擊. 萬一發生任何可疑事件,您將注意到它,因為作業將失敗(如果公鑰不匹配,則 SSH 連接將失敗). 要查找服務器的主機密鑰,請從受信任的網絡(最好是從私有服務器本身)運行`ssh-keyscan`命令: ``` ## Use the domain name ssh-keyscan example.com ## Or use an IP ssh-keyscan 1.2.3.4 ``` 使用`SSH_KNOWN_HOSTS`作為"密鑰"創建一個新[變量](../variables/README.html#gitlab-cicd-environment-variables) ,并作為"值"添加`ssh-keyscan`的輸出. **注意:**如果需要連接到多個服務器,則所有服務器主機密鑰都需要收集在變量的**Value**中,每行一個密鑰.**提示:**通過在`.gitlab-ci.yml`直接使用變量而不是`ssh-keyscan` ,這樣做的好處是,如果主機域名由于某種原因而更改,則不必更改`.gitlab-ci.yml` . 而且,這些值是由您預定義的,這意味著如果主機密鑰突然更改,CI / CD 作業將失敗,并且您將知道服務器或網絡出了點問題. 現在已經創建了`SSH_KNOWN_HOSTS`變量,除了上面[`.gitlab-ci.yml`](#ssh-keys-when-using-the-docker-executor)的[內容](#ssh-keys-when-using-the-docker-executor)之外,還需要添加以下內容: ``` before_script: ## ## Assuming you created the SSH_KNOWN_HOSTS variable, uncomment the ## following two lines. ## - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts - chmod 644 ~/.ssh/known_hosts ## ## Alternatively, use ssh-keyscan to scan the keys of your private server. ## Replace example.com with your private server's domain name. Repeat that ## command if you have more than one server to connect to. ## #- ssh-keyscan example.com >> ~/.ssh/known_hosts #- chmod 644 ~/.ssh/known_hosts ## ## You can optionally disable host key checking. Be aware that by adding that ## you are susceptible to man-in-the-middle attacks. ## WARNING: Use this only with the Docker executor, if you use it with shell ## you will overwrite your user's SSH config. ## #- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" >> ~/.ssh/config' ``` ## Example project[](#example-project "Permalink") 為了方便起見,我們建立了一個[示例 SSH 項目](https://gitlab.com/gitlab-examples/ssh-private-key/) ,使用我們的公共[共享](../runners/README.html)運行程序在[GitLab.com](https://gitlab.com)上運行. 想要破解嗎? 只需對其進行分叉,提交并推送您的更改. 稍后,公共跑步者將選擇更改并開始工作.
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看