|
| 1 | +# This workflow will build and push an application to a Azure Kubernetes Service (AKS) cluster when you push your code |
| 2 | +# |
| 3 | +# This workflow assumes you have already created the target AKS cluster and have created an Azure Container Registry (ACR) |
| 4 | +# The ACR should be attached to the AKS cluster |
| 5 | +# For instructions see: |
| 6 | +# - https://docs.microsoft.com/en-us/azure/aks/kubernetes-walkthrough-portal |
| 7 | +# - https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal |
| 8 | +# - https://learn.microsoft.com/en-us/azure/aks/cluster-container-registry-integration?tabs=azure-cli#configure-acr-integration-for-existing-aks-clusters |
| 9 | +# - https://github.com/Azure/aks-create-action |
| 10 | +# |
| 11 | +# To configure this workflow: |
| 12 | +# |
| 13 | +# 1. Set the following secrets in your repository (instructions for getting these |
| 14 | +# https://docs.microsoft.com/en-us/azure/developer/github/connect-from-azure?tabs=azure-cli%2Clinux): |
| 15 | +# - AZURE_CLIENT_ID |
| 16 | +# - AZURE_TENANT_ID |
| 17 | +# - AZURE_SUBSCRIPTION_ID |
| 18 | +# |
| 19 | +# 2. Set the following environment variables (or replace the values below): |
| 20 | +# - AZURE_CONTAINER_REGISTRY (name of your container registry / ACR) |
| 21 | +# - CONTAINER_NAME (name of the container image you would like to push up to your ACR) |
| 22 | +# - RESOURCE_GROUP (where your cluster is deployed) |
| 23 | +# - CLUSTER_NAME (name of your AKS cluster) |
| 24 | +# - IMAGE_PULL_SECRET_NAME (name of the ImagePullSecret that will be created to pull your ACR image) |
| 25 | +# |
| 26 | +# 3. Choose the appropriate render engine for the bake step https://github.com/Azure/k8s-bake. The config below assumes Kompose. |
| 27 | +# Set your dockerComposeFile and kompose-version to suit your configuration. |
| 28 | +# - DOCKER_COMPOSE_FILE_PATH (the path where your Kompose deployment manifest is located) |
| 29 | +# |
| 30 | +# For more information on GitHub Actions for Azure, refer to https://github.com/Azure/Actions |
| 31 | +# For more samples to get started with GitHub Action workflows to deploy to Azure, refer to https://github.com/Azure/actions-workflow-samples |
| 32 | +# For more options with the actions used below please refer to https://github.com/Azure/login |
| 33 | + |
| 34 | +name: Build and deploy an app to AKS with Kompose |
| 35 | + |
| 36 | +on: |
| 37 | + push: |
| 38 | + branches: ["main"] |
| 39 | + workflow_dispatch: |
| 40 | + |
| 41 | +env: |
| 42 | + AZURE_CONTAINER_REGISTRY: "your-azure-container-registry" |
| 43 | + CONTAINER_NAME: "your-container-name" |
| 44 | + RESOURCE_GROUP: "your-resource-group" |
| 45 | + CLUSTER_NAME: "your-cluster-name" |
| 46 | + DOCKER_COMPOSE_FILE_PATH: "your-docker-compose-file-path" |
| 47 | + |
| 48 | +jobs: |
| 49 | + buildImage: |
| 50 | + permissions: |
| 51 | + contents: read |
| 52 | + id-token: write |
| 53 | + runs-on: ubuntu-latest |
| 54 | + steps: |
| 55 | + # Checks out the repository this file is in |
| 56 | + - uses: actions/checkout@v4 |
| 57 | + |
| 58 | + # Logs in with your Azure credentials |
| 59 | + - name: Azure login |
| 60 | + uses: azure/login@v1.4.6 |
| 61 | + with: |
| 62 | + client-id: ${{ secrets.AZURE_CLIENT_ID }} |
| 63 | + tenant-id: ${{ secrets.AZURE_TENANT_ID }} |
| 64 | + subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} |
| 65 | + |
| 66 | + # Builds and pushes an image up to your Azure Container Registry |
| 67 | + - name: Build and push image to ACR |
| 68 | + run: | |
| 69 | + az acr build --image ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }} --registry ${{ env.AZURE_CONTAINER_REGISTRY }} -g ${{ env.RESOURCE_GROUP }} . |
| 70 | +
|
| 71 | + deploy: |
| 72 | + permissions: |
| 73 | + actions: read |
| 74 | + contents: read |
| 75 | + id-token: write |
| 76 | + runs-on: ubuntu-latest |
| 77 | + needs: [buildImage] |
| 78 | + steps: |
| 79 | + # Checks out the repository this file is in |
| 80 | + - uses: actions/checkout@v4 |
| 81 | + |
| 82 | + # Logs in with your Azure credentials |
| 83 | + - name: Azure login |
| 84 | + uses: azure/login@v1.4.6 |
| 85 | + with: |
| 86 | + client-id: ${{ secrets.AZURE_CLIENT_ID }} |
| 87 | + tenant-id: ${{ secrets.AZURE_TENANT_ID }} |
| 88 | + subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} |
| 89 | + |
| 90 | + # Use kubelogin to configure your kubeconfig for Azure auth |
| 91 | + - name: Set up kubelogin for non-interactive login |
| 92 | + uses: azure/use-kubelogin@v1 |
| 93 | + with: |
| 94 | + kubelogin-version: 'v0.0.25' |
| 95 | + |
| 96 | + # Retrieves your Azure Kubernetes Service cluster's kubeconfig file |
| 97 | + - name: Get K8s context |
| 98 | + uses: azure/aks-set-context@v3 |
| 99 | + with: |
| 100 | + resource-group: ${{ env.RESOURCE_GROUP }} |
| 101 | + cluster-name: ${{ env.CLUSTER_NAME }} |
| 102 | + admin: 'false' |
| 103 | + use-kubelogin: 'true' |
| 104 | + |
| 105 | + # Runs Kompose to create manifest files |
| 106 | + - name: Bake deployment |
| 107 | + uses: azure/k8s-bake@v2 |
| 108 | + with: |
| 109 | + renderEngine: "kompose" |
| 110 | + dockerComposeFile: ${{ env.DOCKER_COMPOSE_FILE_PATH }} |
| 111 | + kompose-version: "latest" |
| 112 | + id: bake |
| 113 | + |
| 114 | + # Deploys application based on manifest files from previous step |
| 115 | + - name: Deploy application |
| 116 | + uses: Azure/k8s-deploy@v4 |
| 117 | + with: |
| 118 | + action: deploy |
| 119 | + manifests: ${{ steps.bake.outputs.manifestsBundle }} |
| 120 | + images: | |
| 121 | + ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }} |
0 commit comments