注意
GitHub Enterprise Server 目前不支持 GitHub 托管的运行器。 可以在 GitHub public roadmap 上查看有关未来支持计划的更多信息。
Introduction
This guide explains how to use GitHub Actions to build and deploy a project to Azure Kubernetes Service.
注意
如果 GitHub Actions 工作流需要访问支持 OpenID Connect (OIDC) 的云提供商提供的资源,则可以将工作流配置为直接向云提供商进行身份验证。 这样就可以停止将这些凭据存储为长期存在的机密,并提供其他安全优势。 有关详细信息,请参阅“关于使用 OpenID Connect 进行安全强化”。 and Configuring OpenID Connect in Azure.
Prerequisites
Before creating your GitHub Actions workflow, you will first need to complete the following setup steps:
-
Create a target AKS cluster and an Azure Container Registry (ACR). For more information, see Quickstart: Deploy an AKS cluster by using the Azure portal - Azure Kubernetes Service and Quickstart - Create registry in portal - Azure Container Registry in the Azure documentation.
-
Create a secret called
AZURE_CREDENTIALS
to store your Azure credentials. For more information about how to find this information and structure the secret, see theAzure/login
action documentation.
Creating the workflow
Once you've completed the prerequisites, you can proceed with creating the workflow.
The following example workflow demonstrates how to build and deploy a project to Azure Kubernetes Service when code is pushed to your repository.
Under the workflow env
key, change the following values:
AZURE_CONTAINER_REGISTRY
to the name of your container registryPROJECT_NAME
to the name of your projectRESOURCE_GROUP
to the resource group containing your AKS clusterCLUSTER_NAME
to the name of your AKS cluster
This workflow uses the helm
render engine for the azure/k8s-bake
action. If you will use the helm
render engine, change the value of CHART_PATH
to the path to your helm file. Change CHART_OVERRIDE_PATH
to an array of override file paths. If you use a different render engine, update the input parameters sent to the azure/k8s-bake
action.
# 此工作流使用未经 GitHub 认证的操作。 # 它们由第三方提供,并受 # 单独的服务条款、隐私政策和支持 # 文档。 # GitHub 建议将操作固定到提交 SHA。 # 若要获取较新版本,需要更新 SHA。 # 还可以引用标记或分支,但该操作可能会更改而不发出警告。 name: Build and deploy to Azure Kubernetes Service env: AZURE_CONTAINER_REGISTRY: MY_REGISTRY_NAME # set this to the name of your container registry PROJECT_NAME: MY_PROJECT_NAME # set this to your project's name RESOURCE_GROUP: MY_RESOURCE_GROUP # set this to the resource group containing your AKS cluster CLUSTER_NAME: MY_CLUSTER_NAME # set this to the name of your AKS cluster REGISTRY_URL: MY_REGISTRY_URL # set this to the URL of your registry # If you bake using helm: CHART_PATH: MY_HELM_FILE # set this to the path to your helm file CHART_OVERRIDE_PATH: MY_OVERRIDE_FILES # set this to an array of override file paths on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Azure Login uses: azure/login@14a755a4e2fd6dff25794233def4f2cf3f866955 with: creds: ${{ secrets.AZURE_CREDENTIALS }} - name: Build image on ACR uses: azure/CLI@61bb69d64d613b52663984bf12d6bac8fd7b3cc8 with: azcliversion: 2.29.1 inlineScript: | az configure --defaults acr=${{ env.AZURE_CONTAINER_REGISTRY }} az acr build -t -t ${{ env.REGISTRY_URL }}/${{ env.PROJECT_NAME }}:${{ github.sha }} - name: Gets K8s context uses: azure/aks-set-context@94ccc775c1997a3fcfbfbce3c459fec87e0ab188 with: creds: ${{ secrets.AZURE_CREDENTIALS }} resource-group: ${{ env.RESOURCE_GROUP }} cluster-name: ${{ env.CLUSTER_NAME }} id: login - name: Configure deployment uses: azure/k8s-bake@61041e8c2f75c1f01186c8f05fb8b24e1fc507d8 with: renderEngine: 'helm' helmChart: ${{ env.CHART_PATH }} overrideFiles: ${{ env.CHART_OVERRIDE_PATH }} overrides: | replicas:2 helm-version: 'latest' id: bake - name: Deploys application uses: Azure/k8s-deploy@dd4bbd13a5abd2fc9ca8bdcb8aee152bb718fa78 with: manifests: ${{ steps.bake.outputs.manifestsBundle }} images: | ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.PROJECT_NAME }}:${{ github.sha }} imagepullsecrets: | ${{ env.PROJECT_NAME }}
# 此工作流使用未经 GitHub 认证的操作。
# 它们由第三方提供,并受
# 单独的服务条款、隐私政策和支持
# 文档。
# GitHub 建议将操作固定到提交 SHA。
# 若要获取较新版本,需要更新 SHA。
# 还可以引用标记或分支,但该操作可能会更改而不发出警告。
name: Build and deploy to Azure Kubernetes Service
env:
AZURE_CONTAINER_REGISTRY: MY_REGISTRY_NAME # set this to the name of your container registry
PROJECT_NAME: MY_PROJECT_NAME # set this to your project's name
RESOURCE_GROUP: MY_RESOURCE_GROUP # set this to the resource group containing your AKS cluster
CLUSTER_NAME: MY_CLUSTER_NAME # set this to the name of your AKS cluster
REGISTRY_URL: MY_REGISTRY_URL # set this to the URL of your registry
# If you bake using helm:
CHART_PATH: MY_HELM_FILE # set this to the path to your helm file
CHART_OVERRIDE_PATH: MY_OVERRIDE_FILES # set this to an array of override file paths
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Azure Login
uses: azure/login@14a755a4e2fd6dff25794233def4f2cf3f866955
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Build image on ACR
uses: azure/CLI@61bb69d64d613b52663984bf12d6bac8fd7b3cc8
with:
azcliversion: 2.29.1
inlineScript: |
az configure --defaults acr=${{ env.AZURE_CONTAINER_REGISTRY }}
az acr build -t -t ${{ env.REGISTRY_URL }}/${{ env.PROJECT_NAME }}:${{ github.sha }}
- name: Gets K8s context
uses: azure/aks-set-context@94ccc775c1997a3fcfbfbce3c459fec87e0ab188
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
resource-group: ${{ env.RESOURCE_GROUP }}
cluster-name: ${{ env.CLUSTER_NAME }}
id: login
- name: Configure deployment
uses: azure/k8s-bake@61041e8c2f75c1f01186c8f05fb8b24e1fc507d8
with:
renderEngine: 'helm'
helmChart: ${{ env.CHART_PATH }}
overrideFiles: ${{ env.CHART_OVERRIDE_PATH }}
overrides: |
replicas:2
helm-version: 'latest'
id: bake
- name: Deploys application
uses: Azure/k8s-deploy@dd4bbd13a5abd2fc9ca8bdcb8aee152bb718fa78
with:
manifests: ${{ steps.bake.outputs.manifestsBundle }}
images: |
${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.PROJECT_NAME }}:${{ github.sha }}
imagepullsecrets: |
${{ env.PROJECT_NAME }}
Additional resources
The following resources may also be useful:
- For the original workflow template, see
azure-kubernetes-service.yml
in the GitHub Actionsstarter-workflows
repository. - The actions used to in this workflow are the official Azure
Azure/login
,Azure/aks-set-context
,Azure/CLI
,Azure/k8s-bake
, andAzure/k8s-deploy
actions. - For more examples of GitHub Action workflows that deploy to Azure, see the actions-workflow-samples repository.