Skip to content

Commit 399473e

Browse files
authored
Merge pull request #5 from crane-cloud/deploy/app
feat: add deploy app workflow
2 parents 55948a0 + 8bef4c4 commit 399473e

15 files changed

Lines changed: 521 additions & 7 deletions

.github/workflows/staging.yml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
name: staging
3+
4+
on:
5+
push:
6+
branches:
7+
- master
8+
9+
workflow_dispatch:
10+
11+
jobs:
12+
build:
13+
outputs:
14+
image: ${{ steps.export.outputs.image }}
15+
tag: ${{ steps.export.outputs.tag }}
16+
17+
runs-on: ubuntu-latest
18+
env:
19+
image: cranecloud/monitoring-api
20+
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v2
24+
25+
- name: Install (Buildx)
26+
uses: docker/setup-buildx-action@v1
27+
28+
- name: Login to Docker Hub
29+
uses: docker/login-action@v2
30+
with:
31+
username: ${{ secrets.DOCKERHUB_USERNAME }}
32+
password: ${{ secrets.DOCKERHUB_TOKEN }}
33+
34+
- id: meta
35+
name: Tag
36+
uses: docker/metadata-action@v3
37+
with:
38+
flavor: |
39+
latest=true
40+
images: ${{ env.image }}
41+
tags: |
42+
type=ref,event=branch
43+
type=ref,event=pr
44+
type=sha
45+
46+
- name: Build
47+
uses: docker/build-push-action@v2
48+
with:
49+
cache-from: type=gha
50+
cache-to: type=gha,mode=max
51+
context: .
52+
labels: ${{ steps.meta.outputs.labels }}
53+
push: true
54+
tags: ${{ steps.meta.outputs.tags }}
55+
56+
- id: export
57+
name: Export
58+
uses: actions/github-script@v5
59+
with:
60+
script: |
61+
const metadata = JSON.parse(`${{ steps.meta.outputs.json }}`)
62+
const fullUrl = metadata.tags.find((t) => t.includes(':sha-'))
63+
if (fullUrl == null) {
64+
core.error('Unable to find sha tag of image')
65+
} else {
66+
const tag = fullUrl.split(':')[1]
67+
core.setOutput('image', fullUrl)
68+
core.setOutput('tag', tag)
69+
}
70+
71+
Microservice:
72+
name: Deploy (Staging)
73+
74+
needs:
75+
- Build
76+
77+
runs-on: ubuntu-latest
78+
env:
79+
namespace: cranecloud-microservice
80+
image: cranecloud/monitoring-api
81+
82+
steps:
83+
- name: Checkout code
84+
uses: actions/checkout@v2
85+
86+
- uses: azure/k8s-set-context@v1
87+
with:
88+
kubeconfig: ${{ secrets.RENU_KUBECONFIG}}
89+
90+
- name: Helm Release
91+
run: |
92+
helm upgrade --install --create-namespace \
93+
monitoring-api ./helm/chart \
94+
--values helm/values.staging.yaml \
95+
--namespace $namespace \
96+
--set image.tag="${{ needs.build.outputs.tag }}" \
97+
--set environment.DATABASE_URI="${{ secrets.STAGING_DATABASE_URI }}" \
98+
--set environment.DATABASE_USER="${{ secrets.STAGING_DATABASE_USER }}" \
99+
--set environment.TEST_DATABASE_URL="${{ secrets.STAGING_TEST_DATABASE_URL }}" \
100+
--set environment.FLASK_APP_SALT="${{ secrets.STAGING_FLASK_APP_SALT }}" \
101+
--set environment.FLASK_APP_SECRET="${{ secrets.STAGING_FLASK_APP_SECRET }}" \
102+
--set environment.PRODUCT_BASE_URL="${{ secrets.STAGING_PRODUCT_BASE_URL }}" \
103+
--set environment.LOGGER_APP_URL="${{ secrets.STAGING_LOGGER_APP_URL }}" \
104+
--timeout=300s
105+
106+
- name: Monitor Rollout
107+
run: |
108+
kubectl rollout status deployment/monitoring-api --timeout=300s --namespace $namespace

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ COPY . /app
2020

2121

2222
# make port available to the world outside this container
23-
EXPOSE 4000
23+
EXPOSE 5000
2424

