分享方式:


教學課程:使用 Azure 儲存體 建置成品

重要

許多 Azure 服務都有 Jenkins 外掛程式。截至 2024 年 2 月 29 日,其中部分外掛程式將不支援。 Azure CLI 目前是整合 Jenkins 與 Azure 服務的建議方式。 如需詳細資訊,請參閱適用於 Azure 的 Jenkins 外掛程式一文

本文說明如何使用 Blob 記憶體作為 Jenkins 持續整合 (CI) 解決方案所建立之組建成品的存放庫,或作為建置程式中要使用的可下載檔案來源。 當您在敏捷式開發環境中撰寫程式代碼(使用 Java 或其他語言)、建置會根據持續整合執行,而且您需要建置成品的存放庫,例如,您可以與其他組織成員、您的客戶共用它們,或維護封存。 另一個案例是當您的建置作業本身需要其他檔案時,例如,相依性要下載為組建輸入的一部分。

必要條件

  • Azure 訂用帳戶:如果您沒有 Azure 訂用帳戶,請在 開始之前建立免費的 Azure 帳戶
  • Jenkins 伺服器:如果您沒有安裝 Jenkins 伺服器, 請在 Azure 上建立 Jenkins 伺服器。
  • Azure CLI:在 Jenkins 伺服器上安裝 Azure CLI(2.0.67 版或更高版本)。
  • Azure 記憶體帳戶:如果您還沒有記憶體帳戶請建立 儲存體 帳戶

新增執行 Azure CLI 所需的 Azure 認證

  1. 流覽至 Jenkins 入口網站。

  2. 從功能表中,選取 [ 管理 Jenkins]。

  3. 選取 [ 管理認證]。

  4. 選取全域網域。

  5. 選取 [ 新增認證]。

  6. 填寫必要的欄位,如下所示:

    • 種類:選取 [具有密碼的用戶名稱]。
    • 使用者名稱:指定 appId 服務主體的 。
    • 密碼:指定 password 服務主體的 。
    • 識別碼:指定認證識別碼,例如 azuresp
    • 描述:選擇性地為您的環境包含有意義的描述。
  7. 選取 [ 確定 ] 以建立認證。

建立管線作業以上傳組建成品

下列步驟會引導您建立管線作業。 管線作業會建立數個檔案,並使用 Azure CLI 將檔案上傳至記憶體帳戶。

  1. 從 Jenkins 儀錶板中,選取 [ 新增專案]。

  2. 將作業 命名為 myjob,選取 [ 管線],然後選取 [ 確定]。

  3. 在作業組態的 [管線] 區段中,選取 [管線腳本],並將下列內容貼到 [腳本]。 編輯佔位元以符合您環境的值。

    pipeline {
      agent any
      environment {
        AZURE_SUBSCRIPTION_ID='99999999-9999-9999-9999-999999999999'
        AZURE_TENANT_ID='99999999-9999-9999-9999-999999999999'
        AZURE_STORAGE_ACCOUNT='myStorageAccount'
      }
      stages {
        stage('Build') {
          steps {
            sh 'rm -rf *'
            sh 'mkdir text'
            sh 'echo Hello Azure Storage from Jenkins > ./text/hello.txt'
            sh 'date > ./text/date.txt'
          }
    
          post {
            success {
              withCredentials([usernamePassword(credentialsId: 'azuresp', 
                              passwordVariable: 'AZURE_CLIENT_SECRET', 
                              usernameVariable: 'AZURE_CLIENT_ID')]) {
                sh '''
                  echo $container_name
                  # Login to Azure with ServicePrincipal
                  az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID
                  # Set default subscription
                  az account set --subscription $AZURE_SUBSCRIPTION_ID
                  # Execute upload to Azure
                  az storage container create --account-name $AZURE_STORAGE_ACCOUNT --name $JOB_NAME --auth-mode login
                  az storage blob upload-batch --destination ${JOB_NAME} --source ./text --account-name $AZURE_STORAGE_ACCOUNT
                  # Logout from Azure
                  az logout
                '''
              }
            }
          }
        }
      }
    }
    
  4. 選取 [ 立即 建置] 以執行 myjob

  5. 檢查主控台輸出中是否有狀態。 建置後動作上傳組建成品時,Azure 記憶體的狀態消息會寫入主控台。

  6. 如果您遇到類似下列的錯誤,表示您需要在容器層級授與存取權: ValidationError: You do not have the required permissions needed to perform this operation. 如果您收到此錯誤訊息,請參閱下列文章來解決:

  7. 成功完成作業后,請開啟公用 Blob 來檢查組建成品:

    1. 登入 Azure 入口網站
    2. 選取儲存體
    3. 選取您用於 Jenkins 的記憶體帳戶名稱。
    4. 選取 [ 容器]。
    5. 在 Blob 清單中選取名為 myjob 的容器。
    6. 您應該會看到下列兩個檔案: hello.txtdate.txt
    7. 複製這些專案的 URL,並將其貼到您的瀏覽器中。
    8. 您會看到上傳為組建成品的文字檔。

    重點︰

    • Azure 記憶體中的容器名稱和 Blob 名稱會區分大小寫。

建立管線作業以從 Azure Blob 儲存體 下載

下列步驟示範如何設定管線作業,以從 Azure Blob 儲存體 下載專案。

  1. 在作業組態的 [管線] 區段中,選取 [管線腳本],然後在 [腳本] 中貼上下列內容。 編輯佔位元以符合您環境的值。

    pipeline {
      agent any
      environment {
        AZURE_SUBSCRIPTION_ID='99999999-9999-9999-9999-999999999999'
        AZURE_TENANT_ID='99999999-9999-9999-9999-999999999999'
        AZURE_STORAGE_ACCOUNT='myStorageAccount'
      }
      stages {
        stage('Build') {
          steps {
            withCredentials([usernamePassword(credentialsId: 'azuresp', 
                            passwordVariable: 'AZURE_CLIENT_SECRET', 
                            usernameVariable: 'AZURE_CLIENT_ID')]) {
              sh '''
                # Login to Azure with ServicePrincipal
                az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID
                # Set default subscription
                az account set --subscription $AZURE_SUBSCRIPTION_ID
                # Execute upload to Azure
                az storage blob download --account-name $AZURE_STORAGE_ACCOUNT --container-name myjob --name hello.txt --file ${WORKSPACE}/hello.txt --auth-mode login
                # Logout from Azure
                az logout
              '''   
            }
          }
        }
      }
    }
    
  2. 執行組建之後,請檢查組建歷程記錄控制台輸出。 或者,您也可以查看下載位置,以查看您預期的 Blob 是否已成功下載。

下一步