教學課程:將 Azure 儲存體使用於組建成品Tutorial: Use Azure Storage for build artifacts

本文說明如何使用 Blob 儲存體作為 Jenkins 連續整合 (CI) 解決方案所建立的組建構件存放庫,或作為在組建程序中要使用的可下載檔案的來源。This article illustrates how to use Blob storage as a repository of build artifacts created by a Jenkins continuous integration (CI) solution, or as a source of downloadable files to be used in a build process. 您會發現此解決方案有其效用的情況之一,是當您在敏捷式開發環境中編寫程式碼 (使用 Java 或其他語言) 時,組建是根據連續整合來執行,而您需要一個存放庫來存放組建構件,以便能夠將這些構件分享給其他組織成員或客戶,或是維護封存等等。One of the scenarios where you would find this solution useful is when you're coding in an agile development environment (using Java or other languages), builds are running based on continuous integration, and you need a repository for your build artifacts, so that you could, for example, share them with other organization members, your customers, or maintain an archive. 另一種情況是當組建工作本身需要其他檔案時,例如,要隨組建輸入一起下載的相依性項目。Another scenario is when your build job itself requires other files, for example, dependencies to download as part of the build input.

在本教學課程中,您將使用由 Microsoft 所提供適用於 Jenkins CI 的 Azure 儲存體外掛程式。In this tutorial, you will be using the Azure Storage Plugin for Jenkins CI made available by Microsoft.

Jenkins 概觀Jenkins overview

Jenkins 透過讓開發人員輕鬆整合自己的程式碼變更,並會以自動且頻繁的方式產生組建,來實現軟體專案的連續整合,從而提升開發人員產能。Jenkins enables continuous integration of a software project by allowing developers to easily integrate their code changes and have builds produced automatically and frequently, thereby increasing the productivity of the developers. 組建會分版本存在,而組建成品可以上傳至各種存放庫。Builds are versioned, and build artifacts can be uploaded to various repositories. 本文說明如何使用 Azure Blob 儲存體作為組建構件的存放庫。This article shows how to use Azure blob storage as the repository of the build artifacts. 其中也示範如何從 Azure Blob 儲存體下載相依性項目。It will also show how to download dependencies from Azure blob storage.

如需 Jenkins 的詳細資訊,請參閱 認識 Jenkins(英文)。More information about Jenkins can be found at Meet Jenkins.

使用 Blob 服務的優點Benefits of using the Blob service

使用 Blob 服務來裝載您敏捷式開發組建成品的優點包括:Benefits of using the Blob service to host your agile development build artifacts include:

  • 組建成品和/或可下載相依性項目的高可用性。High availability of your build artifacts and/or downloadable dependencies.
  • 在您的 Jenkins CI 解決方案上傳組建成品時提供良好的效能。Performance when your Jenkins CI solution uploads your build artifacts.
  • 在您的客戶和合作夥伴下載組建成品時提供良好的效能。Performance when your customers and partners download your build artifacts.
  • 提供使用者存取原則控制,可以選擇匿名存取、期限型共用存取簽章存取、私用存取等。Control over user access policies, with a choice between anonymous access, expiration-based shared access signature access, private access, etc.

PrerequisitesPrerequisites

  • 一個 Jenkins 連續整合解決方案。A Jenkins continuous integration solution.

    如果您目前沒有 Jenkins CI 解決方案,可以透過下列步驟執行 Jenkins CI 解決方案:If you currently don't have a Jenkins CI solution, you can run a Jenkins CI solution using the following technique:

    1. 在已啟用 Java 的機器上,從 https://jenkins-ci.org 下載 jenkins.war。On a Java-enabled machine, download jenkins.war from https://jenkins-ci.org.

    2. 在命令提示字元中切換至包含 jenkins.war 的資料夾,然後執行:At a command prompt that is opened to the folder that contains jenkins.war, run:

      java -jar jenkins.war

    3. 在瀏覽器中開啟 http://localhost:8080/ 以開啟 Jenkins 儀表板,您將使用此儀表板來安裝及設定 Azure 儲存體外掛程式。In your browser, open http://localhost:8080/ to open the Jenkins dashboard, which you will use to install and configure the Azure Storage plugin.

      雖然一般 Jenkins CI 解決方案會設定成以服務的形式執行,但對本教學課程來說,在命令列執行 Jenkins war 已經足夠。While a typical Jenkins CI solution would be set up to run as a service, running the Jenkins war at the command line will be sufficient for this tutorial.

  • 一個 Azure 帳戶。An Azure account. 您可以在 https://www.azure.com 註冊 Azure 帳戶。You can sign up for an Azure account at https://www.azure.com.

  • 一個 Azure 儲存體帳戶。An Azure storage account. 如果您還沒有儲存體帳戶,可以使用 建立儲存體帳戶中的步驟建立一個帳戶。If you don't already have a storage account, you can create one using the steps at Create a Storage Account.

  • 建議您熟悉 Jenkins CI 解決方案,但這並非必要,因為下列內容將使用一個基本範例來示範使用 Blob 服務作為 Jenkins CI 組建成品的存放庫時所需的步驟。Familiarity with the Jenkins CI solution is recommended but not required, as the following content will use a basic example to show you the steps needed when using the Blob service as a repository for Jenkins CI build artifacts.