2525
# connect to start script when db is being used
26-
CMD ["flask", "run", "--host=0.0.0.0", "--port=4000"]
26+
ENTRYPOINT ["sh", "/app/scripts/start.sh"]

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ services:
33
database:
44
restart: always
55
image: postgres:10.8-alpine
6-
container_name: postgres-db
6+
container_name: monitoring-postgres-db
77
environment:
88
POSTGRES_USER: postgres
99
POSTGRES_DB: cc-monitoring
@@ -17,7 +17,7 @@ services:
1717
build:
1818
context: .
1919
dockerfile: Dockerfile
20-
container_name: flask-api
20+
container_name: monitoring-api
2121
environment:
2222
FLASK_APP_SECRET:
2323
JWT_SALT:

helm/chart/.helmignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Patterns to ignore when building packages.
2+
# This supports shell glob matching, relative path matching, and
3+
# negation (prefixed with !). Only one pattern per line.
4+
.DS_Store
5+
# Common VCS dirs
6+
.git/
7+
.gitignore
8+
.bzr/
9+
.bzrignore
10+
.hg/
11+
.hgignore
12+
.svn/
13+
# Common backup files
14+
*.swp
15+
*.bak
16+
*.tmp
17+
*.orig
18+
*~
19+
# Various IDEs
20+
.project
21+
.idea/
22+
*.tmproj
23+
.vscode/

helm/chart/Chart.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: v2
2+
name: cranecloud
3+
description: A Helm chart for cranecloud applications
4+
5+
# A chart can be either an 'application' or a 'library' chart.
6+
#
7+
# Application charts are a collection of templates that can be packaged into versioned archives
8+
# to be deployed.
9+
#
10+
# Library charts provide useful utilities or functions for the chart developer. They're included as
11+
# a dependency of application charts to inject those utilities and functions into the rendering
12+
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
13+
type: application
14+
15+
# This is the chart version. This version number should be incremented each time you make changes
16+
# to the chart and its templates, including the app version.
17+
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18+
version: 0.1.4
19+
20+
# This is the version number of the application being deployed. This version number should be
21+
# incremented each time you make changes to the application. Versions are not expected to
22+
# follow Semantic Versioning. They should reflect the version the application is using.
23+
appVersion: 1.16.0

