Skip to content

Commit bbe2a87

Browse files
committed
add demo app code
1 parent 42d2470 commit bbe2a87

File tree

6 files changed

+176
-0
lines changed

6 files changed

+176
-0
lines changed

.github/workflows/build.yaml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: Publish to ECR & Deploy to EKS
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
env:
12+
ECR_REPOSITORY: demo-app
13+
EKS_CLUSTER_NAME: demo-dev
14+
AWS_REGION: eu-west-1
15+
HTTP_PORT: '8080'
16+
17+
jobs:
18+
build:
19+
name: Deployment
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- name: Check out code
24+
uses: actions/checkout@v3
25+
26+
- name: setup go
27+
uses: actions/setup-go@v5
28+
with:
29+
go-version: '1.21.0'
30+
31+
- name: test
32+
run: |
33+
go test -v -cover ./...
34+
35+
# - name: test in container
36+
# run: |
37+
# docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG --progress plain --no-cache --target run-test-stage .
38+
39+
# - name: Configure AWS credentials
40+
# uses: aws-actions/configure-aws-credentials@v4
41+
# with:
42+
# aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
43+
# aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
44+
# aws-region: ${{env.AWS_REGION}}
45+
46+
# - name: Login to Amazon ECR
47+
# id: login-ecr
48+
# uses: aws-actions/amazon-ecr-login@v2
49+
50+
# - name: Build, tag, and push image to Amazon ECR
51+
# env:
52+
# ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
53+
# IMAGE_TAG: ${{ github.run_number }}
54+
# run: |
55+
# docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f Dockerfile .
56+
# docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
57+
58+
# - name: Update kube config
59+
# run: aws eks update-kubeconfig --name $EKS_CLUSTER_NAME --region $AWS_REGION
60+
61+
# - name: Deploy to EKS to DEV
62+
# env:
63+
# ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
64+
# IMAGE_TAG: ${{ github.run_number }}
65+
# run: |
66+
# sed -i.bak "s|ENV|Dev|g" deploy/manifests/app-deploy.yaml
67+
# sed -i.bak "s|HTTPPORT|$HTTP_PORT|g" deploy/manifests/app-deploy.yaml
68+
# sed -i.bak "s|DOCKER_IMAGE|$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG|g" deploy/manifests/app-deploy.yaml && \
69+
# kubectl apply -f deploy/manifests/app-deploy.yaml
70+
# kubectl apply -f deploy/manifests/app-service.yaml
71+
# kubectl apply -f deploy/manifests/app-ingress.yaml
72+
73+
# - name: Call endpoint with curl
74+
# run: |
75+
# curl -X GET "https://example.com/hello"
76+
77+
# - name: Deploy to EKS to PROD
78+
# env:
79+
# ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
80+
# IMAGE_TAG: ${{ github.run_number }}
81+
# run: |
82+
# sed -i.bak "s|ENV|PROD|g" deploy/manifests/app-deploy.yaml
83+
# sed -i.bak "s|HTTPPORT|$HTTP_PORT|g" deploy/manifests/app-deploy.yaml
84+
# sed -i.bak "s|DOCKER_IMAGE|$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG|g" deploy/manifests/app-deploy.yaml && \
85+
# kubectl apply -f deploy/manifests/app-deploy.yaml
86+
# kubectl apply -f deploy/manifests/app-service.yaml
87+
# kubectl apply -f deploy/manifests/app-ingress.yaml

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.pdf
2+
deploy/
3+
validation/report.html
4+
.terraform
5+
.terraform.lock.hcl
6+
*.tfstate
7+
terraform.tfvars

Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM golang:1.21 as build
2+
3+
# Set destination for COPY
4+
WORKDIR /app
5+
6+
# Download Go modules
7+
COPY go.mod ./
8+
RUN go mod download
9+
10+
# Copy source files
11+
COPY *.go ./
12+
# Build
13+
RUN CGO_ENABLED=0 GOOS=linux go build -o /myapp
14+
15+
# Run the tests in the container
16+
FROM build AS run-test-stage
17+
RUN go test -v -cover ./...
18+
19+
# Final lean image
20+
FROM gcr.io/distroless/base-debian11
21+
COPY --from=build --chmod=764 /myapp /
22+
23+
EXPOSE 8080/tcp
24+
25+
CMD ["/myapp"]

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/DevOpsLK/demo
2+
3+
go 1.21.0

main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"os"
8+
)
9+
10+
func helloHandler(w http.ResponseWriter, r *http.Request) {
11+
fmt.Fprintln(w, "Hello! From DevOps Sri Lanka")
12+
}
13+
14+
func main() {
15+
port := os.Getenv("HTTP_PORT")
16+
if port == "" {
17+
port = "8080" // Default port if HTTP_PORT environment variable is not set
18+
}
19+
http.HandleFunc("/hello", helloHandler)
20+
fmt.Printf("Server is running on port %s\n", port)
21+
log.Fatal(http.ListenAndServe(":"+port, nil))
22+
}

main_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
)
8+
9+
func TestHelloHandler(t *testing.T) {
10+
// Create a request to the /hello endpoint (GET method)
11+
req, err := http.NewRequest("GET", "/hello", nil)
12+
if err != nil {
13+
t.Fatal(err)
14+
}
15+
16+
// Create a ResponseRecorder to record the response
17+
rr := httptest.NewRecorder()
18+
19+
// Call the helloHandler with the request and response recorder
20+
helloHandler(rr, req)
21+
22+
// Check the status code
23+
if status := rr.Code; status != http.StatusOK {
24+
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
25+
}
26+
27+
// Check the response body
28+
expected := "Hello! From Nitro\n"
29+
if body := rr.Body.String(); body != expected {
30+
t.Errorf("Handler returned unexpected body: got %v want %v", body, expected)
31+
}
32+
}

0 commit comments

Comments
 (0)