diff --git a/.github/workflows/deployment.yaml b/.github/workflows/deployment.yaml new file mode 100644 index 000000000..b1d325945 --- /dev/null +++ b/.github/workflows/deployment.yaml @@ -0,0 +1,81 @@ +name: deployment +on: push +jobs: + Setup-kubeconfig: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up kubectl + uses: azure/setup-kubectl@v3 + with: + version: latest + - name: Set up kubeconfig + run: echo "${{ secrets.KUBECONFIG }}" | base64 -d > kubeconfig + + - name: Upload kubeconfig artifact + uses: actions/upload-artifact@v4 + with: + name: kubeconfig + path: kubeconfig + + Redis: + needs: Setup-kubeconfig + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + with: + name: kubeconfig + + - name: deploy-redis + run: kubectl --kubeconfig kubeconfig apply -f redis-deployment.yaml + + - name: redis-services + run: kubectl --kubeconfig kubeconfig apply -f redis-svc.yaml + + - name: redis-persitance + run: kubectl --kubeconfig kubeconfig apply -f redis-pvc.yaml + + Backend: + runs-on: ubuntu-latest + needs: Redis + steps: + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + with: + name: kubeconfig + - name: deploy-backend + run: kubectl --kubeconfig kubeconfig apply -f backend-deployment.yaml + + - name: backend-services + run: kubectl --kubeconfig kubeconfig apply -f backend-svc.yaml + + Frontend: + runs-on: ubuntu-latest + needs: Backend + steps: + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + with: + name: kubeconfig + - name: deploy-frontend + run: kubectl --kubeconfig kubeconfig apply -f frontend-deployment.yaml + + - name: frontend-services + run: kubectl --kubeconfig kubeconfig apply -f frontend-svc.yaml + + Test-runnig: + runs-on: ubuntu-latest + needs: Frontend + steps: + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + with: + name: kubeconfig + - name: test-frontend-deployment + run: kubectl --kubeconfig kubeconfig get pods -l app=frontend + - name: test-backend-deployment + run: kubectl --kubeconfig kubeconfig get pods -l app=backend + - name: test-redis-deployment + run: kubectl --kubeconfig kubeconfig get pods -l app=redis \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 000000000..7a57552dd --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,42 @@ +name: simple-fortune-cookie +on: + push +jobs: + Test: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Test Frontend + working-directory: frontend + run: go test ./... + Dockerpush: + needs: Test + name: push dockerimage to dockerhub + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: login to docker hub + id: docker-hub + env: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} + run: | + docker login -u $username -p $password + - name: build the frontend docker image + id: build-frontend-docker-image + run: | + ls -la + docker build frontend -t jeppeha/frontenddocker:latest + - name: push the frontend docker image + id: push-frontend-docker-image + run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/frontenddocker:latest + - name: build the backend docker image + id: build-backend-docker-image + run: | + ls -la + docker build backend -t jeppeha/backenddocker:latest + - name: push the backend docker image + id: push-backend-docker-image + run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/backenddocker:latest diff --git a/backend-deployment.yaml b/backend-deployment.yaml new file mode 100644 index 000000000..b6851dbc1 --- /dev/null +++ b/backend-deployment.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: backend +spec: + selector: + matchLabels: + app: backend + template: + metadata: + labels: + app: backend + spec: + containers: + - name: backend + image: jeppeha/backenddocker:latest + ports: + - containerPort: 9000 + env: + - name: REDIS_DNS + value: redis-service \ No newline at end of file diff --git a/backend-svc.yaml b/backend-svc.yaml new file mode 100644 index 000000000..1bfe4e200 --- /dev/null +++ b/backend-svc.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: backend-service +spec: + selector: + app: backend + ports: + - port: 9000 + targetPort: 9000 + type: ClusterIP \ No newline at end of file diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 000000000..0b7be642b --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,6 @@ +FROM golang:1.21-alpine AS builder +WORKDIR /app +COPY . /app +RUN go mod download && go mod verify +RUN cd /app && go build -o goapp +ENTRYPOINT ["./goapp"] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 000000000..431977718 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,30 @@ +services: + frontend-container: + image: jeppeha/frontenddocker:latest + ports: + - 8080:8080 + depends_on: + - backend-container + environment: + BACKEND_PORT: 9000 + BACKEND_DNS: backend-container + + + backend-container: + image: jeppeha/backenddocker:latest + ports: + - 9000:9000 + depends_on: + - redis-container + environment: + REDIS_PORT: 6379 + REDIS_DNS: redis-container + + + redis-container: + image: redis:7-alpine + ports: + - 6379:6379 + + + diff --git a/frontend-deployment.yaml b/frontend-deployment.yaml new file mode 100644 index 000000000..50db9c955 --- /dev/null +++ b/frontend-deployment.yaml @@ -0,0 +1,24 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: frontend +spec: + selector: + matchLabels: + app: frontend + template: + metadata: + labels: + app: frontend + spec: + containers: + - name: frontend + image: jeppeha/frontenddocker:latest + ports: + - containerPort: 8080 + env: + - name: BACKEND_DNS + value: backend-service + - name: BACKEND_PORT + value: "9000" + diff --git a/frontend-svc.yaml b/frontend-svc.yaml new file mode 100644 index 000000000..56123ab2e --- /dev/null +++ b/frontend-svc.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: frontend-service +spec: + selector: + app: frontend + ports: + - port: 8080 + targetPort: 8080 + type: NodePort diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 000000000..0b7be642b --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,6 @@ +FROM golang:1.21-alpine AS builder +WORKDIR /app +COPY . /app +RUN go mod download && go mod verify +RUN cd /app && go build -o goapp +ENTRYPOINT ["./goapp"] \ No newline at end of file diff --git a/frontend/main.go b/frontend/main.go index bede472b4..27f4da2a4 100644 --- a/frontend/main.go +++ b/frontend/main.go @@ -101,5 +101,5 @@ func main() { http.Handle("/", http.FileServer(http.Dir("./static"))) err := http.ListenAndServe(":8080", nil) - fmt.Println("%v", err) + fmt.Printf("%v", err) } diff --git a/kubeconfig b/kubeconfig new file mode 100644 index 000000000..e69de29bb diff --git a/redis-deployment.yaml b/redis-deployment.yaml new file mode 100644 index 000000000..14f19cbf4 --- /dev/null +++ b/redis-deployment.yaml @@ -0,0 +1,27 @@ +# redis-deployment.yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: redis +spec: + selector: + matchLabels: + app: redis + template: + metadata: + labels: + app: redis + spec: + volumes: + - name: redis-pvc + persistentVolumeClaim: + claimName: redis-pvc + containers: + - name: redis + image: redis:7-alpine + ports: + - containerPort: 6379 + volumeMounts: + - name: redis-pvc + mountPath: /data + diff --git a/redis-pvc.yaml b/redis-pvc.yaml new file mode 100644 index 000000000..1c1bc924f --- /dev/null +++ b/redis-pvc.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: redis-pvc +spec: + storageClassName: "gp3" + resources: + requests: + storage: 5Gi + accessModes: + - ReadWriteOnce \ No newline at end of file diff --git a/redis-svc.yaml b/redis-svc.yaml new file mode 100644 index 000000000..b604f6d7f --- /dev/null +++ b/redis-svc.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: redis-service +spec: + selector: + app: redis + ports: + - port: 6379 + targetPort: 6379 + type: ClusterIP diff --git a/workflowtest.txt b/workflowtest.txt new file mode 100644 index 000000000..21bcb5544 --- /dev/null +++ b/workflowtest.txt @@ -0,0 +1 @@ +workflowtest