diff --git a/.github/workflows/release-docker.yml b/.github/workflows/release-docker.yml index baf54492..81b1797d 100644 --- a/.github/workflows/release-docker.yml +++ b/.github/workflows/release-docker.yml @@ -32,3 +32,15 @@ jobs: pass: ${{ secrets.DOCKER_PASSWORD }} slug: asyncapi/server-api description: Server API providing official AsyncAPI tools + + deploy-app: + name: Deploy to DigitalOcean App + needs: publish-docker + runs-on: ubuntu-latest + steps: + - name: Deploy to DigitalOcean App + uses: digitalocean/app_action@v1.1.5 + with: + app_name: "server-api" + token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} + images: '[{"name":"asyncapi-server-api","image":{"registry_type":"DOCKER_HUB","registry":"asyncapi","repository":"server-api","tag":"latest"}}]' diff --git a/README.md b/README.md index b4b13209..c7775a5a 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,24 @@ server is ready to use on [http://localhost:80](http://localhost:80). 2. Write code and tests. 3. Make sure all tests pass `npm test` +## Deployment + +This project is deployed to [DigitalOcean App Platform](https://www.digitalocean.com/products/app-platform/) using [Terraform](https://www.terraform.io/) and [GitHub Actions](https://www.github.com/digitalocean/app_action/). To deploy it to your own account, follow these steps: + +1. Fork this repository. +2. Create a [DigitalOcean Personal Access Token](https://cloud.digitalocean.com/account/api/tokens) with `read` and `write` permissions. For more information, see [DigitalOcean's documentation](https://docs.digitalocean.com/reference/api/create-personal-access-token/). +3. Run `terraform init` to initialize the Terraform project as can be seen [here](./deployments/apps/main.tf). This should be run being located at ./deployments/apps directory preferably. +4. Run `terraform apply` to create the necessary infrastructure. + +> [!NOTE] +> You need to export the following environment variables before running `terraform apply`: +> - `DIGITALOCEAN_ACCESS_TOKEN`: Your DigitalOcean Personal Access Token. + + +### How the GitHub workflow works + +The [GitHub workflow](./.github/workflows/release-docker.yml) is triggered when a new tag is pushed to the repository. It will build a new Docker image and push it to the [Docker Hub](https://hub.docker.com/r/asyncapi/server-api) repository. Then the [DigitalOcean App Platform GitHub Action](https://www.github.com/digitalocean/app_action/) updates the application with the new image. + ## Contribution Read [CONTRIBUTING](https://github.com/asyncapi/.github/blob/master/CONTRIBUTING.md) guide. diff --git a/deployments/apps/.gitignore b/deployments/apps/.gitignore new file mode 100644 index 00000000..4c305a08 --- /dev/null +++ b/deployments/apps/.gitignore @@ -0,0 +1,3 @@ +.terraform +terraform.tfstate +terraform.tfstate.backup diff --git a/deployments/apps/.terraform.lock.hcl b/deployments/apps/.terraform.lock.hcl new file mode 100644 index 00000000..8bf3ce37 --- /dev/null +++ b/deployments/apps/.terraform.lock.hcl @@ -0,0 +1,26 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/digitalocean/digitalocean" { + version = "2.34.1" + constraints = ">= 2.0.0" + hashes = [ + "h1:iRyhFUfKnDRWx75+alSOEtdS0BtNUkrvLusC15s34eo=", + "zh:022d4c97af3d022d4e3735a81c6a7297aa43c3b28a8cecaa0ff58273a5677e2e", + "zh:1922f86d5710707eb497fbebcb1a1c5584c843a7e95c3900d750d81bd2785204", + "zh:1b7ab7c67a26c399eb5aa8a7a695cb59279c6a1a562ead3064e4a6b17cdacabe", + "zh:1dc666faa2ec0efc32329b4c8ff79813b54741ef1741bc42d90513e5ba904048", + "zh:220dec61ffd9448a91cca92f2bc6642df10db57b25d3d27036c3a370e9870cb7", + "zh:262301545057e654bd6193dc04b01666531fccfcf722f730827695098d93afa7", + "zh:63677684a14e6b7790833982d203fb2f84b105ad6b9b490b3a4ecc7043cdba81", + "zh:67a2932227623073aa9431a12916b52ce1ccddb96f9a2d6cdae2aaf7558ccbf8", + "zh:70dfc6ac33ee140dcb29a971df7eeb15117741b5a75b9f8486c5468c9dd28f24", + "zh:7e3b3b62754e86442048b4b1284e10807e3e58f417e1d59a4575dd29ac6ba518", + "zh:7e6fe662b1e283ad498eb2549d0c2260b908ab5b848e05f84fa4acdca5b4d5ca", + "zh:9c554170f20e659222896533a3a91954fb1d210eea60de05aea803b36d5ccd5d", + "zh:ad2f64d758bd718eb39171f1c31219900fd2bfb552a14f6a90b18cfd178a74b4", + "zh:cfce070000e95dfe56a901340ac256f9d2f84a73bf62391cba8a8e9bf1f857e0", + "zh:d5ae30eccd53ca7314157e62d8ec53151697ed124e43b24b2d16c565054730c6", + "zh:fbe5edf5337adb7360f9ffef57d02b397555b6a89bba68d1b60edfec6e23f02c", + ] +} diff --git a/deployments/apps/main.tf b/deployments/apps/main.tf new file mode 100644 index 00000000..64dd9351 --- /dev/null +++ b/deployments/apps/main.tf @@ -0,0 +1,82 @@ +terraform { + required_version = ">= 1.0.0" + + required_providers { + digitalocean = { + source = "digitalocean/digitalocean" + version = ">= 2.0.0" + } + } +} + +provider "digitalocean" {} + +resource "digitalocean_app" "server-api" { + spec { + name = "server-api" + region = "sfo3" + + domain { + name = "api.asyncapi.com" + type = "PRIMARY" + } + + ingress { + rule { + component { + name = "asyncapi-server-api" + } + match { + path { + prefix = "/" + } + } + cors { + allow_origins { + exact = "*" + } + allow_methods = ["GET", "POST", "PUT", "DELETE", "OPTIONS"] + allow_headers = ["*"] + } + } + } + + service { + name = "asyncapi-server-api" + http_port = 80 + health_check { + http_path = "/v1/help/validate" + port = 80 + } + env { + key = "PORT" + value = "80" + } + + image { + registry_type = "DOCKER_HUB" + registry = "asyncapi" + repository = "server-api" + tag = "latest" + } + + instance_count = 1 + instance_size_slug = "basic-xs" // $10/month + + alert { + rule = "CPU_UTILIZATION" + value = 80 + operator = "GREATER_THAN" + window = "TEN_MINUTES" + } + } + + alert { + rule = "DEPLOYMENT_FAILED" + } + } +} + +output "live_url" { + value = digitalocean_app.server-api.default_ingress +} diff --git a/deployments/k8s/.helmignore b/deployments/k8s/.helmignore deleted file mode 100644 index 0e8a0eb3..00000000 --- a/deployments/k8s/.helmignore +++ /dev/null @@ -1,23 +0,0 @@ -# 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/deployments/k8s/Chart.yaml b/deployments/k8s/Chart.yaml deleted file mode 100644 index d5d8924e..00000000 --- a/deployments/k8s/Chart.yaml +++ /dev/null @@ -1,7 +0,0 @@ -apiVersion: v2 -name: asyncapi-server-api -description: Helm chart that installs the AsyncAPI Server API - https://github.com/asyncapi/server-api - -type: application -version: 0.1.0 -appVersion: "0.1.0-alpha" diff --git a/deployments/k8s/README.md b/deployments/k8s/README.md deleted file mode 100644 index ce890d29..00000000 --- a/deployments/k8s/README.md +++ /dev/null @@ -1,32 +0,0 @@ -# asyncapi-event-gateway - -![Version: 0.1.0](https://img.shields.io/badge/Version-0.1.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 0.1.0-alpha](https://img.shields.io/badge/AppVersion-0.1.0--alpha-informational?style=flat-square) - -Helm chart that installs the AsyncAPI Server API - https://github.com/asyncapi/server-api - -## Values - -| Key | Type | Default | Description | -|-----|------|---------|-------------| -| affinity | object | `{}` | | -| autoscaling.enabled | bool | `true` | | -| autoscaling.maxReplicas | int | `4` | | -| autoscaling.minReplicas | int | `1` | | -| autoscaling.targetCPUUtilizationPercentage | int | `80` | | -| fullnameOverride | string | `""` | | -| image.pullPolicy | string | `"Always"` | | -| image.repository | string | `"asyncapi/server-api"` | | -| image.tag | string | `"latest"` | | -| imagePullSecrets | list | `[]` | | -| nameOverride | string | `""` | | -| nodeSelector | object | `{}` | | -| podAnnotations | object | `{}` | | -| podSecurityContext | object | `{}` | | -| ports | object | `{"http":80}` | Server API opened ports. | -| ports.http | int | `80` | Port where the Server API will be available. | -| replicaCount | int | `2` | | -| resources | object | `{}` | | -| securityContext | object | `{}` | | -| service.annotations | object | `{}` | | -| service.type | string | `"ClusterIP"` | | -| tolerations | list | `[]` | | diff --git a/deployments/k8s/templates/NOTES.txt b/deployments/k8s/templates/NOTES.txt deleted file mode 100644 index 8e81929e..00000000 --- a/deployments/k8s/templates/NOTES.txt +++ /dev/null @@ -1,16 +0,0 @@ -1. Get the application URL by running these commands: -{{- if contains "NodePort" .Values.service.type }} - export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "asyncapi-server-api.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 "asyncapi-server-api.fullname" . }}' - export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "asyncapi-server-api.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 "asyncapi-server-api.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:80 to use your application" - kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 80:$CONTAINER_PORT -{{- end }} diff --git a/deployments/k8s/templates/_helpers.tpl b/deployments/k8s/templates/_helpers.tpl deleted file mode 100644 index f022e746..00000000 --- a/deployments/k8s/templates/_helpers.tpl +++ /dev/null @@ -1,51 +0,0 @@ -{{/* -Expand the name of the chart. -*/}} -{{- define "asyncapi-server-api.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 "asyncapi-server-api.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 "asyncapi-server-api.chart" -}} -{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} -{{- end }} - -{{/* -Selector labels -*/}} -{{- define "asyncapi-server-api.selectorLabels" -}} -app.kubernetes.io/name: {{ include "asyncapi-server-api.name" . }} -app.kubernetes.io/instance: {{ .Release.Name }} -{{- end }} - -{{/* -Common labels -*/}} -{{- define "asyncapi-server-api.labels" -}} -helm.sh/chart: {{ include "asyncapi-server-api.chart" . }} -{{ include "asyncapi-server-api.selectorLabels" . }} -{{- if .Chart.AppVersion }} -app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} -{{- end }} -app.kubernetes.io/managed-by: {{ .Release.Service }} -{{- end }} \ No newline at end of file diff --git a/deployments/k8s/templates/deployment.yaml b/deployments/k8s/templates/deployment.yaml deleted file mode 100644 index aab5d9ea..00000000 --- a/deployments/k8s/templates/deployment.yaml +++ /dev/null @@ -1,69 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: {{ include "asyncapi-server-api.fullname" . }} - namespace: {{ .Release.Namespace }} - labels: - {{- include "asyncapi-server-api.labels" . | nindent 4 }} -spec: - {{- if not .Values.autoscaling.enabled }} - replicas: {{ .Values.replicaCount }} - {{- end }} - selector: - matchLabels: - {{- include "asyncapi-server-api.selectorLabels" . | nindent 6 }} - template: - metadata: - {{- with .Values.podAnnotations }} - annotations: - {{- toYaml . | nindent 8 }} - {{- end }} - labels: - {{- include "asyncapi-server-api.selectorLabels" . | nindent 8 }} - {{- range $key, $value := .Values.deploymentExtraLabels }} - {{ $key }}: {{ $value | quote }} - {{- end }} - 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 }} - ports: - - name: http - containerPort: {{ .Values.ports.http }} - protocol: TCP - livenessProbe: - httpGet: - path: / - port: {{ .Values.ports.http }} - initialDelaySeconds: {{ .Values.initialDelaySeconds.liveness }} - periodSeconds: {{ .Values.periodSeconds.liveness }} - readinessProbe: - httpGet: - path: / - port: {{ .Values.ports.http }} - initialDelaySeconds: {{ .Values.initialDelaySeconds.readiness }} - periodSeconds: {{ .Values.periodSeconds.readiness }} - - 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/deployments/k8s/templates/hpa.yaml b/deployments/k8s/templates/hpa.yaml deleted file mode 100644 index d6a525a3..00000000 --- a/deployments/k8s/templates/hpa.yaml +++ /dev/null @@ -1,33 +0,0 @@ -{{- if .Values.autoscaling.enabled }} -apiVersion: autoscaling/v2beta2 -kind: HorizontalPodAutoscaler -metadata: - name: {{ include "asyncapi-server-api.fullname" . }} - namespace: {{ .Release.Namespace }} - labels: - {{- include "asyncapi-server-api.labels" . | nindent 4 }} -spec: - scaleTargetRef: - apiVersion: apps/v1 - kind: Deployment - name: {{ include "asyncapi-server-api.fullname" . }} - minReplicas: {{ .Values.autoscaling.minReplicas }} - maxReplicas: {{ .Values.autoscaling.maxReplicas }} - metrics: - {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} - - type: Resource - resource: - name: cpu - target: - type: Utilization - averageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} - {{- end }} - {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} - - type: Resource - resource: - name: memory - target: - type: Utilization - averageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} - {{- end }} -{{- end }} diff --git a/deployments/k8s/templates/ingress.yaml b/deployments/k8s/templates/ingress.yaml deleted file mode 100644 index b8d3d0ef..00000000 --- a/deployments/k8s/templates/ingress.yaml +++ /dev/null @@ -1,26 +0,0 @@ -{{- if .Values.ingress.enabled }} -apiVersion: networking.k8s.io/v1 -kind: Ingress -metadata: - name: {{ include "asyncapi-server-api.fullname" . }} - namespace: {{ .Release.Namespace }} - annotations: - cert-manager.io/issuer: {{ .Values.ingress.issuerName }} # here we use the name of the issuer we created through this chart. -spec: - tls: - - hosts: - - {{ .Values.host }} - secretName: {{ .Values.ingress.secretName }} - ingressClassName: nginx - rules: - - host: {{ .Values.host }} - http: - paths: - - path: / - pathType: Prefix - backend: - service: - name: {{ include "asyncapi-server-api.fullname" . }} - port: - number: {{ .Values.ports.http }} -{{- end }} diff --git a/deployments/k8s/templates/service.yaml b/deployments/k8s/templates/service.yaml deleted file mode 100644 index 6a411ff5..00000000 --- a/deployments/k8s/templates/service.yaml +++ /dev/null @@ -1,20 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: {{ include "asyncapi-server-api.fullname" . }} - namespace: {{ .Release.Namespace }} - labels: - {{- include "asyncapi-server-api.labels" . | nindent 4 }} - {{- with .Values.service.annotations }} - annotations: - {{- toYaml . | nindent 4 }} - {{- end }} -spec: - type: {{ .Values.service.type }} - ports: - - name: http - protocol: TCP - port: {{ .Values.ports.http }} - targetPort: http - selector: - {{- include "asyncapi-server-api.selectorLabels" . | nindent 4 }} diff --git a/deployments/k8s/values.yaml b/deployments/k8s/values.yaml deleted file mode 100644 index 32ad646f..00000000 --- a/deployments/k8s/values.yaml +++ /dev/null @@ -1,79 +0,0 @@ -# Default values for asyncapi-event-gateway. -# This is a YAML-formatted file. -# Declare variables to be passed into your templates. - -image: - repository: asyncapi/server-api - pullPolicy: Always - tag: latest - -imagePullSecrets: [] - -replicaCount: 2 - -host: api.asyncapi.com -# Server API opened ports. -ports: - # Port where the Server API will be available. - http: 80 - -#periodSeconds for livenees and Readiness Ports -periodSeconds: - liveness: 10 - readiness: 6 -#initialDelaySeconds for liveness and Readiness Ports -initialDelaySeconds: - liveness: 30 - readiness: 30 -# Env vars. Needed for configuring the app. -env: {} - -nameOverride: "" -fullnameOverride: "" - -podAnnotations: {} - -podSecurityContext: {} - # fsGroup: 2000 - -securityContext: {} - # capabilities: - # drop: - # - ALL - # readOnlyRootFilesystem: true - # runAsNonRoot: true - # runAsUser: 1000 - -service: - type: ClusterIP - annotations: {} - -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: true - minReplicas: 1 - maxReplicas: 4 - targetCPUUtilizationPercentage: 80 - # targetMemoryUtilizationPercentage: 80 - -nodeSelector: {} - -tolerations: [] - -affinity: {} - -ingress: - enabled: false - issuerName: "letsencrypt-prod" - secretName: "server-api-cert"