-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
deploy/ | ||
validation/report.html | ||
.terraform | ||
.terraform.lock.hcl | ||
*.tfstate | ||
terraform.tfvars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/DevOpsLK/demo | ||
|
||
go 1.21.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |