教學課程:使用 Azure Dev Spaces 搭配 Azure Kubernetes ServiceTutorial: Use Azure Dev Spaces with Azure Kubernetes Service

Azure Dev Spaces 可讓您測試並反覆開發在 Azure Kubernetes Service (AKS) 中執行的微服務應用程式,無需複製或模擬相依性。Azure Dev Spaces allows you to test and iteratively develop your microservice application running in Azure Kubernetes Service (AKS) without the need to replicate or mock dependencies. 適用於 Jenkins 的 Azure Dev Spaces 外掛程式可協助您在持續整合和持續傳遞 (CI/CD) 管線中使用 Dev Spaces。The Azure Dev Spaces plug-in for Jenkins helps you use Dev Spaces in your continuous integration and delivery (CI/CD) pipeline.

此教學課程也使用 Azure Container Registry (ACR)。This tutorial also uses Azure Container Registry (ACR). ACR 會儲存影像、ACR 工作組建 Docker 和 Helm 成品。ACR stores images, and an ACR Task builds Docker and Helm artifacts. 將 ACR 和 ACR 工作用於產生成品,便不再需要在您的 Jenkins 伺服器上安裝其他軟體 (例如,Docker)。Using ACR and ACR Task for artifact generation removes the need for you to install additional software, such as Docker, on your Jenkins server.

在本教學課程中,您將完成下列工作:In this tutorial, you'll complete these tasks:

  • 建立啟用 AKS 叢集的 Azure Dev SpacesCreate an Azure Dev Spaces enabled AKS cluster
  • 將多服務應用程式部署至 AKSDeploy a multi-service application to AKS
  • 準備 Jenkins 伺服器Prepare your Jenkins server
  • 在 Jenkins 管線中使用 Azure Dev Spaces 外掛程式,以在將程式碼合併至專案中之前檢視它們Use the Azure Dev Spaces plug-in in a Jenkins pipeline to preview code changes before merging them into the project

此教學課程假設您有核心 Azure 服務、AKS、ACR、Azure Dev Spaces、Jenkins 管線與外掛程式和 GitHub 的中級程度知識。This tutorial assumes intermediate knowledge of core Azure services, AKS, ACR, Azure Dev Spaces, Jenkins pipelines and plug-ins, and GitHub. 對 kubectl 和 Helm 等支援工具的基本程度熟悉很有幫助。Basic familiarity with supporting tools such as kubectl and Helm is helpful.

PrerequisitesPrerequisites

  • 一個 Azure 帳戶。An Azure account. 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don’t have an Azure subscription, create a free account before you begin.

  • GitHub 帳戶。A GitHub account. 如果您沒有 GitHub 帳戶,請在開始之前建立免費帳戶 (英文)。If you don't have a GitHub account, create a free account before you begin.

  • 已安裝 Azure Dev Spaces (英文) 擴充功能的 Visual Studio Code (英文)。Visual Studio Code with the Azure Dev Spaces extension installed.

  • 已安裝 Azure CLI (2.0.43 版或更新版本)。Azure CLI installed, version 2.0.43 or higher.

  • Jenkins 主要伺服器。A Jenkins master server. 如果您還沒有 Jenkins Master,請遵循此 快速入門步驟在 Azure 上部署 Jenkins 。If you don't already have a Jenkins master, deploy Jenkins on Azure by following the steps in this quickstart.

  • Jenkins 伺服器必須已安裝 Helm 和 kubectl 安裝且可供 Jenkins 帳戶取得,如稍後在此教學課程中所述。The Jenkins server must have both Helm and kubectl installed and available to the Jenkins account, as explained later in this tutorial.

  • VS Code、VS Code Terminal 或 WSL 和 Bash。VS Code, the VS Code Terminal or WSL, and Bash.

建立 Azure 資源Create Azure resources

