From 3ee9d495c99846f0a02405f4dbaa50858957afd1 Mon Sep 17 00:00:00 2001 From: Yifan Zhang Date: Tue, 6 Dec 2022 21:04:32 +0300 Subject: [PATCH] workflow: deploy release to aks deploy to AKS when a release is published --- .../workflows/azure-kubernetes-service.yml | 114 ++++++++++++++++++ .../pytest-redis-postgres-workflow.yml | 6 +- .gitignore | 1 + manifests/apihub-result.yaml | 48 ++++++++ manifests/apihub-server.yaml | 65 ++++++++++ 5 files changed, 231 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/azure-kubernetes-service.yml create mode 100644 manifests/apihub-result.yaml create mode 100644 manifests/apihub-server.yaml diff --git a/.github/workflows/azure-kubernetes-service.yml b/.github/workflows/azure-kubernetes-service.yml new file mode 100644 index 0000000..bc6141e --- /dev/null +++ b/.github/workflows/azure-kubernetes-service.yml @@ -0,0 +1,114 @@ +# 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) +# The ACR should be attached to the AKS cluster +# 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://learn.microsoft.com/en-us/azure/aks/cluster-container-registry-integration?tabs=azure-cli#configure-acr-integration-for-existing-aks-clusters +# - https://github.com/Azure/aks-create-action +# +# 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): +# - AZURE_CLIENT_ID +# - AZURE_TENANT_ID +# - AZURE_SUBSCRIPTION_ID +# +# 2. Set the following environment variables (or replace the values below): +# - AZURE_CONTAINER_REGISTRY (name of your container registry / ACR) +# - RESOURCE_GROUP (where your cluster is deployed) +# - CLUSTER_NAME (name of your AKS cluster) +# - 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) +# - DEPLOYMENT_MANIFEST_PATH (path to the manifest yaml for your deployment) +# +# 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 +# For more options with the actions used below please refer to https://github.com/Azure/login + +name: Build and deploy an app to AKS + +on: + release: + types: [published] + workflow_dispatch: + +env: + AZURE_CONTAINER_REGISTRY: "meganews" + CONTAINER_NAME: "apihub" + RESOURCE_GROUP: "meganews" + CLUSTER_NAME: "apihub" + NAMESPACE: "apihub" + DEPLOYMENT_MANIFEST_PATH: | + manifests/apihub-server.yaml + manifests/apihub-result.yaml + +jobs: + buildImage: + runs-on: ubuntu-latest + environment: production + permissions: + contents: read + id-token: write + steps: + # Checks out the repository this file is in + - uses: actions/checkout@v3 + + # Logs in with your Azure credentials + - name: Azure login + uses: azure/login@v1.4.6 + with: + client-id: ${{ secrets.AZURE_CLIENT_ID }} + tenant-id: ${{ secrets.AZURE_TENANT_ID }} + subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} + + # # Connect to Azure Container registry (ACR) + # - uses: azure/docker-login@v1 + # with: + # login-server: ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io + # username: ${{ secrets.REGISTRY_USERNAME }} + # password: ${{ secrets.REGISTRY_PASSWORD }} + + # 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.ref_name }} --registry ${{ env.AZURE_CONTAINER_REGISTRY }} -g ${{ env.RESOURCE_GROUP }} . + + deploy: + runs-on: ubuntu-latest + environment: production + permissions: + actions: read + contents: read + id-token: write + needs: [buildImage] + steps: + # Checks out the repository this file is in + - uses: actions/checkout@v3 + + # Logs in with your Azure credentials + - name: Azure login + uses: azure/login@v1.4.6 + 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/aks-set-context@v3 + with: + resource-group: ${{ env.RESOURCE_GROUP }} + cluster-name: ${{ env.CLUSTER_NAME }} + + # Deploys application based on given manifest file + - name: Deploys application + uses: Azure/k8s-deploy@v4 + with: + action: deploy + manifests: ${{ env.DEPLOYMENT_MANIFEST_PATH }} + images: | + ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.ref_name }} + namespace: ${{ env.NAMESPACE }} + diff --git a/.github/workflows/pytest-redis-postgres-workflow.yml b/.github/workflows/pytest-redis-postgres-workflow.yml index e8eec5b..748e785 100644 --- a/.github/workflows/pytest-redis-postgres-workflow.yml +++ b/.github/workflows/pytest-redis-postgres-workflow.yml @@ -1,5 +1,5 @@ name: poetry pytest workflow -on: push +on: [pull_request] jobs: # Label of the container job @@ -44,10 +44,10 @@ jobs: steps: # Downloads a copy of the code in your repository before running CI tests - name: Check out repository code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} diff --git a/.gitignore b/.gitignore index 37cdcf9..03e00e0 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ env dist **.apihub **/.DS_Store +.secrets diff --git a/manifests/apihub-result.yaml b/manifests/apihub-result.yaml new file mode 100644 index 0000000..832cddd --- /dev/null +++ b/manifests/apihub-result.yaml @@ -0,0 +1,48 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: apihub-result + name: apihub-result +spec: + progressDeadlineSeconds: 600 + replicas: 1 + revisionHistoryLimit: 10 + selector: + matchLabels: + app: apihub-result + template: + metadata: + labels: + app: apihub-result + spec: + containers: + - command: + - poetry + - run + - apihub_result + envFrom: + - configMapRef: + name: pipeline-config + - configMapRef: + name: result-config + image: meganews.azurecr.io/apihub:v0.1.2a6 + imagePullPolicy: IfNotPresent + name: result + ports: + - containerPort: 8000 + name: http-metrics + protocol: TCP + resources: + limits: + memory: 1Gi + requests: + cpu: 100m + memory: 256Mi + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + dnsPolicy: ClusterFirst + restartPolicy: Always + schedulerName: default-scheduler + securityContext: {} + terminationGracePeriodSeconds: 30 diff --git a/manifests/apihub-server.yaml b/manifests/apihub-server.yaml new file mode 100644 index 0000000..696f9bb --- /dev/null +++ b/manifests/apihub-server.yaml @@ -0,0 +1,65 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: apihub-server + name: apihub-server + namespace: apihub +spec: + progressDeadlineSeconds: 600 + replicas: 1 + revisionHistoryLimit: 10 + selector: + matchLabels: + app: apihub-server + strategy: + rollingUpdate: + maxSurge: 25% + maxUnavailable: 25% + type: RollingUpdate + template: + metadata: + creationTimestamp: null + labels: + app: apihub-server + spec: + containers: + - command: + - poetry + - run + - apihub_server + envFrom: + - configMapRef: + name: pipeline-config + - configMapRef: + name: server-config + image: meganews.azurecr.io/apihub:v0.0.0a0 + imagePullPolicy: IfNotPresent + livenessProbe: + failureThreshold: 3 + httpGet: + path: / + port: 5000 + scheme: HTTP + initialDelaySeconds: 30 + periodSeconds: 30 + successThreshold: 1 + timeoutSeconds: 30 + name: server + ports: + - containerPort: 5000 + name: http-metrics + protocol: TCP + resources: + limits: + memory: 1Gi + requests: + cpu: 100m + memory: 256Mi + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + dnsPolicy: ClusterFirst + restartPolicy: Always + schedulerName: default-scheduler + securityContext: {} + terminationGracePeriodSeconds: 30