如何使用 Blob 服務搭配 Jenkins CIHow to use the Blob service with Jenkins CI

若要使用 Blob 服務搭配 Jenkins,您將要安裝 Azure 儲存體外掛程式、設定外掛程式來使用您的儲存體帳戶,然後建立一個會將您的組建成品上傳至您儲存體帳戶的建置後動作。To use the Blob service with Jenkins, you'll need to install the Azure Storage plugin, configure the plugin to use your storage account, and then create a post-build action that uploads your build artifacts to your storage account. 這些步驟將於下列各節中說明。These steps are described in the following sections.

如何安裝 Azure 儲存體外掛程式How to install the Azure Storage plugin

  1. 在 Jenkins 儀表板中,選取 [管理 Jenkins] 。Within the Jenkins dashboard, select Manage Jenkins.
  2. 在 [管理 Jenkins] 頁面中,選取 [管理外掛程式] 。In the Manage Jenkins page, select Manage Plugins.
  3. 選取 [可用] 索引標籤。Select the Available tab.
  4. 在 [構件上傳程式] 區段中,取核 [Microsoft Azure 儲存體外掛程式] 。In the Artifact Uploaders section, check Microsoft Azure Storage plugin.
  5. 選取 [直接安裝而不重新啟動] 或 [立即下載並於重新啟動後安裝] 。Select either Install without restart or Download now and install after restart.
  6. 重新啟動 Jenkins。Restart Jenkins.

如何設定 Azure 儲存體外掛程式來使用您的儲存體帳戶How to configure the Azure Storage plugin to use your storage account

  1. 在 Jenkins 儀表板中,選取 [管理 Jenkins] 。Within the Jenkins dashboard, select Manage Jenkins.
  2. 在 [管理 Jenkins] 頁面中,選取 [設定系統] 。In the Manage Jenkins page, select Configure System.
  3. 在 [Microsoft Azure Storage Account Configuration] 區段中:In the Microsoft Azure Storage Account Configuration section:
    1. 輸入您的儲存體帳戶名稱,您可從 Azure 入口網站取得此名稱。Enter your storage account name, which you can obtain from the Azure portal.
    2. 輸入您的儲存體帳戶金鑰,這項資訊也可以從 Azure 入口網站取得。Enter your storage account key, also obtainable from the Azure portal.
    3. 如果您使用全域 Azure 雲端,請讓 [Blob 服務端點 URL] 使用預設值。Use the default value for Blob Service Endpoint URL if you are using the global Azure cloud. 如果您使用的是其他 Azure 雲端,請使用 Azure 入口網站 中為您儲存體帳戶所指定的端點。If you are using a different Azure cloud, use the endpoint as specified in the Azure portal for your storage account.
    4. 按一下 [驗證儲存體認證] 以驗證您的儲存體帳戶。Select Validate storage credentials to validate your storage account.
    5. [選擇性] 如果您有其他儲存體帳戶要提供給 Jenkins CI 使用,請選取 [新增更多儲存體帳戶] 。[Optional] If you have additional storage accounts that you want made available to your Jenkins CI, select Add more Storage Accounts.
    6. 選取 [儲存] 以儲存您的設定。Select Save to save your settings.

如何建立會將您的組建成品上傳至您儲存體帳戶的建置後動作How to create a post-build action that uploads your build artifacts to your storage account