在此節中,您會建立 Azure 資源:In this section, you create Azure resources:

  1. 建立資源群組。Create a resource group.

    az group create --name MyResourceGroup --location westus2
    
  2. 建立 AKS 叢集。Create an AKS cluster. 支援 Dev Spaces 的區域 (部分機器翻譯) 建立 AKS 叢集。Create the AKS cluster in a region that supports Dev Spaces.

    az aks create --resource-group MyResourceGroup --name MyAKS --location westus2 --kubernetes-version 1.11.9 --enable-addons http_application_routing --generate-ssh-keys --node-count 1 --node-vm-size Standard_D1_v2
    
  3. 設定 AKS 使用 Dev Spaces。Configure AKS to use Dev Spaces.

    az aks use-dev-spaces --resource-group MyResourceGroup --name MyAKS
    

    此步驟會安裝 azds CLI 擴充功能。This step installs the azds CLI extension.

  4. 建立容器登錄庫。Create a container registry.

    az acr create -n MyACR -g MyResourceGroup --sku Basic --admin-enabled true
    

將範例應用程式部署至 AKS 叢集Deploy sample apps to the AKS cluster

在此節中,您會設定開發空間,並將範例應用程式部署到您在上一節中建立的 AKS 叢集。In this section, you set up a dev space and deploy a sample application to the AKS cluster you created in the last section. 應用程式由兩部分所組成 webfrontendmywebapiThe application consists of two parts, webfrontend and mywebapi. 兩個元件都部署在開發空間中。Both components are deployed in a dev space. 稍後在此教學課程中,您將針對 mywebapi 提交提取要求,以觸發 Jenkins 中的 CI 管線。Later in this tutorial, you'll submit a pull request against mywebapi to trigger the CI pipeline in Jenkins.

