File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ name : Go WebApp CI
2+
3+ on :
4+ push :
5+ branches : [ main ]
6+ pull_request :
7+ branches : [ main ]
8+
9+ jobs :
10+ build-and-test :
11+ runs-on : ubuntu-latest
12+
13+ steps :
14+ - uses : actions/checkout@v4
15+
16+ - name : Set up Go
17+ uses : actions/setup-go@v5
18+ with :
19+ go-version : ' 1.22'
20+ cache : true
21+
22+ - name : Install dependencies
23+ run : go mod download
24+
25+ - name : Run tests
26+ run : go test -v ./...
27+
28+ - name : Build
29+ run : go build -v ./...
30+
31+ build-docker :
32+ needs : build-and-test
33+ runs-on : ubuntu-latest
34+
35+ steps :
36+ - uses : actions/checkout@v4
37+
38+ - name : Set up Docker Buildx
39+ uses : docker/setup-buildx-action@v3
40+
41+ - name : Login to DockerHub
42+ uses : docker/login-action@v3
43+ with :
44+ username : ${{ secrets.DOCKERHUB_USERNAME }}
45+ password : ${{ secrets.DOCKERHUB_TOKEN }}
46+
47+ - name : Build and push
48+ uses : docker/build-push-action@v5
49+ with :
50+ context : .
51+ push : true
52+ tags : |
53+ ${{ secrets.DOCKERHUB_USERNAME }}/go_webapp:latest
54+ ${{ secrets.DOCKERHUB_USERNAME }}/go_webapp:${{ github.sha }}
55+ cache-from : type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/go_webapp:buildcache
56+ cache-to : type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/go_webapp:buildcache,mode=max
Original file line number Diff line number Diff line change 1+ apiVersion : apps/v1
2+ kind : Deployment
3+ metadata :
4+ name : go-webapp
5+ labels :
6+ app : go-webapp
7+ spec :
8+ replicas : 2
9+ selector :
10+ matchLabels :
11+ app : go-webapp
12+ template :
13+ metadata :
14+ labels :
15+ app : go-webapp
16+ spec :
17+ containers :
18+ - name : go-webapp
19+ image : go_webapp:latest
20+ imagePullPolicy : IfNotPresent
21+ ports :
22+ - containerPort : 8080
23+ resources :
24+ requests :
25+ memory : " 64Mi"
26+ cpu : " 250m"
27+ limits :
28+ memory : " 128Mi"
29+ cpu : " 500m"
30+ livenessProbe :
31+ httpGet :
32+ path : /home
33+ port : 8080
34+ initialDelaySeconds : 5
35+ periodSeconds : 10
36+ readinessProbe :
37+ httpGet :
38+ path : /home
39+ port : 8080
40+ initialDelaySeconds : 5
41+ periodSeconds : 10
42+ ---
43+ apiVersion : v1
44+ kind : Service
45+ metadata :
46+ name : go-webapp-service
47+ spec :
48+ selector :
49+ app : go-webapp
50+ ports :
51+ - protocol : TCP
52+ port : 80
53+ targetPort : 8080
54+ type : LoadBalancer
Original file line number Diff line number Diff line change 1+ apiVersion : argoproj.io/v1alpha1
2+ kind : Application
3+ metadata :
4+ name : go-webapp
5+ namespace : argocd
6+ spec :
7+ project : default
8+ source :
9+ repoURL : https://github.com/Varnit01-dev/go_webapp.git
10+ targetRevision : HEAD
11+ path : k8s
12+ directory :
13+ recurse : true
14+ destination :
15+ server : https://kubernetes.default.svc
16+ namespace : default
17+ syncPolicy :
18+ automated :
19+ prune : true
20+ selfHeal : true
21+ syncOptions :
22+ - CreateNamespace=true
23+ health :
24+ status : Healthy
Original file line number Diff line number Diff line change 11FROM golang:1.22 as base
22
3- WORKDIR /the/workdir/path
3+ WORKDIR /app
44
5- COPY go.mod .
5+ # Copy go mod and sum files
6+ COPY go.mod ./
67
7- RUN go mod download
8+ # Download all dependencies
9+ RUN go mod download
810
11+ # Copy the source code
912COPY . .
1013
11- RUN go build -o main .
14+ # Build the application
15+ RUN CGO_ENABLED=0 GOOS=linux go build -o main .
1216
1317#FINAL STAGE -Distroless image
14- FROM gcr.io/Distroless
18+ FROM gcr.io/distroless/static-debian11
1519
16- COPY --from=base /app/main /
20+ WORKDIR /app
1721
18- COPY --from=base /app/static/ ./static
22+ # Copy the binary and static files
23+ COPY --from=base /app/main /app/main
24+ COPY --from=base /app/static /app/static
1925
2026EXPOSE 8080
2127
22- CMD [ "./main" ]
23-
28+ CMD ["/app/main"]
2429
2530
2631
Original file line number Diff line number Diff line change 1+ apiVersion : apps/v1
2+ kind : Deployment
3+ metadata :
4+ name : go-webapp
5+ labels :
6+ app : go-webapp
7+ spec :
8+ replicas : 2
9+ selector :
10+ matchLabels :
11+ app : go-webapp
12+ template :
13+ metadata :
14+ labels :
15+ app : go-webapp
16+ spec :
17+ containers :
18+ - name : go-webapp
19+ image : ${DOCKERHUB_USERNAME}/go_webapp:latest
20+ imagePullPolicy : Always
21+ ports :
22+ - containerPort : 8080
23+ resources :
24+ requests :
25+ memory : " 64Mi"
26+ cpu : " 250m"
27+ limits :
28+ memory : " 128Mi"
29+ cpu : " 500m"
30+ livenessProbe :
31+ httpGet :
32+ path : /home
33+ port : 8080
34+ initialDelaySeconds : 5
35+ periodSeconds : 10
36+ readinessProbe :
37+ httpGet :
38+ path : /home
39+ port : 8080
40+ initialDelaySeconds : 5
41+ periodSeconds : 10
42+ ---
43+ apiVersion : v1
44+ kind : Service
45+ metadata :
46+ name : go-webapp-service
47+ spec :
48+ selector :
49+ app : go-webapp
50+ ports :
51+ - protocol : TCP
52+ port : 80
53+ targetPort : 8080
54+ type : LoadBalancer
Original file line number Diff line number Diff line change 1+ apiVersion : v1
2+ kind : Secret
3+ metadata :
4+ name : dockerhub-secret
5+ namespace : default
6+ type : kubernetes.io/dockerconfigjson
7+ data :
8+ .dockerconfigjson : ${DOCKER_CONFIG_JSON}
9+ ---
10+ apiVersion : apps/v1
11+ kind : Deployment
12+ metadata :
13+ name : go-webapp
14+ labels :
15+ app : go-webapp
16+ spec :
17+ replicas : 2
18+ selector :
19+ matchLabels :
20+ app : go-webapp
21+ template :
22+ metadata :
23+ labels :
24+ app : go-webapp
25+ spec :
26+ imagePullSecrets :
27+ - name : dockerhub-secret
28+ containers :
29+ - name : go-webapp
30+ image : ${DOCKERHUB_USERNAME}/go_webapp:latest
31+ imagePullPolicy : Always
32+ ports :
33+ - containerPort : 8080
34+ resources :
35+ requests :
36+ memory : " 64Mi"
37+ cpu : " 250m"
38+ limits :
39+ memory : " 128Mi"
40+ cpu : " 500m"
41+ livenessProbe :
42+ httpGet :
43+ path : /home
44+ port : 8080
45+ initialDelaySeconds : 5
46+ periodSeconds : 10
47+ readinessProbe :
48+ httpGet :
49+ path : /home
50+ port : 8080
51+ initialDelaySeconds : 5
52+ periodSeconds : 10
Original file line number Diff line number Diff line change 1+ #! /bin/bash
2+
3+ # Check if Docker Hub credentials are provided
4+ if [ -z " $1 " ] || [ -z " $2 " ]; then
5+ echo " Usage: $0 <dockerhub-username> <dockerhub-password>"
6+ exit 1
7+ fi
8+
9+ # Create Docker config.json
10+ DOCKER_CONFIG_JSON=$( echo -n " {\" auths\" :{\" https://index.docker.io/v1/\" :{\" auth\" :\" $( echo -n " $1 :$2 " | base64) \" }}}" | base64)
11+
12+ # Create the secret
13+ cat << EOF | kubectl apply -f -
14+ apiVersion: v1
15+ kind: Secret
16+ metadata:
17+ name: dockerhub-secret
18+ namespace: default
19+ type: kubernetes.io/dockerconfigjson
20+ data:
21+ .dockerconfigjson: ${DOCKER_CONFIG_JSON}
22+ EOF
23+
24+ echo " Docker Hub secret created successfully!"
You can’t perform that action at this time.
0 commit comments