為了方便說明,您會先建立一個會建立數個檔案的作業,然後新增會將這些檔案上傳至儲存體帳戶的建置後動作。For instructional purposes, you first need to create a job that will create several files, and then add in the post-build action to upload the files to your storage account.

  1. 在 Jenkins 儀表板中,選取 [新增項目] 。Within the Jenkins dashboard, select New Item.

  2. 將作業命名為 MyJob,選取 [建立任意樣式的軟體專案] ,然後選取 [確定] 。Name the job MyJob, select Build a free-style software project, and then select OK.

  3. 在作業組態的 [組建] 區段中,選取 [新增組建步驟] ,然後選取 [執行 Windows 批次命令] 。In the Build section of the job configuration, select Add build step and select Execute Windows batch command.

  4. 在 [Command] 中,使用下列命令:In Command, use the following commands:

    md text
    cd text
    echo Hello Azure Storage from Jenkins > hello.txt
    date /t > date.txt
    time /t >> date.txt
    
  5. 在作業組態的 [建置後動作] 區段中,選取 [新增建置後動作] ,然後選取 [將構件上傳至 Azure Blob 儲存體] 。In the Post-build Actions section of the job configuration, select Add post-build action and select Upload artifacts to Azure Blob storage.

  6. 在 [儲存體帳戶名稱] 中,選取要使用的儲存體帳戶。For Storage account name, select the storage account to use.

  7. 在 [容器名稱] 中,指定容器名稱。For Container name, specify the container name. (如果上傳組建成品時該容器尚未存在,將會建立該容器。)您可以使用環境變數,就這個範例而言,請輸入 ${JOB_NAME} 作為容器名稱。(The container will be created if it does not already exist when the build artifacts are uploaded.) You can use environment variables, so for this example enter ${JOB_NAME} as the container name.

    提示

    在您為 [執行 Windows 批次命令] 輸入指令碼的 [命令] 區段底下有一個 Jenkins 所辨識環境變數的連結。Below the Command section where you entered a script for Execute Windows batch command is a link to the environment variables recognized by Jenkins. 選取該連結可了解各環境變數名稱和描述。Select that link to learn the environment variable names and descriptions. 含有特殊字元的環境變數 (例如 BUILD_URL 環境變數) 不能當做容器名稱或共同虛擬路徑。Environment variables that contain special characters, such as the BUILD_URL environment variable, are not allowed as a container name or common virtual path.

  8. 在此範例中,請選取 [依預設將新容器設為公用] 。Select Make new container public by default for this example. (如果您想要使用私用容器,則需要建立共用存取簽章來允許存取,但這不在本文的討論之列。(If you want to use a private container, you'll need to create a shared access signature to allow access, which is beyond the scope of this article. 若要深入了解共用存取簽章,請參閱使用共用存取簽章 (SAS)。)You can learn more about shared access signatures at Using Shared Access Signatures (SAS).)

  9. [選擇性] 如果您要在上傳組建構件之前清除容器的內容,請選取 [在上傳之前清除容器] (若不想清除容器的內容,請維持不核取)。[Optional] Select Clean container before uploading if you want the container to be cleared of contents before build artifacts are uploaded (leave it unchecked if you do not want to clean the contents of the container).

  10. 針對 [要上傳的構件清單] ,輸入 text/*.txtFor List of Artifacts to upload, enter text/*.txt.

  11. 在 [上傳構件的常用虛擬路徑] 中,基於本教學課程的目的,輸入 ${BUILD\_ID}/${BUILD\_NUMBER}For Common virtual path for uploaded artifacts, for purposes of this tutorial, enter ${BUILD\_ID}/${BUILD\_NUMBER}.

  12. 選取 [儲存] 以儲存您的設定。Select Save to save your settings.

  13. 在 Jenkins 儀表板中,選取 [立即建置] 以執行 MyJobIn the Jenkins dashboard, select Build Now to run MyJob. 檢查主控台輸出中的狀態。Examine the console output for status. 當建置後動作開始上傳組建成品時,主控台輸出中將會包含 Azure 儲存體的狀態訊息。Status messages for Azure storage will be included in the console output when the post-build action starts to upload build artifacts.

  14. 順利完成作業時,您就可以開啟公用 Blob 來檢查組建成品。Upon successful completion of the job, you can examine the build artifacts by opening the public blob.

    1. 登入 Azure 入口網站Sign in to the Azure portal.
    2. 選取 [儲存體] 。Select Storage.
    3. 選取您用於 Jenkins 的儲存體帳戶名稱。Select the storage account name that you used for Jenkins.
    4. 選取 [容器] 。Select Containers.
    5. 選取名為 myjob的容器,這是您建立 Jenkins 工作時所指派的工作名稱小寫版本。Select the container named myjob, which is the lowercase version of the job name that you assigned when you created the Jenkins job. 在 Azure 儲存體中,容器名稱和 Blob 名稱皆為小寫 (並且區分大小寫)。Container names and blob names are lowercase (and case-sensitive) in Azure storage. 在容器 myjob 的 Blob 清單中,您應該會看到 hello.txtdate.txtWithin the list of blobs for the container named myjob, you should see hello.txt and date.txt. 請複製這些項目中任何一項的 URL,然後在瀏覽器中開啟它。Copy the URL for either of these items and open it in your browser. 您會看到文字檔已上傳作為組建成品。You will see the text file that was uploaded as a build artifact.

每一工作只能建立一個將成品上傳至 Azure Blob 儲存體的建置後動作。Only one post-build action that uploads artifacts to Azure blob storage can be created per job. 將構件上傳至 Azure Blob 儲存體的單一建置後動作,可在 [要上船的構件清單] 內使用分號作為分隔符號來指定不同的檔案 (包含萬用字元) 和檔案路徑。The single post-build action to upload artifacts to Azure blob storage can specify different files (including wildcards) and paths to files within List of Artifacts to upload using a semi-colon as a separator. 例如,若 Jenkins 組建在您工作區的 build 資料夾中產生 JAR 檔和 TXT 檔,且您想將兩者都上傳至 Azure Blob 儲存體,請在 [要上傳的構件清單] 選項中使用下列值:build/\*.jar;build/\*.txtFor example, if your Jenkins build produces JAR files and TXT files in your workspace's build folder, and you want to upload both to Azure blob storage, use the following value for the List of Artifacts to upload option: build/\*.jar;build/\*.txt. 您也可以使用雙冒號語法來指定要在 Blob 名稱內使用的路徑。You can also use double-colon syntax to specify a path to use within the blob name. 例如,若您想要在 Blob 路徑中使用 binaries 上傳 JAR,並在 Blob 路徑中使用 notices 上傳 TXT,請在 [要上傳的構件清單] 中使用下列值:build/\*.jar::binaries;build/\*.txt::noticesFor example, if you want the JARs to get uploaded using binaries in the blob path and the TXT files to get uploaded using notices in the blob path, use the following value for the List of Artifacts to upload option: build/\*.jar::binaries;build/\*.txt::notices.

如何建立從 Azure Blob 儲存體下載的組建步驟How to create a build step that downloads from Azure blob storage

下列步驟說明如何設定從 Azure Blob 儲存體下載項目的組建步驟,如果您想要在組件中包含項目,則可以使用。The following steps illustrate to configure a build step to download items from Azure blob storage, which is useful if you want to include items in your build. 使用此模式的範例,是您可能想要在 Azure Blob 儲存體中保存的 JAR。An example of using this pattern is JARs that you might want to persist in Azure blob storage.

  1. 在工作組態的 [組建] 區段中,選取 [新增組建步驟] ,然後選取 [從 Azure Blob 儲存體下載] 。In the Build section of the job configuration, select Add build step and select Download from Azure Blob storage.
  2. 在 [儲存體帳戶名稱] 中,選取要使用的儲存體帳戶。For Storage account name, select the storage account to use.
  3. 在 [容器名稱] 中,指定您要下載的 Blob 所在之容器的名稱。For Container name, specify the name of the container that has the blobs you want to download. 您可以使用環境變數。You can use environment variables.
  4. 在 [Blob 名稱] 中,指定 Blob 名稱。For Blob name, specify the blob name. 您可以使用環境變數。You can use environment variables. 另外,您也可以在指定 Blob 名稱的開頭字母之後,使用星號作為萬用字元。Also, you can use an asterisk, as a wildcard after you specify the initial letter(s) of the blob name. 例如,project\ * 指定名稱開頭為 project 的所有 Blob。For example, project\* would specify all blobs whose names start with project.
  5. [選擇性] 在 [下載路徑] 中,指定在 Jenkins 機器上您要從 Azure Blob 儲存體下載檔案的路徑。[Optional] For Download path, specify the path on the Jenkins machine where you want to download files from Azure blob storage. 也可以使用環境變數Environment variables can also be used. (如果您未提供 [下載路徑] 的值,則 Azure Blob 儲存體中的檔案會下載至工作的工作區。)(If you do not provide a value for Download path, the files from Azure blob storage will be downloaded to the job's workspace.)

如果您還有其他項目要從 Azure Blob 儲存體下載,您可以建立其他組建步驟。If you have additional items you want to download from Azure blob storage, you can create additional build steps.

執行組建之後,您可以檢查組建記錄主控台輸出,或查看下載位置,了解是否已成功下載您想要的 Blob。After you run a build, you can check the build history console output, or look at your download location, to see whether the blobs you expected were successfully downloaded.

Blob 服務所使用的元件Components used by the Blob service

本節提供 Blob 服務元件的概觀。This section provides an overview of the Blob service components.

  • 儲存體帳戶:所有對 Azure 儲存體的存取都是透過儲存體帳戶進行。Storage Account: All access to Azure Storage is done through a storage account. 儲存體帳戶是存取 Blob 的最上層命名空間。A storage account is the highest level of the namespace for accessing blobs. 帳戶可以包含不限數目的容器,只要它們的大小總計低於 100 TB 即可。An account can contain an unlimited number of containers, as long as their total size is under 100 TB.

  • 容器:容器會提供一組 Blob 的群組。Container: A container provides a grouping of a set of blobs. 所有 Blob 都必須位於容器中。All blobs must be in a container. 帳戶可以包含不限制數目的容器。An account can contain an unlimited number of containers. 容器可以儲存不限數目的 Blob。A container can store an unlimited number of blobs.

  • Blob:任何類型和大小的檔案。Blob: A file of any type and size. Azure 儲存體可以儲存的 Blob 類型有兩種:區塊和分頁 Blob。There are two types of blobs that can be stored in Azure Storage: block and page blobs. 大部分檔案都是區塊 Blob。Most files are block blobs. 單一區塊 Blob 的大小上限為 200 GB。A single block blob can be up to 200 GB in size. 本教學課程使用區塊 Blob。This tutorial uses block blobs. 分頁 Blob (另一種 Blob 類型) 的大小上限為 1 TB,當檔案中的位元組範圍經常修改時,分頁 Blob 的效率較高。Page blobs, another blob type, can be up to 1 TB in size, and are more efficient when ranges of bytes in a file are modified frequently. 如需關於 Blob 的詳細資訊,請參閱 了解區塊 Blob、附加 Blob 及分頁 BlobFor more information about blobs, see Understanding Block Blobs, Append Blobs, and Page Blobs.

  • URL 格式:可利用下列 URL 格式來定址 Blob:URL format: Blobs are addressable using the following URL format:

    http://storageaccount.blob.core.windows.net/container_name/blob_name

    (上述格式適用於全域 Azure 雲端。(The format above applies to the global Azure cloud. 如果您使用其他 Azure 雲端,請使用 Azure 入口網站中的端點來判斷您的 URL 端點。)If you are using a different Azure cloud, use the endpoint within the Azure portal to determine your URL endpoint.)

    在上述格式中,storageaccount 代表您的儲存體帳戶名稱,container_name 代表您的容器名稱,而 blob_name 代表您的 Blob 名稱。In the format above, storageaccount represents the name of your storage account, container_name represents the name of your container, and blob_name represents the name of your blob, respectively. 容器名稱中可以有多個路徑,這些路徑彼此以正斜線 / (英文)。Within the container name, you can have multiple paths, separated by a forward slash, /. 本教學課程中使用的範例容器名稱為 MyJob,使用的共同虛擬路徑則是 ${BUILD_ID}/${BUILD_NUMBER} ,產生的 Blob URL 格式如下:The example container name used for this tutorial was MyJob, and ${BUILD_ID}/${BUILD_NUMBER} was used for the common virtual path, resulting in the blob having a URL of the following form:

    http://example.blob.core.windows.net/myjob/2014-04-14_23-57-00/1/hello.txt

對 Jenkins 外掛程式進行疑難排解Troubleshooting the Jenkins plugin

如果您遇到任何有關 Jenkins 外掛程式的錯誤,請在 Jenkins JIRA 的特定元件中提交問題。If you encounter any bugs with the Jenkins plugins, file an issue in the Jenkins JIRA for the specific component.

後續步驟Next steps