From d9e945ee92b5a4e0dfba702677bb4591c5b84c8a Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Tue, 7 Dec 2021 11:25:40 +0400 Subject: [PATCH] Added gitlab-ci. Signed-off-by: Pavel Kirilin --- .dockerignore | 90 +++++++++++++++++++++++++++ .github/workflows/publish.yml | 34 ---------- .gitlab-ci.yml | 50 +++++++++++++++ deploy/Dockerfile | 11 ++++ deploy/helm/.helmignore | 23 +++++++ deploy/helm/Chart.yaml | 24 +++++++ deploy/helm/files/dockerauth.json | 8 +++ deploy/helm/templates/NOTES.txt | 22 +++++++ deploy/helm/templates/_helpers.tpl | 51 +++++++++++++++ deploy/helm/templates/deployment.yaml | 56 +++++++++++++++++ deploy/helm/templates/hpa.yaml | 28 +++++++++ deploy/helm/templates/ingress.yaml | 60 ++++++++++++++++++ deploy/helm/templates/pull_secret.yml | 9 +++ deploy/helm/templates/service.yaml | 15 +++++ deploy/helm/values.yaml | 41 ++++++++++++ layouts/default.vue | 7 +-- nuxt.config.js | 21 ++++++- package.json | 1 + pages/_slug.vue | 4 ++ pages/index.vue | 4 +- tsconfig.json | 1 - yarn.lock | 71 ++++++++++++++++++++- 22 files changed, 582 insertions(+), 49 deletions(-) create mode 100644 .dockerignore delete mode 100644 .github/workflows/publish.yml create mode 100644 .gitlab-ci.yml create mode 100644 deploy/Dockerfile create mode 100644 deploy/helm/.helmignore create mode 100644 deploy/helm/Chart.yaml create mode 100644 deploy/helm/files/dockerauth.json create mode 100644 deploy/helm/templates/NOTES.txt create mode 100644 deploy/helm/templates/_helpers.tpl create mode 100644 deploy/helm/templates/deployment.yaml create mode 100644 deploy/helm/templates/hpa.yaml create mode 100644 deploy/helm/templates/ingress.yaml create mode 100644 deploy/helm/templates/pull_secret.yml create mode 100644 deploy/helm/templates/service.yaml create mode 100644 deploy/helm/values.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e8f682b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,90 @@ +# Created by .ignore support plugin (hsz.mobi) +### Node template +# Logs +/logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# next.js build output +.next + +# nuxt.js build output +.nuxt + +# Nuxt generate +dist + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless + +# IDE / Editor +.idea + +# Service worker +sw.* + +# macOS +.DS_Store + +# Vim swap files +*.swp diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml deleted file mode 100644 index 5bfedfb..0000000 --- a/.github/workflows/publish.yml +++ /dev/null @@ -1,34 +0,0 @@ -name: Publish docs -on: - push: - branches: - - master - -jobs: - build: - name: Deploy docs - runs-on: ubuntu-latest - steps: - - name: Checkout main - uses: actions/checkout@v2 - - - name: install dependencies. - uses: borales/actions-yarn@v2.3.0 - with: - cmd: install # will run `yarn install` command - - - name: Build docs - uses: borales/actions-yarn@v2.3.0 - with: - cmd: generate # will run `yarn generate` command - - - name: Push to docs repo - uses: cpina/github-action-push-to-another-repository@main - env: - API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }} - with: - source-directory: 'dist' - destination-github-username: 's3rius' - destination-repository-name: 's3rius.github.io' - user-email: win10@list.ru - target-branch: master diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..81d41ca --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,50 @@ +variables: + APP_VERSION: $CI_COMMIT_REF_SLUG + DOCKER_AUTH_CONFIG: '{"auths":{"$DOCKER_REGISTRY":{"username":"$DOCKER_USER","password":"$DOCKER_PASSWORD"}}}' + +build: + stage: build + image: + name: gcr.io/kaniko-project/executor:v1.6.0-debug + entrypoint: [""] + tags: + - kube + only: + - master + script: + - mkdir -p /kaniko/.docker + - echo "$DOCKER_AUTH_CONFIG" > /kaniko/.docker/config.json + - /kaniko/executor + --context . + --dockerfile deploy/Dockerfile + --destination "$DOCKER_REGISTRY/s3rius/blog:latest" + --cache=true + --force + +deploy: + stage: deploy + image: + name: alpine/helm:3.7.1 + entrypoint: ["/bin/sh", "-c"] + tags: + - kube + only: + - master + environment: + name: blog-production + kubernetes: + namespace: "$NAMESPACE" + action: start + url: "https://s3rius.blog/" + script: + - helm + upgrade + "s3rius-blog" + ./deploy/helm + --install + --wait + --create-namespace + --atomic + --timeout 2m + --namespace "$NAMESPACE" + -f "$HELM_CONFIG" diff --git a/deploy/Dockerfile b/deploy/Dockerfile new file mode 100644 index 0000000..07e1dfc --- /dev/null +++ b/deploy/Dockerfile @@ -0,0 +1,11 @@ +FROM node:lts-alpine3.14 as builder + +COPY . /app +WORKDIR /app + +RUN yarn install +RUN yarn generate + +FROM nginx:1.20.2-alpine + +COPY --from=builder /app/dist/ /usr/share/nginx/html/ diff --git a/deploy/helm/.helmignore b/deploy/helm/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/deploy/helm/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/deploy/helm/Chart.yaml b/deploy/helm/Chart.yaml new file mode 100644 index 0000000..9ea0f6a --- /dev/null +++ b/deploy/helm/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: s3rius-blog +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" diff --git a/deploy/helm/files/dockerauth.json b/deploy/helm/files/dockerauth.json new file mode 100644 index 0000000..cd2ce74 --- /dev/null +++ b/deploy/helm/files/dockerauth.json @@ -0,0 +1,8 @@ +{ + "auths": { + {{ .Values.image.pullSecret.registry | quote}}: { + "username": {{.Values.image.pullSecret.username | quote }}, + "password": {{.Values.image.pullSecret.password | quote }} + } + } +} diff --git a/deploy/helm/templates/NOTES.txt b/deploy/helm/templates/NOTES.txt new file mode 100644 index 0000000..f3cf193 --- /dev/null +++ b/deploy/helm/templates/NOTES.txt @@ -0,0 +1,22 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "s3rius-blog.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "s3rius-blog.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "s3rius-blog.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "s3rius-blog.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }} diff --git a/deploy/helm/templates/_helpers.tpl b/deploy/helm/templates/_helpers.tpl new file mode 100644 index 0000000..14d7ee8 --- /dev/null +++ b/deploy/helm/templates/_helpers.tpl @@ -0,0 +1,51 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "s3rius-blog.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "s3rius-blog.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "s3rius-blog.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "s3rius-blog.labels" -}} +helm.sh/chart: {{ include "s3rius-blog.chart" . }} +{{ include "s3rius-blog.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "s3rius-blog.selectorLabels" -}} +app.kubernetes.io/name: {{ include "s3rius-blog.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} diff --git a/deploy/helm/templates/deployment.yaml b/deploy/helm/templates/deployment.yaml new file mode 100644 index 0000000..e351012 --- /dev/null +++ b/deploy/helm/templates/deployment.yaml @@ -0,0 +1,56 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "s3rius-blog.fullname" . }} + labels: + {{- include "s3rius-blog.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "s3rius-blog.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "s3rius-blog.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.image.pullSecret }} + imagePullSecrets: + - name: {{ .secretName | quote }} + {{- end }} + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 80 + protocol: TCP + livenessProbe: + httpGet: + path: / + port: http + readinessProbe: + httpGet: + path: / + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/deploy/helm/templates/hpa.yaml b/deploy/helm/templates/hpa.yaml new file mode 100644 index 0000000..d1d6062 --- /dev/null +++ b/deploy/helm/templates/hpa.yaml @@ -0,0 +1,28 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "s3rius-blog.fullname" . }} + labels: + {{- include "s3rius-blog.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "s3rius-blog.fullname" . }} + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} + metrics: + {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }} diff --git a/deploy/helm/templates/ingress.yaml b/deploy/helm/templates/ingress.yaml new file mode 100644 index 0000000..f960c69 --- /dev/null +++ b/deploy/helm/templates/ingress.yaml @@ -0,0 +1,60 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "s3rius-blog.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "s3rius-blog.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/deploy/helm/templates/pull_secret.yml b/deploy/helm/templates/pull_secret.yml new file mode 100644 index 0000000..1f3a321 --- /dev/null +++ b/deploy/helm/templates/pull_secret.yml @@ -0,0 +1,9 @@ +{{- if .Values.image.pullSecret }} +apiVersion: v1 +kind: Secret +metadata: + name: {{ .Values.image.pullSecret.secretName }} +data: + .dockerconfigjson: {{tpl (.Files.Get "files/dockerauth.json") . | b64enc | quote}} +type: kubernetes.io/dockerconfigjson +{{- end }} diff --git a/deploy/helm/templates/service.yaml b/deploy/helm/templates/service.yaml new file mode 100644 index 0000000..b604321 --- /dev/null +++ b/deploy/helm/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "s3rius-blog.fullname" . }} + labels: + {{- include "s3rius-blog.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "s3rius-blog.selectorLabels" . | nindent 4 }} diff --git a/deploy/helm/values.yaml b/deploy/helm/values.yaml new file mode 100644 index 0000000..9b1c0b6 --- /dev/null +++ b/deploy/helm/values.yaml @@ -0,0 +1,41 @@ +replicaCount: 1 + +image: + repository: $DOCKER_REGISTRY/s3rius/blog + pullPolicy: Always + tag: "latest" + pullSecret: {} + +podAnnotations: {} + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: true + className: "" + annotations: {} + hosts: + - host: "s3rius.blog" + paths: + - path: / + pathType: Prefix + tls: + - hosts: + - "s3rius.blog" + +resources: {} + # cpu: 100m + # memory: 100Mi + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} +tolerations: [] +affinity: {} diff --git a/layouts/default.vue b/layouts/default.vue index 3ae225f..0b2f719 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -1,5 +1,5 @@