From 1d967328be8737469c4de142655b350a0ccc6998 Mon Sep 17 00:00:00 2001 From: itzaname Date: Fri, 17 Jun 2022 23:26:52 -0400 Subject: [PATCH] Add CI files --- .drone.yaml | 55 +++++++++++++++++++++++++ Dockerfile | 9 +++++ bot.js | 2 +- chart/.helmignore | 23 +++++++++++ chart/Chart.yaml | 24 +++++++++++ chart/templates/_helpers.tpl | 62 ++++++++++++++++++++++++++++ chart/templates/deployment.yaml | 72 +++++++++++++++++++++++++++++++++ chart/templates/hpa.yaml | 28 +++++++++++++ chart/templates/secret.yaml | 9 +++++ chart/values.yaml | 56 +++++++++++++++++++++++++ commands/take.js | 2 +- deploy-commands.js | 2 +- 12 files changed, 341 insertions(+), 3 deletions(-) create mode 100644 .drone.yaml create mode 100644 Dockerfile create mode 100644 chart/.helmignore create mode 100644 chart/Chart.yaml create mode 100644 chart/templates/_helpers.tpl create mode 100644 chart/templates/deployment.yaml create mode 100644 chart/templates/hpa.yaml create mode 100644 chart/templates/secret.yaml create mode 100644 chart/values.yaml diff --git a/.drone.yaml b/.drone.yaml new file mode 100644 index 0000000..832a9ea --- /dev/null +++ b/.drone.yaml @@ -0,0 +1,55 @@ +--- +kind: pipeline +type: docker + +platform: + os: linux + arch: amd64 + +steps: +- name: build + image: gcr.io/kaniko-project/executor:latest + entrypoint: [ /kaniko/executor ] + command: [ "--context=/drone/src", "--dockerfile=Dockerfile", "--destination=image", "--no-push", "--tarPath=maptest-bot.tar" ] + +- name: scan-image + image: aquasec/trivy:latest + commands: + - trivy image --exit-code 1 --severity CRITICAL --input maptest-bot.tar + +- name: push-image + image: alpine:3 + commands: + - wget -qO- https://github.com/google/go-containerregistry/releases/download/v0.9.0/go-containerregistry_Linux_x86_64.tar.gz | tar xvz -C /usr/bin/ + - crane auth login -u $REGISTRY_USER -p $REGISTRY_PASS $REGISTRY_URL + - crane push maptest-bot.tar $REGISTRY_URL/strafesnet/maptest-bot:latest + - crane push maptest-bot.tar $REGISTRY_URL/strafesnet/maptest-bot:master-$DRONE_BUILD_NUMBER + environment: + REGISTRY_URL: + from_secret: REGISTRY_URL + REGISTRY_USER: + from_secret: REGISTRY_USER + REGISTRY_PASS: + from_secret: REGISTRY_PASS + when: + ref: + - refs/heads/master + +- name: deploy + image: alpine/helm:latest + commands: + - mkdir ~/.kube && echo "$KUBE_CONF_DEV" > ~/.kube/config && chmod 600 ~/.kube/config + - helm install --namespace=strafesnet-dev maptest-bot --set config="$(echo $APP_CONFIG | base64 -w0)" --set image.tag=master-$DRONE_BUILD_NUMBER chart + environment: + APP_CONFIG: + from_secret: APP_CONFIG + KUBE_CONF_DEV: + from_secret: KUBE_CONF_DEV + when: + ref: + - refs/heads/master + +trigger: + ref: + - refs/heads/master + - refs/pull/*/head \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..65075b1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM node:18-alpine + +COPY ./ /app + +WORKDIR /app + +RUN npm install + +CMD [ "node","bot.js" ] \ No newline at end of file diff --git a/bot.js b/bot.js index ee32b8e..84d2974 100644 --- a/bot.js +++ b/bot.js @@ -1,6 +1,6 @@ const fs = require('node:fs'); const { Client, Collection, Intents } = require("discord.js"); -const {token} = require("./config.json"); +const {token} = require("./config/config.json"); const client = new Client({intents: [Intents.FLAGS.GUILDS]}); diff --git a/chart/.helmignore b/chart/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/chart/.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/chart/Chart.yaml b/chart/Chart.yaml new file mode 100644 index 0000000..4f430fd --- /dev/null +++ b/chart/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: maptest-bot +description: Map test bot helm chart + +# 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/chart/templates/_helpers.tpl b/chart/templates/_helpers.tpl new file mode 100644 index 0000000..dc12e56 --- /dev/null +++ b/chart/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "maptest-bot.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 "maptest-bot.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 "maptest-bot.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "maptest-bot.labels" -}} +helm.sh/chart: {{ include "maptest-bot.chart" . }} +{{ include "maptest-bot.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "maptest-bot.selectorLabels" -}} +app.kubernetes.io/name: {{ include "maptest-bot.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "maptest-bot.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "maptest-bot.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/chart/templates/deployment.yaml b/chart/templates/deployment.yaml new file mode 100644 index 0000000..ba8dff3 --- /dev/null +++ b/chart/templates/deployment.yaml @@ -0,0 +1,72 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "maptest-bot.fullname" . }} + labels: + {{- include "maptest-bot.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "maptest-bot.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "maptest-bot.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + resources: + {{- toYaml .Values.resources | nindent 12 }} + command: ["node"] + args: ["bot.js"] + volumeMounts: + - name: config + mountPath: "/app/config" + readOnly: true + initContainers: + - name: {{ .Chart.Name }}-init + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + resources: + {{- toYaml .Values.resources | nindent 12 }} + command: ["node"] + args: ["deploy-commands.js"] + volumeMounts: + - name: config + mountPath: "/app/config" + readOnly: true + volumes: + - name: config + secret: + secretName: {{ include "maptest-bot.fullname" . }}-config + {{- 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/chart/templates/hpa.yaml b/chart/templates/hpa.yaml new file mode 100644 index 0000000..67864d7 --- /dev/null +++ b/chart/templates/hpa.yaml @@ -0,0 +1,28 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "maptest-bot.fullname" . }} + labels: + {{- include "maptest-bot.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "maptest-bot.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/chart/templates/secret.yaml b/chart/templates/secret.yaml new file mode 100644 index 0000000..43525d0 --- /dev/null +++ b/chart/templates/secret.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +data: + config.json: {{ .Values.config | quote }} +kind: Secret +metadata: + name: {{ include "maptest-bot.fullname" . }}-config + labels: + {{- include "maptest-bot.labels" . | nindent 4 }} +type: Opaque \ No newline at end of file diff --git a/chart/values.yaml b/chart/values.yaml new file mode 100644 index 0000000..2cb0e0e --- /dev/null +++ b/chart/values.yaml @@ -0,0 +1,56 @@ +# Default values for maptest-bot. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 +image: + repository: registry.itzana.me/strafesnet/maptest-bot + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "latest" + +imagePullSecrets: + - name: registry-itzaname +nameOverride: "" +fullnameOverride: "" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} + +config: "" + diff --git a/commands/take.js b/commands/take.js index c33b565..7bfaa99 100644 --- a/commands/take.js +++ b/commands/take.js @@ -1,6 +1,6 @@ const { SlashCommandBuilder } = require('@discordjs/builders'); const noblox = require("noblox.js"); -const { bhopCookie, surfCookie, deathrunCookie } = require("../config.json"); +const { bhopCookie, surfCookie, deathrunCookie } = require("../config/config.json"); async function execute(interaction) { const game = interaction.options.getString("game"); diff --git a/deploy-commands.js b/deploy-commands.js index 6c1d17b..8369924 100644 --- a/deploy-commands.js +++ b/deploy-commands.js @@ -1,6 +1,6 @@ const { REST } = require('@discordjs/rest'); const { Routes } = require('discord-api-types/v9'); -const { clientId, token } = require('./config.json'); +const { clientId, token } = require('./config/config.json'); const fs = require('node:fs'); const commands = [];