Skip to content

Commit 1ab6108

Browse files
Merge pull request #2 from EspenHegdal123/deployment-to-berget
Deploying the k8s files for the pipeline-api repo. deplyment of this …
2 parents ad49425 + 993e37b commit 1ab6108

File tree

13 files changed

+392
-0
lines changed

13 files changed

+392
-0
lines changed

.github/workflows/ci.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: Continuous Integration
2+
3+
env:
4+
# Use docker.io for Docker Hub if empty
5+
REGISTRY: ghcr.io
6+
# github.repository as <account>/<repo>
7+
IMAGE_NAME: ${{ github.repository }}
8+
9+
on:
10+
push:
11+
branches:
12+
- main
13+
paths-ignore:
14+
- 'k8s/**' # Avoid CI runs for updates inside k8s folder
15+
16+
jobs:
17+
ci:
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
# Step 1: Checkout the repository code
22+
- name: 🛎️ Checkout
23+
uses: actions/checkout@v4
24+
with:
25+
fetch-depth: 0
26+
27+
# Step 2: Extract and normalize repository name into lowercase (e.g., pipeline-api)
28+
- id: imagename
29+
uses: ASzc/change-string-case-action@v6
30+
with:
31+
string: ${{ github.repository }}
32+
33+
# Step 3: Get the current version of the project (assumes `package.json` exists)
34+
- name: 📝 Get Current Version
35+
id: package-version
36+
uses: martinbeentjes/npm-get-version-action@main
37+
38+
# Step 4: Login to Docker Registry (GHCR authentication)
39+
- name: 🔐 Login to Docker Registry
40+
uses: docker/login-action@v3
41+
with:
42+
registry: ghcr.io
43+
username: ${{ github.actor }}
44+
password: ${{ secrets.GITHUB_TOKEN }}
45+
46+
# Step 5: Set up Docker Buildx (required for advanced Docker build features)
47+
- name: 🏗️ Set up Docker Buildx
48+
uses: docker/setup-buildx-action@v3
49+
50+
# Step 6: Build and push Docker image
51+
- name: 🔧 Build and push Docker Image
52+
uses: docker/build-push-action@v6
53+
with:
54+
push: true
55+
tags: |
56+
${{ env.REGISTRY }}/${{ steps.imagename.outputs.lowercase }}:${{ steps.package-version.outputs.current-version }}
57+
${{ env.REGISTRY }}/${{ steps.imagename.outputs.lowercase }}:latest
58+
cache-from: type=gha
59+
cache-to: type=gha,mode=max
60+
61+
# Step 7: Update version in package.json to prepare for next development cycle
62+
- name: 🎫 Update patch version
63+
run: |
64+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
65+
git config --global user.name "github-actions[bot]"
66+
git pull --rebase origin main
67+
npm version prerelease --preid=rc # Increment version (e.g., 1.0.0 -> 1.0.1-rc.0)
68+
git push origin main

Dockerfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Use Node.js with alpine for a lightweight environment
2+
FROM node:lts-alpine3.20 AS base
3+
# Install system dependencies required for the application
4+
RUN apk update && apk add --no-cache \
5+
python3 \
6+
make \
7+
g++ \
8+
ca-certificates
9+
# Set working directory
10+
WORKDIR /app
11+
# Copy `package.json` and `package-lock.json` to the container
12+
COPY package*.json ./
13+
# Copy the rest of the application source code
14+
COPY . .
15+
# Expose API port
16+
EXPOSE 4000
17+
18+
FROM base AS prod
19+
# Install only production dependencies
20+
RUN npm ci --omit=dev
21+
# Build the TypeScript application
22+
RUN npm run build
23+
# Start the Node.js application in prod mode
24+
CMD ["npm", "start"]
25+
26+
FROM base AS dev
27+
# Install dependencies (including dev)
28+
RUN npm ci
29+
# Start the Node.js application in dev mode
30+
CMD ["npm", "dev"]

k8s/base/api.yaml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: pipeline-api
5+
namespace: default
6+
labels:
7+
app: pipeline-api
8+
spec:
9+
replicas: 3
10+
selector:
11+
matchLabels:
12+
app: pipeline-api
13+
template:
14+
metadata:
15+
labels:
16+
app: pipeline-api
17+
spec:
18+
terminationGracePeriodSeconds: 60
19+
initContainers:
20+
- name: db-migration
21+
image: ghcr.io/klimatbyran/pipeline-api
22+
command: ['npm', 'run', 'migrate']
23+
resources:
24+
requests:
25+
memory: '64Mi'
26+
cpu: '100m'
27+
limits:
28+
memory: '128Mi'
29+
cpu: '200m'
30+
env:
31+
- name: POSTGRES_PASSWORD
32+
valueFrom:
33+
secretKeyRef:
34+
name: postgresql
35+
key: postgres-password
36+
- name: DATABASE_URL
37+
value: postgresql://postgres:$(POSTGRES_PASSWORD)@postgresql:5432/pipeline-api
38+
containers:
39+
- name: pipeline-api
40+
image: ghcr.io/klimatbyran/pipeline-api
41+
resources:
42+
requests:
43+
memory: "512Mi"
44+
cpu: "250m"
45+
limits:
46+
memory: "1024Mi"
47+
cpu: "500m"
48+
ports:
49+
- containerPort: 4000
50+
env:
51+
- name: POSTGRES_PASSWORD
52+
valueFrom:
53+
secretKeyRef:
54+
name: postgresql
55+
key: postgres-password
56+
- name: DATABASE_URL
57+
value: postgresql://postgres:$(POSTGRES_PASSWORD)@postgresql:5432/pipeline-api
58+
- name: API_SECRET
59+
valueFrom:
60+
secretKeyRef:
61+
name: env
62+
key: API_SECRET
63+
- name: REDIS_PASSWORD
64+
valueFrom:
65+
secretKeyRef:
66+
name: redis
67+
key: redis-password
68+
- name: REDIS_HOST
69+
value: redis-master
70+
- name: CHROMA_HOST
71+
value: http://chromadb:8000
72+
- name: FRONTEND_URL
73+
value: https://frontend.myapp.com
74+
imagePullPolicy: Always

