Skip to content

Commit

Permalink
Merge pull request #298 from Mohmn/api-devops
Browse files Browse the repository at this point in the history
feat: add k8s files and ci cd files for user api
  • Loading branch information
Mohmn authored Dec 17, 2024
2 parents ada4c76 + 3a3f702 commit 940a9a2
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 1 deletion.
31 changes: 31 additions & 0 deletions .github/workflows/api-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CI for New Pull Requests

on:
pull_request:
branches:
- "*"
paths:
- 'apps/user/**' # Only trigger for changes in the apps/user directory

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Use Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: '18.x'

- name: Install dependencies for specific package
run: yarn workspace greenstand-wallet-app-api install

- name: Build TypeScript project
run: yarn workspace greenstand-wallet-app-api build

- name: Lint code with ESLint
run: yarn workspace greenstand-wallet-app-api lint

- name: Run tests
run: yarn workspace greenstand-wallet-app-api test
97 changes: 97 additions & 0 deletions .github/workflows/build-dev-docker-img-and-relase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: Service CI/CD Pipeline to Release and Deploy to Dev Env


on:
push:
branches:
- main
paths:
- 'apps/user/**' # Only trigger for changes in the apps/user directory

jobs:
release:
if: |
!contains(github.event.head_commit.message, 'skip-ci')
name: Build and Release Docker Image
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Use Node.js 20.x
uses: actions/setup-node@v3
with:
node-version: '20.x'

- name: Yarn Clean Install
run: yarn workspace greenstand-wallet-app-api install

- name: Install Semantic Release
run: yarn global add semantic-release @semantic-release/{git,exec,changelog}

- name: Semantic Release
run: semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Make Build
run: yarn workspace greenstand-wallet-app-api build

- name: Get NPM Version
id: package-version
uses: martinbeentjes/npm-get-version-action@master
with:
path: apps/user

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}


- name: Build and Push Docker Image
id: docker_build_release
uses: docker/build-push-action@v5
with:
context: ./apps/user/
file: ./apps/user/Dockerfile
push: true
tags: greenstand/treetracker-wallet-monorepo-user-api:${{ steps.package-version.outputs.current-version }}

outputs:
bumped_version: ${{ steps.package-version.outputs.current-version }}

deploy:
if: ${{ needs.release.result == 'success' }} && !contains(github.event.head_commit.message, 'skip-ci')
name: Deploy to Dev Environment
runs-on: ubuntu-latest
needs: release
steps:
- uses: actions/checkout@v4

- name: Install Kustomize
run: |
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
- name: Update Kustomize Image
run: |
(cd apps/user/deployment/base && kustomize edit set image greenstand/treetracker-wallet-monorepo-user-api:${{ needs.release.outputs.bumped_version }})
- name: Install DigitalOcean CLI
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DEV_DIGITALOCEAN_TOKEN }}

- name: Save Kubernetes Config
run: doctl kubernetes cluster kubeconfig save ${{ secrets.DEV_CLUSTER_NAME }}

- name: Deploy to Kubernetes
run: |
kustomize build apps/user/deployment/overlays/development | \
kubectl apply -n ${{ secrets.K8S_NAMESPACE }} --wait -f -
8 changes: 8 additions & 0 deletions apps/user/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
deployment
node_modules
dist
.git
.cache
.env
.github
.husky
14 changes: 14 additions & 0 deletions apps/user/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:18-alpine AS builder
WORKDIR /app
ENV PATH=/app/node_modules/.bin:$PATH
COPY package.json ./
RUN yarn install --silent
COPY . .
RUN yarn build
RUN yarn install --production --ignore-scripts --prefer-offline
FROM node:18-alpine AS prod
WORKDIR /app
COPY --from=builder app/dist/ ./dist
COPY --from=builder app/node_modules ./node_modules
EXPOSE 8080
CMD ["node", "dist/src/main.js"]
13 changes: 13 additions & 0 deletions apps/user/deployment/base/cluster-role-binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: k8s-wait-for
# annotations:
subjects:
- kind: ServiceAccount
name: default
namespace: treetracker-wallet-monorepo-user-api
roleRef:
kind: ClusterRole
name: k8s-wait-for
apiGroup: rbac.authorization.k8s.io
12 changes: 12 additions & 0 deletions apps/user/deployment/base/cluster-role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: k8s-wait-for
# annotations:
rules:
- apiGroups: ['']
resources: ['services', 'pods', 'jobs']
verbs: ['get', 'watch', 'list']
- apiGroups: ['batch']
resources: ['services', 'pods', 'jobs']
verbs: ['get', 'watch', 'list']
32 changes: 32 additions & 0 deletions apps/user/deployment/base/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: treetracker-wallet-monorepo-user-api
labels:
app: treetracker-wallet-monorepo-user-api
namespace: treetracker-wallet-monorepo-user-api
spec:
replicas: 2
selector:
matchLabels:
app: treetracker-wallet-monorepo-user-api
template:
metadata:
labels:
app: treetracker-wallet-monorepo-user-api
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: doks.digitalocean.com/node-pool
operator: In
values:
- microservices-node-pool
containers:
- name: treetracker-wallet-monorepo-user-api
image: greenstand/treetracker-wallet-monorepo-user-api:TAG
# get this from env as well
ports:
- containerPort: 8080
7 changes: 7 additions & 0 deletions apps/user/deployment/base/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resources:
- deployment.yaml
- mapping.yaml
- service.yaml
- namespace.yaml
- cluster-role.yaml
- cluster-role-binding.yaml
7 changes: 7 additions & 0 deletions apps/user/deployment/base/mapping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: getambassador.io/v2
kind: Mapping
metadata:
name: treetracker-wallet-monorepo-user-api
spec:
prefix: /monorepo-user/
service: treetracker-wallet-monorepo-user-api-service
4 changes: 4 additions & 0 deletions apps/user/deployment/base/namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: treetracker-wallet-monorepo-user-api
13 changes: 13 additions & 0 deletions apps/user/deployment/base/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: treetracker-wallet-monorepo-user-api-service
namespace: treetracker-wallet-monorepo-user-api
spec:
selector:
app: treetracker-wallet-monorepo-user-api
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP
4 changes: 4 additions & 0 deletions apps/user/deployment/overlays/development/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resources:
- ../../base
patchesStrategicMerge:
- mapping.yaml
11 changes: 11 additions & 0 deletions apps/user/deployment/overlays/development/mapping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: getambassador.io/v2
kind: Mapping
metadata:
name: treetracker-wallet-monorepo-user-api
spec:
cors:
origins: "*"
methods: GET, POST, PATCH, OPTIONS
headers:
- Content-Type
- Authorization
7 changes: 6 additions & 1 deletion apps/user/src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Post } from '@nestjs/common';
import { Body, Controller, Get, Post } from '@nestjs/common';
import { UserService } from './user.service';
import { RegisterUserDto } from '@dtos/register-user.dto';

Expand All @@ -10,4 +10,9 @@ export class UserController {
registerUser(@Body() registerUserDto: RegisterUserDto) {
return this.userService.createUser(registerUserDto);
}

@Get('test')
check(@Body() registerUserDto: RegisterUserDto) {
return 'tree growing!!'
}
}

0 comments on commit 940a9a2

Please sign in to comment.