如需搭配使用 Azure Dev Spaces 和多服務開發與 Azure Dev Spaces 的詳細資訊,請參閱在使用 Java 的 Azure Dev Spaces 上開始使用使用 Azure Dev Spaces 進行多服務開發For more information on using Azure Dev Spaces and multi-service development with Azure Dev Spaces, see Get started on Azure Dev Spaces with Java, and Multi-service development with Azure Dev Spaces. 這些教學課程提供此文章並未提及的額外背景資訊。Those tutorials provide additional background information not included here.

  1. 從 GitHub 下載 https://github.com/Azure/dev-spaces 存放庫。Download the https://github.com/Azure/dev-spaces repo from GitHub.

  2. 在 VS Code 中開啟 samples/java/getting-started/webfrontend 資料夾。Open the samples/java/getting-started/webfrontend folder in VS Code. (您可以略過要新增偵錯資產或還原專案的任何預設提示。)(You can ignore any default prompts to add debug assets or restore the project.)

  3. /src/main/java/com/ms/sample/webfrontend/Application.java 更新為看起來如下所示︰Update /src/main/java/com/ms/sample/webfrontend/Application.java to look like the following:

    package com.ms.sample.webfrontend;
    
    import java.io.*;
    import java.net.*;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.*;
    
    @SpringBootApplication
    @RestController
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @RequestMapping(value = "/greeting", produces = "text/plain")
        public String greeting(@RequestHeader(value = "azds-route-as", required = false) String azdsRouteAs) throws Exception {
            URLConnection conn = new URL("http://mywebapi/").openConnection();
            conn.setRequestProperty("azds-route-as", azdsRouteAs); // propagate dev space routing header
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())))
            {
                return "Hello from webfrontend and " + reader.lines().reduce("\n", String::concat);
            }
        }
    }
    
  4. 依序按一下 [檢視] 、[終端機] ,以在 VS Code 中開啟整合式終端機。Click View then Terminal to open the Integrated Terminal in VS Code.

  5. 執行 azds prep 命令,備妥您的應用程以在開發空間中執行。Run the azds prep command to prepare your application to run in a dev space. 此命令必須從 dev-spaces/samples/java/getting-started/webfrontend 執行,以正確地準備您的應用程式:This command must be run from dev-spaces/samples/java/getting-started/webfrontend to prepare your application correctly:

    azds prep --public
    

    Dev Spaces CLI 的 azds prep 命令會產生具有預設設定的 Docker 和 Kubernetes 資產。The Dev Spaces CLI's azds prep command generates Docker and Kubernetes assets with default settings. 在專案存留期期間,系統會保存這些檔案,且您可自訂它們:These files persist for the lifetime of the project, and they can be customized:

    • ./Dockerfile./Dockerfile.develop 描述應用程式的容器映像,以及如何建置原始程式碼和在容器內執行。./Dockerfile and ./Dockerfile.develop describe the app's container image, and how the source code is built and runs within the container.
    • ./charts/webfrontend 底下的 Helm 圖表會說明如何將容器部署至 Kubernetes。A Helm chart under ./charts/webfrontend describes how to deploy the container to Kubernetes.
    • ./azds.yaml 是 Azure Dev Spaces 組態檔。./azds.yaml is the Azure Dev Spaces configuration file.

    如需詳細資訊,請參閱 Azure Dev Spaces 的運作和設定方式 (部分機器翻譯)。For more information, see How Azure Dev Spaces works and is configured.

  6. 使用 azds up 命令在 AKS 中建置和執行應用程式:Build and run the application in AKS using the azds up command:

    azds up
    

    掃描主控台輸出,以取得 up 命令所建立之 URL 的相關資訊。Scan the console output for information about the URL that was created by the up command. 它會在表單中:It will be in the form:

    (pending registration) Service 'webfrontend' port 'http' will be available at '<url>'
    Service 'webfrontend' port 80 (TCP) is available at 'http://localhost:<port>'
    

    在瀏覽器視窗中開啟此 URL,您應該會看到 Web 應用程式。Open this URL in a browser window, and you should see the web app. 當容器執行時,stdoutstderr 輸出會串流至終端機視窗。As the container executes, stdout and stderr output is streamed to the terminal window.

  7. 接下來,設定和部署 mywebapiNext, set up and deploy mywebapi:

    1. 變更目錄為 dev-spaces/samples/java/getting-started/mywebapiChange directory to dev-spaces/samples/java/getting-started/mywebapi

    2. 執行Run

      azds prep
      
    3. 執行Run

      azds up -d
      

準備 Jenkins 伺服器Prepare Jenkins server

在此節中,您會準備 Jenkins 伺服器,以執行範例 CI 管線。In this section, you prepare the Jenkins server to run the sample CI pipeline.

  • 安裝外掛程式Install plug-ins
  • 安裝 Helm 和 Kubernetes CLIInstall Helm and Kubernetes CLI
  • 新增認證Add credentials

安裝外掛程式Install plug-ins

  1. 登入您的 Jenkins 伺服器。Sign in to your Jenkins server. 選擇 [管理 Jenkins] > [管理外掛程式] 。Choose Manage Jenkins > Manage Plugins.

  2. 在 [可用的] 索引標籤上,選取下列外掛程式:On the Available tab, select the following plug-ins:

    如果這些外掛程式未出現在清單中,請查看 [已安裝] 索引標籤,確認它們是否已安裝。If these plug-ins don't appear in the list, check the Installed tab to see if they're already installed.

  3. 若要安裝您選取的外掛程式,請選擇 [立即下載並於重新啟動後安裝] 。To install the plug-ins, choose Download now and install after restart.

  4. 重新啟動您的 Jenkins 伺服器以完成安裝。Restart your Jenkins server to complete the installation.

安裝 Helm 和 kubectlInstall Helm and kubectl

