From 130182c156cfe648bb14f816040d28459a0b59eb Mon Sep 17 00:00:00 2001 From: "Shkar T. Noori" Date: Mon, 7 Feb 2022 11:52:10 +0000 Subject: [PATCH] Add CI/CD --- .dockerignore | 15 +++++ .github/CODEOWNERS | 2 + .github/workflows/deploy-base.yaml | 57 +++++++++++++++++++ .github/workflows/deploy-dev.yaml | 54 ++++++++++++++++++ .github/workflows/tests-base.yaml | 32 +++++++++++ .github/workflows/tests-run.yaml | 40 +++++++++++++ Dockerfile | 20 +++++++ kubernetes/base/deployment.yaml | 38 +++++++++++++ kubernetes/base/ingress.yaml | 18 ++++++ kubernetes/base/kustomization.yaml | 12 ++++ kubernetes/base/service.yaml | 13 +++++ kubernetes/development/kustomization.yaml | 14 +++++ .../development/patches/ingress-host.yaml | 9 +++ nginx.conf | 15 +++++ 14 files changed, 339 insertions(+) create mode 100644 .dockerignore create mode 100644 .github/CODEOWNERS create mode 100644 .github/workflows/deploy-base.yaml create mode 100644 .github/workflows/deploy-dev.yaml create mode 100644 .github/workflows/tests-base.yaml create mode 100644 .github/workflows/tests-run.yaml create mode 100644 Dockerfile create mode 100644 kubernetes/base/deployment.yaml create mode 100644 kubernetes/base/ingress.yaml create mode 100644 kubernetes/base/kustomization.yaml create mode 100644 kubernetes/base/service.yaml create mode 100644 kubernetes/development/kustomization.yaml create mode 100644 kubernetes/development/patches/ingress-host.yaml create mode 100644 nginx.conf diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..2a800ff --- /dev/null +++ b/.dockerignore @@ -0,0 +1,15 @@ +.env +.git +.github +.dockerignore +.gitignore +.docusaurus +build +README.md +Dockerfile +kubernetes + +# Artifacts that will be built during image creation. +# This should contain all files created during `npm run build`. +build +node_modules diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..95415fd --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,2 @@ +/.github/ @ditkrg/devops +/kubernetes/ @ditkrg/devops diff --git a/.github/workflows/deploy-base.yaml b/.github/workflows/deploy-base.yaml new file mode 100644 index 0000000..64ea605 --- /dev/null +++ b/.github/workflows/deploy-base.yaml @@ -0,0 +1,57 @@ +name: Deploy + +on: + workflow_call: + inputs: + image: + type: string + required: true + env_url: + type: string + required: true + env_name: + type: string + required: true + + secrets: + KUBECONFIG: + required: true + +env: + NAMESPACE: dit-docs + +jobs: + deploy: + timeout-minutes: 10 + runs-on: [self-hosted, ubuntu-focal] + environment: + url: ${{ inputs.env_url }} + name: ${{ inputs.env_name }} + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Kubectl tool installer + uses: Azure/setup-kubectl@v1 + + - uses: azure/k8s-set-context@v1 + with: + method: kubeconfig + kubeconfig: ${{ secrets.KUBECONFIG }} + + - name: Setup Kustomize + uses: imranismail/setup-kustomize@v1 + with: + kustomize-version: "4.4.1" + + - name: Edit kustomization file + working-directory: kubernetes/base + run: kustomize edit set image IMAGE="${{ inputs.image }}" + + - name: Deploy + working-directory: kubernetes/${{ inputs.env_name }} + run: |- + + kustomize build | kubectl apply -f - + kubectl rollout -n "$NAMESPACE" status deployment/dsm-client-deployment -w --timeout=3m diff --git a/.github/workflows/deploy-dev.yaml b/.github/workflows/deploy-dev.yaml new file mode 100644 index 0000000..b61eea5 --- /dev/null +++ b/.github/workflows/deploy-dev.yaml @@ -0,0 +1,54 @@ +name: Deploy To Development + +on: + push: + branches: + - dev + paths-ignore: + - "README.md" + - ".vscode/**" + +concurrency: + group: deploy-${{ github.ref }} + cancel-in-progress: true + +jobs: + setup: + name: Setup + runs-on: ubuntu-20.04 + env: + IMAGE_REPO: reg.dev.krd/dit-docs/dsm-client + steps: + - name: Extract image name + run: | + REF_NAME=$(echo ${GITHUB_REF##*/}) + echo "IMAGE=$IMAGE_REPO:$REF_NAME" >> $GITHUB_ENV + outputs: + image: ${{ env.IMAGE }} + image-sha: ${{ env.IMAGE }}-${{ github.sha }} + + test: + uses: ditkrg/dit-digital-service-manual/.github/workflows/tests-base.yaml@dev + + build: + uses: ditkrg/common-github-workflows/.github/workflows/build-push-image.yaml@main + needs: [setup, test] + with: + tags: |- + ${{ needs.setup.outputs.image }} + ${{ needs.setup.outputs.image-sha }} + cache-from: type=registry,ref=${{ needs.setup.outputs.image }} + secrets: + username: ${{ secrets.HARBOR_USER }} + password: ${{ secrets.HARBOR_TOKEN }} + + deploy: + uses: ditkrg/dit-digital-service-manual/.github/workflows/deploy-base.yaml@dev + needs: [setup, build] + with: + image: ${{ needs.setup.outputs.image-sha }} + env_url: https://service-manual.docs.dev.krd + env_name: development + + secrets: + KUBECONFIG: ${{ secrets.KUBECONFIG }} diff --git a/.github/workflows/tests-base.yaml b/.github/workflows/tests-base.yaml new file mode 100644 index 0000000..45a853d --- /dev/null +++ b/.github/workflows/tests-base.yaml @@ -0,0 +1,32 @@ +name: Run Tests + +on: + workflow_call: + +jobs: + run-tests: + name: Run Tests + timeout-minutes: 10 + runs-on: ubuntu-20.04 + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Kubectl tool installer + uses: Azure/setup-kubectl@v1 + + - name: Setup Kustomize + uses: imranismail/setup-kustomize@v1 + with: + kustomize-version: "4.4.1" + + - name: Build k8s manifests + working-directory: kubernetes + run: | + envs=( "development" ) + + for i in "${envs[@]}" + do + kustomize build "$i" + done diff --git a/.github/workflows/tests-run.yaml b/.github/workflows/tests-run.yaml new file mode 100644 index 0000000..a5472aa --- /dev/null +++ b/.github/workflows/tests-run.yaml @@ -0,0 +1,40 @@ +name: Run Tests + +on: + push: + branches-ignore: + - dev + - staging + - main + paths-ignore: + - "**.md" + - ".vscode/**" + pull_request: + +jobs: + run-tests: + name: Run Tests + timeout-minutes: 10 + runs-on: ubuntu-20.04 + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Kubectl tool installer + uses: Azure/setup-kubectl@v1 + + - name: Setup Kustomize + uses: imranismail/setup-kustomize@v1 + with: + kustomize-version: "4.4.1" + + - name: Build k8s manifests + working-directory: kubernetes + run: | + envs=( "development" ) + + for i in "${envs[@]}" + do + kustomize build "$i" + done diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..66f2c30 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM reg.dev.krd/hub.docker/library/node:16 as build-stage + +WORKDIR /app + +COPY package.json package-lock.json ./ + +RUN npm ci + +COPY src src +COPY docs docs +COPY static static +COPY *.js ./ + +RUN npm run build + +FROM reg.dev.krd/hub.docker/library/nginx:1.20-alpine AS production + +COPY --from=build-stage /app/build /usr/share/nginx/html +COPY nginx.conf /etc/nginx/conf.d/default.conf +EXPOSE 80 diff --git a/kubernetes/base/deployment.yaml b/kubernetes/base/deployment.yaml new file mode 100644 index 0000000..291176d --- /dev/null +++ b/kubernetes/base/deployment.yaml @@ -0,0 +1,38 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: deployment + labels: + app: dsm-client + +spec: + selector: + matchLabels: + app: dsm-client + + template: + metadata: + labels: + app: dsm-client + + spec: + containers: + - name: dsm-client + image: IMAGE + resources: + limits: + memory: 32Mi + cpu: 500m + requests: + memory: 32Mi + cpu: 5m + ports: + - containerPort: 80 + + livenessProbe: + initialDelaySeconds: 5 + periodSeconds: 15 + httpGet: + path: /health + port: 80 diff --git a/kubernetes/base/ingress.yaml b/kubernetes/base/ingress.yaml new file mode 100644 index 0000000..8152274 --- /dev/null +++ b/kubernetes/base/ingress.yaml @@ -0,0 +1,18 @@ +--- +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ingress + annotations: + cert-manager.io/cluster-issuer: letsencrypt-cluster-issuer +spec: + rules: + - http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: service + port: + number: 80 diff --git a/kubernetes/base/kustomization.yaml b/kubernetes/base/kustomization.yaml new file mode 100644 index 0000000..a34b0f4 --- /dev/null +++ b/kubernetes/base/kustomization.yaml @@ -0,0 +1,12 @@ +--- +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +namespace: dit-docs + +commonLabels: + app: dsm-client + +resources: + - ingress.yaml + - service.yaml + - deployment.yaml diff --git a/kubernetes/base/service.yaml b/kubernetes/base/service.yaml new file mode 100644 index 0000000..54983f3 --- /dev/null +++ b/kubernetes/base/service.yaml @@ -0,0 +1,13 @@ +--- +apiVersion: v1 +kind: Service +metadata: + name: service + labels: + app: dsm-client +spec: + selector: + app: dsm-client + ports: + - port: 80 + type: ClusterIP diff --git a/kubernetes/development/kustomization.yaml b/kubernetes/development/kustomization.yaml new file mode 100644 index 0000000..3239f15 --- /dev/null +++ b/kubernetes/development/kustomization.yaml @@ -0,0 +1,14 @@ +--- +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization + +namespace: dit-docs +namePrefix: dsm-client- + +resources: + - ../base + +patches: + - path: patches/ingress-host.yaml + target: + kind: Ingress diff --git a/kubernetes/development/patches/ingress-host.yaml b/kubernetes/development/patches/ingress-host.yaml new file mode 100644 index 0000000..3f572b8 --- /dev/null +++ b/kubernetes/development/patches/ingress-host.yaml @@ -0,0 +1,9 @@ +- op: add + path: /spec/rules/0/host + value: service-manual.docs.dev.krd +- op: add + path: /spec/tls + value: + - hosts: + - service-manual.docs.dev.krd + secretName: ingress-cert-dit-docs-dsm-client diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..0b403f7 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,15 @@ +server { + listen 80; + server_name _; + + location / { + root /usr/share/nginx/html; + try_files $uri $uri/ /index.html; + } + + location /health { + access_log off; + default_type text/plain; + return 200 "Healthy\n"; + } +}