Skip to content

Commit

Permalink
add demo app code
Browse files Browse the repository at this point in the history
  • Loading branch information
amila-ku committed Feb 25, 2024
1 parent 42d2470 commit bbe2a87
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 0 deletions.
87 changes: 87 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Publish to ECR & Deploy to EKS

on:
push:
branches:
- main
pull_request:
branches:
- main

env:
ECR_REPOSITORY: demo-app
EKS_CLUSTER_NAME: demo-dev
AWS_REGION: eu-west-1
HTTP_PORT: '8080'

jobs:
build:
name: Deployment
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v3

- name: setup go
uses: actions/setup-go@v5
with:
go-version: '1.21.0'

- name: test
run: |
go test -v -cover ./...
# - name: test in container
# run: |
# docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG --progress plain --no-cache --target run-test-stage .

# - name: Configure AWS credentials
# uses: aws-actions/configure-aws-credentials@v4
# with:
# aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
# aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# aws-region: ${{env.AWS_REGION}}

# - name: Login to Amazon ECR
# id: login-ecr
# uses: aws-actions/amazon-ecr-login@v2

# - name: Build, tag, and push image to Amazon ECR
# env:
# ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
# IMAGE_TAG: ${{ github.run_number }}
# run: |
# docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f Dockerfile .
# docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

# - name: Update kube config
# run: aws eks update-kubeconfig --name $EKS_CLUSTER_NAME --region $AWS_REGION

# - name: Deploy to EKS to DEV
# env:
# ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
# IMAGE_TAG: ${{ github.run_number }}
# run: |
# sed -i.bak "s|ENV|Dev|g" deploy/manifests/app-deploy.yaml
# sed -i.bak "s|HTTPPORT|$HTTP_PORT|g" deploy/manifests/app-deploy.yaml
# sed -i.bak "s|DOCKER_IMAGE|$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG|g" deploy/manifests/app-deploy.yaml && \
# kubectl apply -f deploy/manifests/app-deploy.yaml
# kubectl apply -f deploy/manifests/app-service.yaml
# kubectl apply -f deploy/manifests/app-ingress.yaml

# - name: Call endpoint with curl
# run: |
# curl -X GET "https://example.com/hello"

# - name: Deploy to EKS to PROD
# env:
# ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
# IMAGE_TAG: ${{ github.run_number }}
# run: |
# sed -i.bak "s|ENV|PROD|g" deploy/manifests/app-deploy.yaml
# sed -i.bak "s|HTTPPORT|$HTTP_PORT|g" deploy/manifests/app-deploy.yaml
# sed -i.bak "s|DOCKER_IMAGE|$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG|g" deploy/manifests/app-deploy.yaml && \
# kubectl apply -f deploy/manifests/app-deploy.yaml
# kubectl apply -f deploy/manifests/app-service.yaml
# kubectl apply -f deploy/manifests/app-ingress.yaml
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.pdf
deploy/
validation/report.html
.terraform
.terraform.lock.hcl
*.tfstate
terraform.tfvars
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM golang:1.21 as build

# Set destination for COPY
WORKDIR /app

# Download Go modules
COPY go.mod ./
RUN go mod download

# Copy source files
COPY *.go ./
# Build
RUN CGO_ENABLED=0 GOOS=linux go build -o /myapp

# Run the tests in the container
FROM build AS run-test-stage
RUN go test -v -cover ./...

# Final lean image
FROM gcr.io/distroless/base-debian11
COPY --from=build --chmod=764 /myapp /

EXPOSE 8080/tcp

CMD ["/myapp"]
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/DevOpsLK/demo

go 1.21.0
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"
"log"
"net/http"
"os"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello! From DevOps Sri Lanka")
}

func main() {
port := os.Getenv("HTTP_PORT")
if port == "" {
port = "8080" // Default port if HTTP_PORT environment variable is not set
}
http.HandleFunc("/hello", helloHandler)
fmt.Printf("Server is running on port %s\n", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
32 changes: 32 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestHelloHandler(t *testing.T) {
// Create a request to the /hello endpoint (GET method)
req, err := http.NewRequest("GET", "/hello", nil)
if err != nil {
t.Fatal(err)
}

// Create a ResponseRecorder to record the response
rr := httptest.NewRecorder()

// Call the helloHandler with the request and response recorder
helloHandler(rr, req)

// Check the status code
if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
}

// Check the response body
expected := "Hello! From Nitro\n"
if body := rr.Body.String(); body != expected {
t.Errorf("Handler returned unexpected body: got %v want %v", body, expected)
}
}

0 comments on commit bbe2a87

Please sign in to comment.