範例管線使用 Helm 和 kubectl 部署至開發空間。The sample pipeline uses Helm and kubectl to deploy to the dev space. 安裝 Jenkins 時,它會建立名為 jenkins 的系統管理員帳戶。When Jenkins is installed, it creates an admin account named jenkins. Helm 和 kubectl 必須可供 jenkins 使用者存取。Both Helm and kubectl need to be accessible to the jenkins user.

  1. 建立對 Jenkins Master 的 SSH 連線。Make an SSH connection to the Jenkins master.

  2. 切換至 jenkins 使用者:Switch to the jenkins user:

    sudo su jenkins
    
  3. 安裝 Helm CLI。Install the Helm CLI. 如需詳細資訊,請參閱安裝 Helm (英文)。For more information, see Installing Helm.

  4. 安裝 kubectl。Install kubectl. 如需詳細資訊,請參閱 az acs kubernetes install-cli (英文)。For more information, see az acs kubernetes install-cli.

將認證新增至 JenkinsAdd credentials to Jenkins

  1. Jenkins 需要 Azure 服務主體來驗證和存取 Azure 資源。Jenkins needs an Azure service principal for authenticating and accessing Azure resources. 若要建立服務主體,請參閱部署到 Azure App Service 教學課程中的 建立服務主體 一節。To create the service principal, Refer to the Create service principal section in the Deploy to Azure App Service tutorial. 請務必儲存一份 create-for-rbac 的輸出,因為您需要該資訊來完成下一個步驟。Be sure to save a copy of the output from create-for-rbac because you need that information to complete the next step. 輸出會看起來類似這樣:The output will look something like this:

    {
      "appId": "f4150da1-xxx-xxxx-xxx-xxxxxxxxxxxx",
      "displayName": "xxxxxxxjenkinssp",
      "name": "http://xxxxxxxjenkinssp",
      "password": "f6e4d839-xxx-xxxx-xxx-xxxxxxxxxxxx",
      "tenant": "72f988bf--xxx-xxxx-xxx-xxxxxxxxxxxx"
    }
    
  2. 使用來自上一步的 Azure 服務主體,在 Jenkins 中新增「Microsoft Azure 服務主體」 認證類型。Add a Microsoft Azure Service Principal credential type in Jenkins, using the service principal information from the previous step. 以下螢幕擷取畫面中的名稱與 create-for-rbac 的輸出對應。The names in the screenshot below correspond to the output from create-for-rbac.

    Jenkins 中的 [ID] 欄位是您服務主體的認證名稱。The ID field is the Jenkins credential name for your service principal. 此範例會使用 displayName 值 (在此案例中為 xxxxxxxjenkinssp),但您可以使用任何您想要的文字。The example uses the value of displayName (in this instance, xxxxxxxjenkinssp), but you can use any text you want. 此認證名稱是下一節中 AZURE_CRED_ID 環境變數的值。This credential name is the value for the AZURE_CRED_ID environment variable in the next section.

    新增 Jenkins 服務主體認證

    [描述] 是選擇性的。The Description is optional. 如需詳細指示,請參閱部署至 Azure App Service 教學課程中的將服務主體新增至 Jenkins  一節。For more detailed instructions, see Add service principal to Jenkins section in the Deploy to Azure App Service tutorial.

  3. 若要顯示您的 ACR 認證,請執行此命令:To show your ACR credentials, run this command:

    az acr credential show -n <yourRegistryName>
    

    建立 JSON 輸出的副本,應該看起來像這樣:Make a copy of the JSON output, which should look something like this:

    {
      "passwords": [
        {
          "name": "password",
          "value": "vGBP=zzzzzzzzzzzzzzzzzzzzzzzzzzz"
        },
        {
          "name": "password2",
          "value": "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
        }
      ],
      "username": "acr01"
    }
    
  4. 在 Jenkins 中新增「使用者名稱與密碼」 認證類型。Add a Username with password credential type in Jenkins. username 是從最後一個步驟中的使用者名稱,在此範例中為 acr01The username is the username from the last step, in this example acr01. password 是第一個密碼的值,在此範例中是 vGBP=zzzzzzzzzzzzzzzzzzzzzzzzzzzThe password is the value for the first password, in this example vGBP=zzzzzzzzzzzzzzzzzzzzzzzzzzz. 此認證的 ID 是 ACR_CRED_ID 的值。The ID of this credential is the value of ACR_CRED_ID.

  5. 設定 AKS 認證。Set up an AKS credential. 新增 Jenkins 中的「Kubernetes 設定 (kubeconfig)」 認證類型 (使用 [直接輸入] 選項)。Add a Kubernetes configuration (kubeconfig) credential type in Jenkins (use the option "Enter directly"). 若要取得您 AKS 叢集的存取認證,請執行下列命令:To get the access credentials for your AKS cluster, run the following command:

    az aks get-credentials -g MyResourceGroup -n <yourAKSName> -f -
    

    此認證的 ID 是下一節中 KUBE_CONFIG_ID 的值。The ID this credential is the value of KUBE_CONFIG_ID in the next section.