helm/chart/templates/_helpers.tpl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{{/* vim: set filetype=mustache: */}}
2+
{{/*
3+
Expand the name of the chart.
4+
*/}}
5+
{{- define "cranecloud.name" -}}
6+
{{- default .Release.Name .Values.nameOverride .Chart.Name | trunc 63 | trimSuffix "-" }}
7+
{{- end }}
8+
9+
10+
{{/*
11+
Create chart name and version as used by the chart label.
12+
*/}}
13+
{{- define "cranecloud.chart" -}}
14+
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
15+
{{- end }}
16+
17+
{{/*
18+
Common labels
19+
*/}}
20+
{{- define "cranecloud.labels" -}}
21+
helm.sh/chart: {{ include "cranecloud.chart" . }}
22+
{{ include "cranecloud.selectorLabels" . }}
23+
{{- if .Chart.AppVersion }}
24+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
25+
{{- end }}
26+
app.kubernetes.io/managed-by: {{ .Release.Service }}
27+
{{- end }}
28+
29+
{{/*
30+
Selector labels
31+
*/}}
32+
{{- define "cranecloud.selectorLabels" -}}
33+
app: {{ include "cranecloud.name" . }}
34+
app.kubernetes.io/name: {{ include "cranecloud.name" . }}
35+
app.kubernetes.io/instance: {{ .Release.Name }}
36+
{{- end }}
37+
38+
{{/*
39+
Create the name of the service account to use
40+
*/}}
41+
{{- define "cranecloud.serviceAccountName" -}}
42+
{{- if .Values.serviceAccount.create }}
43+
{{- default (include "cranecloud.name" .) .Values.serviceAccount.name }}
44+
{{- else }}
45+
{{- default "default" .Values.serviceAccount.name }}
46+
{{- end }}
47+
{{- end }}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{{- if .Values.celery.create -}}
2+
apiVersion: apps/v1
3+
kind: Deployment
4+
metadata:
5+
name: {{ include "cranecloud.name" . }}-celery
6+
labels:
7+
{{- include "cranecloud.labels" . | nindent 4 }}
8+
spec:
9+
replicas: {{ .Values.celery.replicaCount }}
10+
selector:
11+
matchLabels:
12+
{{- include "cranecloud.selectorLabels" . | nindent 6 }}
13+
strategy:
14+
rollingUpdate:
15+
maxSurge: 1
16+
maxUnavailable: 25%
17+
type: RollingUpdate
18+
template:
19+
metadata:
20+
{{- with .Values.podAnnotations }}
21+
annotations:
22+
{{- toYaml . | nindent 8 }}
23+
{{- end }}
24+
labels:
25+
{{- include "cranecloud.selectorLabels" . | nindent 8 }}
26+
spec:
27+
{{- with .Values.imagePullSecrets }}
28+
imagePullSecrets:
29+
{{- toYaml . | nindent 8 }}
30+
{{- end }}
31+
serviceAccountName: {{ include "cranecloud.serviceAccountName" . }}
32+
securityContext:
33+
{{- toYaml .Values.podSecurityContext | nindent 8 }}
34+
containers:
35+
- name: {{ include "cranecloud.name" . }}-celery
36+
securityContext:
37+
{{- toYaml .Values.securityContext | nindent 12 }}
38+
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
39+
imagePullPolicy: {{ .Values.image.pullPolicy }}
40+
command: {{ .Values.celery.command | toYaml | nindent 12}}
41+
env:
42+
- name: test
43+
value: test
44+
{{- range $key, $value := .Values.environment }}
45+
- name: {{ $key }}
46+
value: {{ $value | quote }}
47+
{{- end }}
48+
ports:
49+
- name: celery-worker
50+
containerPort: {{ .Values.celery.port }}
51+
protocol: TCP
52+
resources:
53+
{{- toYaml .Values.resources | nindent 12 }}
54+
{{- end }}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: {{ include "cranecloud.name" . }}
5+
labels:
6+
{{- include "cranecloud.labels" . | nindent 4 }}
7+
spec:
8+
replicas: {{ .Values.replicaCount }}
9+
selector:
10+
matchLabels:
11+
{{- include "cranecloud.selectorLabels" . | nindent 6 }}
12+
strategy:
13+
rollingUpdate:
14+
maxSurge: 1
15+
maxUnavailable: 25%
16+
type: RollingUpdate
17+
template:
18+
metadata:
19+
{{- with .Values.podAnnotations }}
20+
annotations:
21+
{{- toYaml . | nindent 8 }}
22+
{{- end }}
23+
labels:
24+
{{- include "cranecloud.selectorLabels" . | nindent 8 }}
25+
spec:
26+
{{- with .Values.imagePullSecrets }}
27+
imagePullSecrets:
28+
{{- toYaml . | nindent 8 }}
29+
{{- end }}
30+
serviceAccountName: {{ include "cranecloud.serviceAccountName" . }}
31+
securityContext:
32+
{{- toYaml .Values.podSecurityContext | nindent 8 }}
33+
containers:
34+
- name: {{ include "cranecloud.name" . }}
35+
securityContext:
36+
{{- toYaml .Values.securityContext | nindent 12 }}
37+
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
38+
imagePullPolicy: {{ .Values.image.pullPolicy }}
39+
env:
40+
{{- range $key, $value := .Values.environment }}
41+
- name: {{ $key }}
42+
value: {{ $value | quote }}
43+
{{- end }}
44+
45+
ports:
46+
- name: backend-api
47+
containerPort: 8000
48+
protocol: TCP
49+
resources:
50+
{{- toYaml .Values.resources | nindent 12 }}
51+
- name: nginx
52+
image: nginx:1.21.6
53+
imagePullPolicy: IfNotPresent
54+
ports:
55+
- name: http
56+
containerPort: 80
57+
volumeMounts:
58+
- name: nginx-proxy-config
59+
mountPath: /etc/nginx/conf.d/default.conf
60+
subPath: nginx.conf
61+
{{- with .Values.nodeSelector }}
62+
nodeSelector:
63+
{{- toYaml . | nindent 8 }}
64+
{{- end }}
65+
{{- with .Values.affinity }}
66+
affinity:
67+
{{- toYaml . | nindent 8 }}
68+
{{- end }}
69+
{{- with .Values.tolerations }}
70+
tolerations:
71+
{{- toYaml . | nindent 8 }}
72+
{{- end }}
73+
volumes:
74+
- name: nginx-proxy-config
75+
configMap:
76+
name: {{ .Release.Name }}-nginx-configmap
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: {{ .Release.Name }}-nginx-configmap
5+
data:
6+
nginx.conf: |-
7+
{{ tpl .Values.nginxConf . | indent 4 }}

0 commit comments

Comments
 (0)