Files
starter-workflows/deployments/azure-kubernetes-service.yml
T

143 lines
5.8 KiB
YAML
Raw Normal View History

2021-10-28 22:58:17 -04:00
# This workflow will build and push an application to a Azure Kubernetes Service (AKS) cluster when you push your code
#
# This workflow assumes you have already created the target AKS cluster and have created an Azure Container Registry (ACR)
2022-03-03 22:43:12 +05:30
# For instructions see:
# - https://docs.microsoft.com/en-us/azure/aks/kubernetes-walkthrough-portal
# - https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal
# - https://github.com/Azure/aks-create-action
2021-10-28 22:58:17 -04:00
#
# To configure this workflow:
#
# 1. Set the following secrets in your repository (instructions for getting these can be found at https://docs.microsoft.com/en-us/azure/developer/github/connect-from-azure?tabs=azure-cli%2Clinux):
2022-03-03 22:43:12 +05:30
# - AZURE_CLIENT_ID
# - AZURE_TENANT_ID
# - AZURE_SUBSCRIPTION_ID
2021-10-28 22:58:17 -04:00
#
# 2. Set the following environment variables (or replace the values below):
2022-03-03 22:43:12 +05:30
# - AZURE_CONTAINER_REGISTRY (name of your container registry / ACR)
2021-10-28 22:58:17 -04:00
# - RESOURCE_GROUP (where your cluster is deployed)
# - CLUSTER_NAME (name of your AKS cluster)
2022-03-03 22:43:12 +05:30
# - CONTAINER_NAME (name of the container image you would like to push up to your ACR)
# - IMAGE_PULL_SECRET_NAME (name of the ImagePullSecret that will be created to pull your ACR image)
2022-03-03 22:43:12 +05:30
# - DEPLOYMENT_MANIFEST_PATH (path to the manifest yaml for your deployment)
2021-10-28 22:58:17 -04:00
#
# For more information on GitHub Actions for Azure, refer to https://github.com/Azure/Actions
# For more samples to get started with GitHub Action workflows to deploy to Azure, refer to https://github.com/Azure/actions-workflow-samples
2022-03-03 22:43:12 +05:30
# For more options with the actions used below please refer to https://github.com/Azure/login
2021-10-28 22:58:17 -04:00
2022-03-03 22:43:12 +05:30
name: Build and deploy an app to AKS
on:
push:
2022-08-30 10:13:31 -04:00
branches: [ $default-branch ]
2022-03-03 22:43:12 +05:30
workflow_dispatch:
env:
AZURE_CONTAINER_REGISTRY: "your-azure-container-registry"
CONTAINER_NAME: "your-container-name"
RESOURCE_GROUP: "your-resource-group"
CLUSTER_NAME: "your-cluster-name"
IMAGE_PULL_SECRET_NAME: "your-image-pull-secret-name"
DEPLOYMENT_MANIFEST_PATH: 'your-deployment-manifest-path'
2021-10-28 22:58:17 -04:00
jobs:
buildImage:
2022-03-03 22:43:12 +05:30
permissions:
contents: read
id-token: write
2021-10-28 22:58:17 -04:00
runs-on: ubuntu-latest
steps:
2022-03-03 22:43:12 +05:30
# Checks out the repository this file is in
2022-03-28 13:10:48 -04:00
- uses: actions/checkout@v3
2021-10-28 22:58:17 -04:00
2022-03-03 22:43:12 +05:30
# Logs in with your Azure credentials
- name: Azure login
uses: azure/[email protected]
2021-10-28 22:58:17 -04:00
with:
2022-03-03 22:43:12 +05:30
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
# Builds and pushes an image up to your Azure Container Registry
- name: Build and push image to ACR
run: |
az acr build --image ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }} --registry ${{ env.AZURE_CONTAINER_REGISTRY }} -g ${{ env.RESOURCE_GROUP }} .
2021-10-28 22:58:17 -04:00
createSecret:
permissions:
contents: read
id-token: write
runs-on: ubuntu-latest
steps:
# Logs in with your Azure credentials
- name: Azure login
uses: azure/[email protected]
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
2022-03-03 22:43:12 +05:30
# Retrieves your Azure Kubernetes Service cluster's kubeconfig file
- name: Get K8s context
uses: azure/[email protected]
2021-10-28 22:58:17 -04:00
with:
2022-03-03 22:43:12 +05:30
resource-group: ${{ env.RESOURCE_GROUP }}
cluster-name: ${{ env.CLUSTER_NAME }}
2021-10-28 22:58:17 -04:00
2022-03-03 22:43:12 +05:30
# Retrieves the credentials for pulling images from your Azure Container Registry
- name: Get ACR credentials
run: |
az acr update -n ${{ env.AZURE_CONTAINER_REGISTRY }} -g ${{ env.RESOURCE_GROUP }} --admin-enabled true
ACR_USERNAME=$(az acr credential show -g ${{ env.RESOURCE_GROUP }} -n ${{ env.AZURE_CONTAINER_REGISTRY }} --query username -o tsv)
ACR_PASSWORD=$(az acr credential show -g ${{ env.RESOURCE_GROUP }} -n ${{ env.AZURE_CONTAINER_REGISTRY }} --query passwords[0].value -o tsv)
echo "::add-mask::${ACR_USERNAME}"
2022-03-03 22:43:12 +05:30
echo "::set-output name=username::${ACR_USERNAME}"
echo "::add-mask::${ACR_PASSWORD}"
2022-03-03 22:43:12 +05:30
echo "::set-output name=password::${ACR_PASSWORD}"
id: get-acr-creds
# Creates a kubernetes secret on your Azure Kubernetes Service cluster that matches up to the credentials from the last step
- name: Create K8s secret for pulling image from ACR
uses: Azure/[email protected]
with:
container-registry-url: ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io
container-registry-username: ${{ steps.get-acr-creds.outputs.username }}
container-registry-password: ${{ steps.get-acr-creds.outputs.password }}
secret-name: ${{ env.IMAGE_PULL_SECRET_NAME }}
deploy:
permissions:
actions: read
contents: read
id-token: write
runs-on: ubuntu-latest
needs: [buildImage, createSecret]
steps:
# Checks out the repository this file is in
- uses: actions/checkout@v3
# Logs in with your Azure credentials
- name: Azure login
uses: azure/[email protected]
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
# Retrieves your Azure Kubernetes Service cluster's kubeconfig file
- name: Get K8s context
uses: azure/[email protected]
with:
resource-group: ${{ env.RESOURCE_GROUP }}
cluster-name: ${{ env.CLUSTER_NAME }}
2022-03-03 22:43:12 +05:30
# Deploys application based on given manifest file
2021-10-28 23:05:42 -04:00
- name: Deploys application
uses: Azure/[email protected]
2021-10-28 22:58:17 -04:00
with:
2022-03-03 22:43:12 +05:30
action: deploy
manifests: ${{ env.DEPLOYMENT_MANIFEST_PATH }}
2021-10-28 22:58:17 -04:00
images: |
2022-03-03 22:43:12 +05:30
${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }}
2021-10-28 22:58:17 -04:00
imagepullsecrets: |
${{ env.IMAGE_PULL_SECRET_NAME }}