k8s/base/hpa.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: autoscaling/v2
2+
kind: HorizontalPodAutoscaler
3+
metadata:
4+
name: pipeline-api-hpa
5+
namespace: default
6+
spec:
7+
scaleTargetRef:
8+
apiVersion: apps/v1
9+
kind: Deployment
10+
name: pipeline-api
11+
minReplicas: 2
12+
maxReplicas: 12
13+
metrics:
14+
- type: Resource
15+
resource:
16+
name: cpu
17+
target:
18+
type: Utilization
19+
averageUtilization: 70
20+
- type: Resource
21+
resource:
22+
name: memory
23+
target:
24+
type: Utilization
25+
averageUtilization: 80
26+
behavior:
27+
scaleDown:
28+
stabilizationWindowSeconds: 300
29+
scaleUp:
30+
stabilizationWindowSeconds: 60

k8s/base/ingress.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: Ingress
3+
metadata:
4+
name: pipeline-api
5+
annotations:
6+
cert-manager.io/cluster-issuer: 'letsencrypt-prod'
7+
spec:
8+
tls:
9+
- hosts:
10+
- api.pipeline.myapp.com
11+
secretName: pipeline-api-tls
12+
rules:
13+
- host: api.pipeline.myapp.com
14+
http:
15+
paths:
16+
- path: /
17+
pathType: Prefix
18+
backend:
19+
service:
20+
name: pipeline-api
21+
port:
22+
number: 80

k8s/base/kustomization.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
4+
resources:
5+
- api.yaml
6+
- service.yaml
7+
- ingress.yaml
8+
- worker.yaml
9+
- hpa.yaml

k8s/base/service.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: pipeline-api
5+
namespace: default
6+
spec:
7+
selector:
8+
app: pipeline-api
9+
ports:
10+
- protocol: TCP
11+
port: 80
12+
targetPort: 4000

k8s/base/worker.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: pipeline-api-worker
5+
namespace: default
6+
labels:
7+
app: pipeline-api-worker
8+
spec:
9+
replicas: 1
10+
selector:
11+
matchLabels:
12+
app: pipeline-api-worker
13+
template:
14+
metadata:
15+
labels:
16+
app: pipeline-api-worker
17+
spec:
18+
containers:
19+
- name: pipeline-api-worker
20+
image: ghcr.io/klimatbyran/pipeline-api
21+
command: ['npm', 'run', 'worker']
22+
resources:
23+
requests:
24+
memory: "512Mi"
25+
cpu: "250m"
26+
limits:
27+
memory: "1024Mi"
28+
cpu: "500m"
29+
env:
30+
- name: REDIS_HOST
31+
value: redis-master
32+
- name: REDIS_PASSWORD
33+
valueFrom:
34+
secretKeyRef:
35+
name: redis
36+
key: redis-password
37+
- name: DATABASE_URL
38+
valueFrom:
39+
secretKeyRef:
40+
name: postgresql
41+
key: DATABASE_URL
42+
- name: API_SECRET
43+
valueFrom:
44+
secretKeyRef:
45+
name: env
46+
key: API_SECRET
47+
imagePullPolicy: Always
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: Ingress
3+
metadata:
4+
name: pipeline-api
5+
spec:
6+
tls:
7+
- hosts:
8+
- api.pipeline.myapp.com
9+
secretName: api-pipeline-myapp-com-tls
10+
rules:
11+
- host: api.pipeline.myapp.com
12+
http:
13+
paths:
14+
- path: /
15+
pathType: Prefix
16+
backend:
17+
service:
18+
name: pipeline-api
19+
port:
20+
number: 80
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
namespace: pipeline-api
4+
resources:
5+
- ../../base
6+
patches:
7+
- path: ingress-patch.yaml
8+
images:
9+
- name: ghcr.io/klimatbyran/pipeline-api
10+
newTag: '1.0.0' # {"$imagepolicy": "flux-system:pipeline-api:tag"}

0 commit comments

Comments
 (0)