建立管線Create a pipeline

為範例管線所選的案例是以實際模式為基礎:提取要求觸發 CI 管線將建議變更建置並部署到 Azure 開發空間,以進行測試和檢閱。The scenario selected for the example pipeline is based on a real-world pattern: A pull request triggers a CI pipeline that builds and then deploys the proposed changes to an Azure dev space for testing and review. 根據檢閱的結果,所做的變更會合併並部署到 AKS 或捨棄。Depending on the outcome of the review, the changes are either merged and deployed to AKS or discarded. 最後,系統會移除開發空間。Finally, the dev space is removed.

Jenkins 管線設定和 Jenkinsfile 定義 CI 管線中的階段。The Jenkins pipeline configuration and Jenkinsfile define the stages in the CI pipeline. 此流程圖顯示管線階段和 Jenkinsfile 定義的決策點:This flowchart shows the pipeline stages and decision points defined by the Jenkinsfile:

Jenkins 管線流程

  1. https://github.com/azure-devops/mywebapi 下載 mywebapi 專案的修改版本。Download a modified version of the mywebapi project from https://github.com/azure-devops/mywebapi. 此專案包含建立管線所需的數個檔案,包括 JenkinsfileDockerfile 和 Helm 圖表。This project contains several files needed to create a pipeline, including the Jenkinsfile, Dockerfiles, and Helm chart.

  2. 登入 Jenkins。Log into Jenkins. 從左側功能表選取 [新增項目] 。From the menu on the left, select Add Item.

  3. 選取 [管線] ,然後在 [輸入項目名稱] 方塊中輸入名稱。Select Pipeline, and then enter a name in the Enter an item name box. 選取 [確定] ,然後管線設定畫面會自動開啟。Select OK, and then the pipeline configuration screen will automatically open.

  4. 在 [一般] 索引標籤上,選取 [準備執行環境] 。On the General tab, check Prepare an environment for the run.

  5. 選取 [保留 Jenkins 環境變數] 和 [保留 Jenkins 組建變數] 。Check Keep Jenkins Environment Variables and Keep Jenkins Build Variables.

  6. 在 [內容屬性] 方塊中,輸入下列環境變數:In the Properties Content box, enter the following environment variables:

    AZURE_CRED_ID=[your credential ID of service principal]
    RES_GROUP=[your resource group of the function app]
    ACR_RES_GROUP=[your ACR resource group]
    ACR_NAME=[your ACR name]
    ACR_REGISTRY=[your ACR registry url, without http schema]
    ACR_CRED_ID=[your credential id of your ACR account]
    AKS_RES_GROUP=[your AKS resource group]
    AKS_NAME=[your AKS name]
    IMAGE_NAME=[name of Docker image you will push to ACR, without registry prefix]
    KUBE_CONFIG_ID=[your kubeconfig id]
    PARENT_DEV_SPACE=[shared dev space name]
    TEST_ENDPOINT=[your web frontend end point for testing. Should be webfrontend.XXXXXXXXXXXXXXXXXXX.xxxxxx.aksapp.io]
    

    使用上一節中給定的範例值,環境變數清單應該看起來像這樣:Using the sample values given in the preceding sections, the list of environment variables should look something like this:

    Jenkins 管線環境變數

  7. 選擇 [管線] > [定義] 中的 [來自 SCM 的管線指令碼] 。Choose Pipeline script from SCM in Pipeline > Definition.

  8. 在 [SCM] 中,選擇 [Git] ,然後輸入您的存放庫 URL。In SCM, choose Git and then enter your repo URL.

  9. 在 [分支指定名稱] 中,輸入 refs/remotes/origin/${GITHUB_PR_SOURCE_BRANCH}In Branch Specifier, enter refs/remotes/origin/${GITHUB_PR_SOURCE_BRANCH}.

  10. 填入 SCM 存放庫 URL 和指令碼路徑 "Jenkinsfile"。Fill in the SCM repo URL and script path "Jenkinsfile".

  11. 應選取 [輕量簽出] 。Lightweight checkout should be checked.

