{"meta":{"title":"部署到 Azure Kubernetes 服务","intro":"了解如何将项目部署到Azure Kubernetes Service (AKS)作为持续部署（CD）工作流的一部分。","product":"GitHub Actions","breadcrumbs":[{"href":"/zh/actions","title":"GitHub Actions"},{"href":"/zh/actions/how-tos","title":"操作方法"},{"href":"/zh/actions/how-tos/deploy","title":"部署"},{"href":"/zh/actions/how-tos/deploy/deploy-to-third-party-platforms","title":"部署到第三方平台"},{"href":"/zh/actions/how-tos/deploy/deploy-to-third-party-platforms/azure-kubernetes-service","title":"Azure Kubernetes 服务"}],"documentType":"article"},"body":"# 部署到 Azure Kubernetes 服务\n\n了解如何将项目部署到Azure Kubernetes Service (AKS)作为持续部署（CD）工作流的一部分。\n\n## 先决条件\n\n在创建 GitHub Actions 工作流程之前，首先需要完成以下设置步骤：\n\n1. 创建目标 AKS 群集和Azure Container Registry（ACR）。 有关详细信息，请参阅[Quickstart：使用 Azure 门户部署 AKS 群集 - Azure Kubernetes Service](https://docs.microsoft.com/azure/aks/kubernetes-walkthrough-portal) 和 [Quickstart - 在门户中创建注册表 - Azure Container Registry](https://docs.microsoft.com/azure/container-registry/container-registry-get-started-portal) Azure 文档中。\n\n1. 创建名为 `AZURE_CREDENTIALS` 的机密来存储Azure凭据。 有关如何查找此信息和构造机密的详细信息，请参阅 [`Azure/login`操作文档](https://github.com/Azure/login#configure-a-service-principal-with-a-secret)。\n\n## 创建工作流程\n\n完成先决条件后，可以继续创建工作流程。\n\n以下示例工作流演示如何在将代码推送到存储库时生成项目并将其部署到Azure Kubernetes Service。\n\n在工作流 `env` 键下，更改以下值：\n* 将 `AZURE_CONTAINER_REGISTRY` 替换为容器注册表的名称\n* 在项目的名称后添加`PROJECT_NAME`\n* 将 `RESOURCE_GROUP` 指向包含 AKS 群集的资源组\n*               将 `CLUSTER_NAME` 更改为 AKS 群集名称\n\n此工作流使用 `helm` 渲染引擎执行 [`azure/k8s-bake` 操作](https://github.com/Azure/k8s-bake)。 如果将使用 `helm` 呈现引擎，请将 `CHART_PATH` 的值更改为 helm 文件的路径。 将 `CHART_OVERRIDE_PATH` 更改为覆盖文件路径数组。 如果使用其他呈现引擎，请更新发送到 `azure/k8s-bake` 操作的输入参数。\n\n```yaml copy\n# 此工作流使用未经 GitHub 认证的操作。\n# 它们由第三方提供，并受\n# 单独的服务条款、隐私政策和支持\n# 文档。\n\n# GitHub 建议将操作固定到提交 SHA。\n# 若要获取较新版本，需要更新 SHA。\n# 还可以引用标记或分支，但该操作可能会更改而不发出警告。\n\nname: Build and deploy to Azure Kubernetes Service\n\nenv:\n  AZURE_CONTAINER_REGISTRY: MY_REGISTRY_NAME # set this to the name of your container registry\n  PROJECT_NAME: MY_PROJECT_NAME              # set this to your project's name\n  RESOURCE_GROUP: MY_RESOURCE_GROUP          # set this to the resource group containing your AKS cluster\n  CLUSTER_NAME: MY_CLUSTER_NAME              # set this to the name of your AKS cluster\n  REGISTRY_URL: MY_REGISTRY_URL              # set this to the URL of your registry\n  # If you bake using helm:\n  CHART_PATH: MY_HELM_FILE                   # set this to the path to your helm file\n  CHART_OVERRIDE_PATH: MY_OVERRIDE_FILES     # set this to an array of override file paths\n\non: [push]\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n    steps:\n    - uses: actions/checkout@v6\n\n    - name: Azure Login\n      uses: azure/login@14a755a4e2fd6dff25794233def4f2cf3f866955\n      with:\n        creds: ${{ secrets.AZURE_CREDENTIALS }}\n\n    - name: Build image on ACR\n      uses: azure/CLI@61bb69d64d613b52663984bf12d6bac8fd7b3cc8\n      with:\n        azcliversion: 2.29.1\n        inlineScript: |\n          az configure --defaults acr=${{ env.AZURE_CONTAINER_REGISTRY }}\n          az acr build -t -t ${{ env.REGISTRY_URL }}/${{ env.PROJECT_NAME }}:${{ github.sha }}\n\n    - name: Gets K8s context\n      uses: azure/aks-set-context@94ccc775c1997a3fcfbfbce3c459fec87e0ab188\n      with:\n          creds: ${{ secrets.AZURE_CREDENTIALS }}\n          resource-group: ${{ env.RESOURCE_GROUP }}\n          cluster-name: ${{ env.CLUSTER_NAME }}\n      id: login\n\n    - name: Configure deployment\n      uses: azure/k8s-bake@61041e8c2f75c1f01186c8f05fb8b24e1fc507d8\n      with:\n        renderEngine: 'helm'\n        helmChart: ${{ env.CHART_PATH }}\n        overrideFiles: ${{ env.CHART_OVERRIDE_PATH }}\n        overrides: |\n          replicas:2\n        helm-version: 'latest'\n      id: bake\n\n    - name: Deploys application\n      uses: Azure/k8s-deploy@dd4bbd13a5abd2fc9ca8bdcb8aee152bb718fa78\n      with:\n        manifests: ${{ steps.bake.outputs.manifestsBundle }}\n        images: |\n          ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.PROJECT_NAME }}:${{ github.sha }}\n        imagepullsecrets: |\n          ${{ env.PROJECT_NAME }}\n```\n\n## 其他阅读材料\n\n* 对于原始工作流模板，请参阅 GitHub Actions `azure-kubernetes-service.yml` 存储库中的 [](https://github.com/actions/starter-workflows/blob/main/deployments/azure-kubernetes-service.yml)。\n* 此工作流中使用的操作是官方的 Azure [`Azure/login`](https://github.com/Azure/login)、[`Azure/aks-set-context`](https://github.com/Azure/aks-set-context)、[`Azure/CLI`](https://github.com/Azure/CLI)、[`Azure/k8s-bake`](https://github.com/Azure/k8s-bake) 和 [`Azure/k8s-deploy`](https://github.com/Azure/k8s-deploy)操作。\n* 有关部署到 Azure 的 GitHub 操作工作流的更多示例，请参阅 [actions-workflow-samples](https://github.com/Azure/actions-workflow-samples) 存储库。"}