Skip to content

Update README.md (#6) #12

Update README.md (#6)

Update README.md (#6) #12

name: Build and Deploy to AKS
on:
push:
branches:
- main
# At a minimum, change the variables denoted with a * in the comment in order to ensure uniqueness.
env:
RESOURCEGROUPNAME: "azuremetrics-dotnet" # Azure Resource group's name *
LOCATION: "East US" # Azure Resource group's location
IMAGENAME: "azuremetricsdotnet" # Image name *
REGISTRYSKU: "Standard" # Registry sku
REGISTRYNAME: "azuremetricsdotnet" # Container registry name *
REGISTRYLOCATION: "East US" # Container registry location
CLUSTERNAME: "azuremetricsdotnet" # Cluster name *
APPINSIGHTSLOCATION: "East US" # Application insights location
CLUSTERLOCATION: "East US" # Cluster location
AGENTCOUNT: "2" # Agent count
AGENTVMSIZE: "Standard_DS2_v2" # Agent VM Size
KUBERNETESVERSION: 1.19.7 # Kubernetes version
OMSLOCATION: "East US" # OMS location
OMSWORKSPACENAME: "azuremetricsdotnet" # Name of the OMS workspace *
HTTPSAPPLICATIONROUTINGENABLED: false
KUBERNETESAPI: "apps/v1"
NAMESPACE: "azuremetricsdotnet" # Set namespace *
jobs:
build:
name: Build and push image to container registry
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Authentication
# Set up the following secret in your repository:
# AZURE_CREDENTIALS
- name: Login to Azure
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Create AKS Cluster
uses: azure/arm-deploy@v1
id: deploy
with:
scope: resourcegroup
subscriptionId: ${{ secrets.AZURE_CREDENTIALS.subscriptionId }}
resourceGroupName: ${{ env.RESOURCEGROUPNAME }}
template: ./ArmTemplates/aks-template.json
parameters: clusterName="${{ env.CLUSTERNAME }}" appInsightsLocation="${{ env.APPINSIGHTSLOCATION }}" clusterLocation="${{ env.CLUSTERLOCATION }}" agentCount="${{ env.AGENTCOUNT }}" agentVMSize="${{ env.AGENTVMSIZE }}" kubernetesVersion="${{ env.KUBERNETESVERSION }}" omsLocation="${{ env.OMSLOCATION }}" omsWorkspaceName="${{ env.OMSWORKSPACENAME }}" httpApplicationRoutingEnabled="${{ env.HTTPSAPPLICATIONROUTINGENABLED }}" dnsPrefix="${{ env.CLUSTERNAME }}"
- name: Create ACR
id: createACR
run: |
az acr create -n ${{ env.REGISTRYNAME }} -g ${{ env.RESOURCEGROUPNAME }} --location "${{env.REGISTRYLOCATION}}" --sku ${{env.REGISTRYSKU}} --admin-enabled
echo "::set-output name=acr_username::`az acr credential show -n ${{ env.REGISTRYNAME }} --query username`"
echo "::set-output name=acr_password::`az acr credential show -n ${{ env.REGISTRYNAME }} --query passwords[0].value`"
echo "::add-mask::`az acr credential show -n ${{ env.REGISTRYNAME }} --query passwords[0].value`"
- name: Build and push image to ACR
id: build-image
run: |
echo "::add-mask::${{ steps.createACR.outputs.acr_password }}"
docker login ${{ env.REGISTRYNAME }}.azurecr.io --username ${{ steps.createACR.outputs.acr_username }} --password ${{ steps.createACR.outputs.acr_password }}
docker build "$GITHUB_WORKSPACE/Application" -f "Application/Dockerfile" -t ${{ env.REGISTRYNAME }}.azurecr.io/${{ env.IMAGENAME }}:${{ github.sha }}
docker push ${{ env.REGISTRYNAME }}.azurecr.io/${{ env.IMAGENAME }}:${{ github.sha }}
deploy:
name: Deploy application to AKS
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# login to azure
- name: Login to Azure
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Get AKS Credentials
id: getContext
run: |
az aks get-credentials --resource-group ${{ env.RESOURCEGROUPNAME }} --name ${{ env.CLUSTERNAME }} --file $GITHUB_WORKSPACE/kubeconfig
echo "KUBECONFIG=$GITHUB_WORKSPACE/kubeconfig" >> $GITHUB_ENV
- name: Create namespace
run: |
namespacePresent=`kubectl get namespace | grep ${{ env.NAMESPACE }} | wc -l`
if [ $namespacePresent -eq 0 ]
then
echo `kubectl create namespace ${{ env.NAMESPACE }}`
fi
- name: Get ACR credentials
id: getACRCred
run: |
echo "::set-output name=acr_username::`az acr credential show -n ${{ env.REGISTRYNAME }} --query username | xargs`"
echo "::set-output name=acr_password::`az acr credential show -n ${{ env.REGISTRYNAME }} --query passwords[0].value | xargs`"
echo "::add-mask::`az acr credential show -n ${{ env.REGISTRYNAME }} --query passwords[0].value | xargs`"
- uses: azure/k8s-create-secret@v1
with:
namespace: ${{ env.NAMESPACE }}
container-registry-url: ${{ env.REGISTRYNAME }}.azurecr.io
container-registry-username: ${{ steps.getACRCred.outputs.acr_username }}
container-registry-password: ${{ steps.getACRCred.outputs.acr_password }}
secret-name: ${{ env.CLUSTERNAME }}dockerauth
- name: Fetch Application insights key
id: GetAppInsightsKey
run: |
echo "::set-output name=AIKey::`az resource show -g ${{ env.RESOURCEGROUPNAME }} -n ${{ env.CLUSTERNAME }} --resource-type "Microsoft.Insights/components" --query "properties.InstrumentationKey" -o tsv`"
echo "::add-mask::`az resource show -g ${{ env.RESOURCEGROUPNAME }} -n ${{ env.CLUSTERNAME }} --resource-type "Microsoft.Insights/components" --query "properties.InstrumentationKey" -o tsv`"
- uses: azure/k8s-bake@v1
id: bakeManifests
with:
renderEngine: "helm"
helmChart: "./Application/charts/sampleapp"
overrideFiles: "./Application/charts/sampleapp/values.yaml"
overrides: |
image.repository:${{ env.REGISTRYNAME }}.azurecr.io/${{ env.IMAGENAME }}
image.tag:${{ github.sha }}
imagePullSecrets:{${{ env.CLUSTERNAME }}dockerauth}
applicationInsights.InstrumentationKey:${{ steps.GetAppInsightsKey.outputs.AIKey }}
apiVersion:${{ env.KUBERNETESAPI }}
extensionApiVersion:${{ env.KUBERNETESAPI }}
helm-version: "latest"
silent: "true"
- uses: azure/k8s-deploy@v1
with:
namespace: ${{ env.NAMESPACE }}
manifests: ${{ steps.bakeManifests.outputs.manifestsBundle }}
images: |
${{ env.REGISTRYNAME }}.azurecr.io/${{ env.IMAGENAME }}:${{ github.sha }}
imagepullsecrets: |
${{ env.CLUSTERNAME }}dockerauth
- name: Cleanup
run: |
az logout
rm -rf $GITHUB_WORKSPACE/kubeconfig