建立提取要求來觸發管線Create a pull request to trigger the pipeline

若要完成此節中的步驟 3,您必須將 Jenkinsfile 的一部分註解化,否則當您嘗試並排檢視新版本和舊版本時會收到 404 錯誤。To complete step 3 in this section, you will need to comment part of the Jenkinsfile, otherwise you will get a 404 error when you try to view the new and old versions side by side. 根據預設,當您選擇要合併的 PR 時,共用的舊版 mywebapi 會被移除並取代為新版。By default, when you choose to merge the PR, the previous shared version of mywebapi will be removed and replaced by the new version. 完成步驟 1 之前,先對 Jenkinsfile 進行下列變更:Make the following change to the Jenkinsfile before completing step 1:

    if (userInput == true) {
        stage('deploy') {
            // Apply the deployment to shared namespace in AKS using acsDeploy, Helm or kubectl   
        }
        
        stage('Verify') {
            // verify the staging environment is working properly
        }
        
        /* Comment the cleanup stage to allow side by side comparison with the new child dev space

        stage('cleanup') {
            devSpacesCleanup aksName: env.AKS_NAME, 
                azureCredentialsId: env.AZURE_CRED_ID, 
                devSpaceName: devSpaceNamespace, 
                kubeConfigId: env.KUBE_CONFIG_ID, 
                resourceGroupName: env.AKS_RES_GROUP,
                helmReleaseName: releaseName
        }
        */

    } else {
        // Send a notification
    }
  1. 變更 mywebapi/src/main/java/com/ms/sample/mywebapi/Application.java 然後建立提取要求。Make a change to mywebapi/src/main/java/com/ms/sample/mywebapi/Application.javaand then create a pull request. 例如:For example:

    public String index() {
        return "Hello from mywebapi in my private dev space";
    }
    
  2. 登入 Jenkins 並選取管線名稱,然後選擇 [立即建置] 。Sign into Jenkins and select the pipeline name, and then choose Build Now.

    您也可以設定 webhook,以自動觸發 Jenkins 管線。You can also set up a webhook to automatically trigger the Jenkins pipeline. 輸入提取要求後,GitHub 會發出 POST 到 Jenkins 進而觸發管線。When a pull request is entered, GitHub issues a POST to Jenkins, triggering the pipeline. 如需有關設定 Webhook 的詳細資訊,請參閱將 Jenkins 連線到 GitHubFor more information about setting up a webhook, see Connect Jenkins to GitHub.

  3. 比較目前共用版本的變更:Compare changes to the current shared version:

    1. 開啟瀏覽器並瀏覽至共用版本 https://webfrontend.XXXXXXXXXXXXXXXXXXX.eastus.aksapp.ioOpen your browser and navigate to the shared version https://webfrontend.XXXXXXXXXXXXXXXXXXX.eastus.aksapp.io. TEST_ENDPOINT 包含 URL。TEST_ENDPOINT contains the URL.

    2. 開啟另一個索引標籤,然後輸入 PR 開發空間 URL。Open another tab and then enter the PR dev space URL. 該屬性將類似於 https://<yourdevspacename>.s.webfrontend.XXXXXXXXXXXXXXXXXXX.eastus.aksapp.ioIt will be similar to https://<yourdevspacename>.s.webfrontend.XXXXXXXXXXXXXXXXXXX.eastus.aksapp.io. 您會在 Jenkins 作業的 [建置歷程記錄] > [<組建#>] > [主控台輸出] 中找到連結。You'll find the link in Build History > <build#> > Console Output for the Jenkins job. 在頁面上搜尋 aksapp,或只查看前置詞,搜尋 azdsprefixSearch the page for aksapp, or to see only the prefix, search for azdsprefix.

建構子開發空間的 URLConstructing the URL to the child dev space

當您發出提取要求時,Jenkins 會根據小組的公用開發空間建立子開發空間,並在該子開發空間中執行來自提取要求的程式碼。When you file a pull request, Jenkins creates a child dev space based on the team's shared dev space and runs the code from your pull request in that child dev space. 子開發空間的 URL 格式為 http://$env.azdsprefix.<test_endpoint>The URL to the child dev space takes the form http://$env.azdsprefix.<test_endpoint>.

Azure Dev Spaces 外掛程式會在管線執行期間藉由 devSpacesCreate 來設定 $env.azdsprefix$env.azdsprefix is set during pipeline execution by the Azure Dev Spaces plug-in by devSpacesCreate:

stage('create dev space') {
    devSpacesCreate aksName: env.AKS_NAME,
        azureCredentialsId: env.AZURE_CRED_ID,
        kubeconfigId: env.KUBE_CONFIG_ID,
        resourceGroupName: env.AKS_RES_GROUP,
        sharedSpaceName: env.PARENT_DEV_SPACE,
        spaceName: devSpaceNamespace
}

test_endpoint 是您先前在將範例應用程式部署至 AKS 叢集 (步驟 7) 中使用 azds up 設定的 webfrontend 應用程式 URL。The test_endpoint is the URL to the webfrontend app you previously deployed using azds upin Deploy sample apps to the AKS cluster, Step 7. $env.TEST_ENDPOINT 的值設定在管線設定中。The value of $env.TEST_ENDPOINT is set in the pipeline configuration.

下列程式碼片段顯示子開發空間 URL 如何用於 smoketest 階段中。The following code snippet shows how the child dev space URL is used in the smoketest stage. 程式碼會檢查子開發空間 TEST_ENDPOINT 是否可供使用,如果可用,便會將問候語下載到 stdout:The code checks to see if the child dev space TEST_ENDPOINT is available, and if so, downloads the greeting text to stdout:

stage('smoketest') {
    // CI testing against http://$env.azdsprefix.$env.TEST_ENDPOINT" 
    SLEEP_TIME = 30
    SITE_UP = false
    for (int i = 0; i < 10; i++) {
        sh "sleep ${SLEEP_TIME}"
        code = "0"
        try {
            code = sh returnStdout: true, script: "curl -sL -w '%{http_code}' 'http://$env.azdsprefix.$env.TEST_ENDPOINT/greeting' -o /dev/null"
        } catch (Exception e){
            // ignore
        }
        if (code == "200") {
            sh "curl http://$env.azdsprefix.$env.TEST_ENDPOINT/greeting"
            SITE_UP = true
            break
        }
    }
    if(!SITE_UP) {
        echo "The site has not been up after five minutes"
    }
}

清除資源Clean up resources

當您使用完範例應用程式之後,請刪除資源群組以清除 Azure 資源:When you're done using the sample application, clean up Azure resources by deleting the resource group:

az group delete -y --no-wait -n MyResourceGroup

後續步驟Next steps