From 8e46fe38d3dae941ff6feff0c7bc330c5ca0c491 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Sat, 29 Oct 2022 09:06:41 +0200 Subject: [PATCH] feat(ci/cd): add deploy pipelines --- .drone.yml | 88 ++++++++++++++++++++++++++- build/docker/dcbot/prod/Dockerfile | 22 +++++++ build/docker/dcbot/prod/manifest.tmpl | 13 ++++ k8s/overlays/prod/bot.yml | 33 ++++++++++ k8s/overlays/prod/jobs.yml | 21 +++++++ k8s/overlays/prod/kustomization.yaml | 9 +++ 6 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 build/docker/dcbot/prod/Dockerfile create mode 100644 build/docker/dcbot/prod/manifest.tmpl create mode 100644 k8s/overlays/prod/bot.yml create mode 100644 k8s/overlays/prod/jobs.yml create mode 100644 k8s/overlays/prod/kustomization.yaml diff --git a/.drone.yml b/.drone.yml index 8765f8f..1869459 100644 --- a/.drone.yml +++ b/.drone.yml @@ -64,8 +64,94 @@ trigger: - pull_request branch: - master + +--- +kind: pipeline +type: docker +name: linux-amd64 + +platform: + os: linux + arch: amd64 + +steps: + - name: publish + image: plugins/docker + settings: + username: + from_secret: docker_username + password: + from_secret: docker_password + registry: gitea.dwysokinski.me + repo: gitea.dwysokinski.me/twhelp-packages/dcbot + auto_tag: true + auto_tag_suffix: linux-amd64 + dockerfile: ./build/docker/dcbot/prod/Dockerfile + build_args_from_env: [DRONE_TAG] + +trigger: + event: + - tag + +--- +kind: pipeline +type: docker +name: manifest + +steps: + - name: manifest + image: plugins/manifest + settings: + auto_tag: "true" + ignore_missing: "true" + spec: ./build/docker/dcbot/prod/manifest.tmpl + username: + from_secret: docker_username + password: + from_secret: docker_password + - name: manifest-latest + image: plugins/manifest + settings: + tags: latest + ignore_missing: "true" + spec: ./build/docker/dcbot/prod/manifest.tmpl + username: + from_secret: docker_username + password: + from_secret: docker_password + +trigger: + event: + - tag + +depends_on: + - linux-amd64 + +--- +kind: pipeline +type: docker +name: deploy + +steps: + - name: deploy-k8s + image: alpine/k8s:1.25.0 + environment: + KUBECONFIG: + from_secret: kubeconfig + commands: + - "mkdir ~/.kube && echo \"$KUBECONFIG\" > ~/.kube/twhelp" + - "cd ./k8s/overlays/prod && kustomize edit set image twhelp=gitea.dwysokinski.me/twhelp-packages/dcbot:${DRONE_TAG##v} && cd ../../.." + - "kubectl --kubeconfig ~/.kube/twhelp -n twhelp delete jobs.batch twhelp-dcbot-migrations-job || true" + - kustomize build ./k8s/overlays/prod | kubectl --kubeconfig ~/.kube/twhelp apply -n twhelp -f - + +trigger: + event: + - tag + +depends_on: + - manifest --- kind: signature -hmac: d3d244bdb305befafa4382a48a5ec1c68e79371706383a9a8d455e28ff3996c0 +hmac: 5f322bb5ee59be0e758fc2b28812a50589851b3c0502995c89a1d7bf8adcb7bf ... diff --git a/build/docker/dcbot/prod/Dockerfile b/build/docker/dcbot/prod/Dockerfile new file mode 100644 index 0000000..add05ee --- /dev/null +++ b/build/docker/dcbot/prod/Dockerfile @@ -0,0 +1,22 @@ +FROM golang:1.19.2-alpine3.16 as builder + +WORKDIR /dcbot + +COPY go.mod go.sum ./ +RUN go mod download + +RUN apk --no-cache add make + +COPY . . +RUN make generate +ARG DRONE_TAG="development" +RUN CGO_ENABLED=0 go build -ldflags "-X main.version=${DRONE_TAG##v}" -trimpath -o dcbot ./cmd/dcbot/main.go + +######## Start a new stage from scratch ####### +FROM alpine:3.16 + +RUN apk --no-cache add ca-certificates tzdata + +COPY --from=builder /dcbot/dcbot /usr/bin/ + +ENTRYPOINT ["dcbot"] \ No newline at end of file diff --git a/build/docker/dcbot/prod/manifest.tmpl b/build/docker/dcbot/prod/manifest.tmpl new file mode 100644 index 0000000..d7b8cd0 --- /dev/null +++ b/build/docker/dcbot/prod/manifest.tmpl @@ -0,0 +1,13 @@ +image: gitea.dwysokinski.me/twhelp-packages/dcbot:{{#if build.tag}}{{trimPrefix "v" build.tag}}{{else}}latest{{/if}} +{{#if build.tags}} +tags: +{{#each build.tags}} + - {{this}} +{{/each}} +{{/if}} +manifests: + - + image: gitea.dwysokinski.me/twhelp-packages/dcbot:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-amd64 + platform: + architecture: amd64 + os: linux \ No newline at end of file diff --git a/k8s/overlays/prod/bot.yml b/k8s/overlays/prod/bot.yml new file mode 100644 index 0000000..b51cd49 --- /dev/null +++ b/k8s/overlays/prod/bot.yml @@ -0,0 +1,33 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: twhelp-dcbot-deployment +spec: + template: + spec: + containers: + - name: twhelp-dcbot + image: dcbot + env: + - name: APP_MODE + value: production + - name: DB_DSN + valueFrom: + secretKeyRef: + name: twhelp-dcbot-secret + key: db-dsn + - name: DB_MAX_OPEN_CONNECTIONS + value: "15" + - name: DB_MAX_IDLE_CONNECTIONS + value: "8" + - name: BOT_TOKEN + valueFrom: + secretKeyRef: + name: twhelp-dcbot-secret + key: token + - name: TWHELP_URL + value: "https://tribalwarshelp.com" + - name: BOT_MAX_GROUPS_PER_SERVER + value: "5" + - name: BOT_MAX_MONITORS_PER_GROUP + value: "10" diff --git a/k8s/overlays/prod/jobs.yml b/k8s/overlays/prod/jobs.yml new file mode 100644 index 0000000..b4e953d --- /dev/null +++ b/k8s/overlays/prod/jobs.yml @@ -0,0 +1,21 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: twhelp-dcbot-migrations-job +spec: + template: + spec: + containers: + - name: twhelp-dcbot-migrations + image: dcbot + args: ["db", "migrate"] + env: + - name: APP_MODE + value: production + - name: DB_MAX_OPEN_CONNECTIONS + value: "1" + - name: DB_DSN + valueFrom: + secretKeyRef: + name: twhelp-dcbot-secret + key: db-dsn diff --git a/k8s/overlays/prod/kustomization.yaml b/k8s/overlays/prod/kustomization.yaml new file mode 100644 index 0000000..30154a0 --- /dev/null +++ b/k8s/overlays/prod/kustomization.yaml @@ -0,0 +1,9 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +namespace: twhelp +resources: + - ../../base + - ingress.yml +patchesStrategicMerge: + - bot.yml + - jobs.yml \ No newline at end of file