├── Dockerfile ├── README.md ├── deployment.yaml ├── main.go ├── main_test.go └── service.yaml /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.16.4-buster AS builder 2 | 3 | ARG VERSION=dev 4 | 5 | WORKDIR /go/src/app 6 | COPY main.go . 7 | RUN go build -o main -ldflags=-X=main.version=${VERSION} main.go 8 | 9 | FROM debian:buster-slim 10 | COPY --from=builder /go/src/app/main /go/bin/main 11 | ENV PATH="/go/bin:${PATH}" 12 | CMD ["main"] 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Golang-CICD-Tutorial! 2 | This is a simple CI/CD tutorial using [Harness](https://www.harness.io/). 3 | -------------------------------------------------------------------------------- /deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: go-app-deployment 5 | spec: 6 | replicas: 1 7 | selector: 8 | matchLabels: 9 | app: go-app 10 | template: 11 | metadata: 12 | labels: 13 | app: go-app 14 | spec: 15 | containers: 16 | - name: go-app 17 | image: pavansa/golang-hello-world:latest 18 | ports: 19 | - containerPort: 8080 20 | env: 21 | - name: PORT 22 | value: "8080" 23 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net/http" 7 | ) 8 | 9 | func homePage(w http.ResponseWriter, r *http.Request) { 10 | fmt.Fprintf(w, "Home Page") 11 | } 12 | 13 | func wsEndpoint(w http.ResponseWriter, r *http.Request) { 14 | fmt.Fprintf(w, "Hello World") 15 | } 16 | 17 | func setupRoutes() { 18 | http.HandleFunc("/", homePage) 19 | http.HandleFunc("/ws", wsEndpoint) 20 | } 21 | 22 | func main() { 23 | fmt.Println("Hello World") 24 | setupRoutes() 25 | log.Fatal(http.ListenAndServe(":8080", nil)) 26 | } -------------------------------------------------------------------------------- /main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | "net/http/httptest" 6 | "testing" 7 | ) 8 | 9 | func TestHomePage(t *testing.T) { 10 | req, err := http.NewRequest("GET", "/", nil) 11 | if err != nil { 12 | t.Fatal(err) 13 | } 14 | rr := httptest.NewRecorder() 15 | handler := http.HandlerFunc(homePage) 16 | handler.ServeHTTP(rr, req) 17 | if status := rr.Code; status != http.StatusOK { 18 | t.Errorf("handler returned wrong status code: got %v want %v", 19 | status, http.StatusOK) 20 | } 21 | if rr.Body.String() != "Home Page" { 22 | t.Errorf("handler returned unexpected body: got %v want %v", 23 | rr.Body.String(), "Home Page") 24 | } 25 | } 26 | 27 | func TestWsEndpoint(t *testing.T) { 28 | req, err := http.NewRequest("GET", "/ws", nil) 29 | if err != nil { 30 | t.Fatal(err) 31 | } 32 | rr := httptest.NewRecorder() 33 | handler := http.HandlerFunc(wsEndpoint) 34 | handler.ServeHTTP(rr, req) 35 | if status := rr.Code; status != http.StatusOK { 36 | t.Errorf("handler returned wrong status code: got %v want %v", 37 | status, http.StatusOK) 38 | } 39 | if rr.Body.String() != "Hello World" { 40 | t.Errorf("handler returned unexpected body: got %v want %v", 41 | rr.Body.String(), "Hello World") 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /service.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: go-app-service 5 | spec: 6 | selector: 7 | app: go-app 8 | ports: 9 | - name: http 10 | protocol: TCP 11 | port: 80 12 | targetPort: 8080 13 | type: LoadBalancer 14 | --------------------------------------------------------------------------------