diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 000000000..88f809358 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,94 @@ +# CICD using GitHub actions + +name: CI/CD + +# Exclude the workflow to run on changes to the helm chart +on: + push: + branches: + - main + paths-ignore: + - 'helm/**' + - 'k8/**' + - 'README.md' + +jobs: + + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Go 1.22 + uses: actions/setup-go@v2 + with: + go-version: 1.22 + + - name: Build + run: go build -o go-web-app + + - name: Test + run: go test ./... + + code-quality: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Run golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.56.2 + + push: + runs-on: ubuntu-latest + + needs: build + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and Push action + uses: docker/build-push-action@v6 + with: + context: . + file: ./Dockerfile + push: true + tags: ${{ secrets.DOCKERHUB_USERNAME }}/go-web-app:${{github.run_id}} + + update-newtag-in-helm-chart: + runs-on: ubuntu-latest + + needs: push + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + token: ${{ secrets.TOKEN }} + + - name: Update tag in Helm chart + run: | + sed -i 's/tag: .*/tag: "${{github.run_id}}"/' helm/go-web-app-chart/values.yaml + + - name: Commit and push changes + run: | + git config --global user.email "visala.k@algorims.com" + git config --global user.name "visala123" + git add helm/go-web-app-chart/values.yaml + git commit -m "Update tag in Helm chart" + git push diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..ab4b3e73c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,34 @@ +# Start with a base image +FROM golang:1.22 AS base + +# Set the working directory inside the container +WORKDIR /app + +# Copy the go.mod and go.sum files to the working directory +COPY go.mod ./ + +# Download all the dependencies +RUN go mod download + +# Copy the source code to the working directory +COPY . . + +# Build the application +RUN go build -o main . + +####################################################### +# Reduce the image size using multi-stage builds +# We will use a distroless image to run the application +FROM gcr.io/distroless/base + +# Copy the binary from the previous stage +COPY --from=base /app/main . + +# Copy the static files from the previous stage +COPY --from=base /app/static ./static + +# Expose the port on which the application will run +EXPOSE 8080 + +# Command to run the application +CMD ["./main"] diff --git a/helm/go-web-app-chart/.helmignore b/helm/go-web-app-chart/.helmignore new file mode 100644 index 000000000..0e8a0eb36 --- /dev/null +++ b/helm/go-web-app-chart/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/helm/go-web-app-chart/Chart.yaml b/helm/go-web-app-chart/Chart.yaml new file mode 100644 index 000000000..5d5e4a81b --- /dev/null +++ b/helm/go-web-app-chart/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: go-web-app-chart +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" diff --git a/helm/go-web-app-chart/templates/deployment.yaml b/helm/go-web-app-chart/templates/deployment.yaml new file mode 100644 index 000000000..fbb129b2b --- /dev/null +++ b/helm/go-web-app-chart/templates/deployment.yaml @@ -0,0 +1,22 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: go-web-app + labels: + app: go-web-app +spec: + replicas: 1 + selector: + matchLabels: + app: go-web-app + template: + metadata: + labels: + app: go-web-app + spec: + containers: + - name: go-web-app + image: visala123/go-web-app:{{ .Values.image.tag }} + ports: + - containerPort: 8080 + diff --git a/helm/go-web-app-chart/templates/ingress.yaml b/helm/go-web-app-chart/templates/ingress.yaml new file mode 100644 index 000000000..39122ef40 --- /dev/null +++ b/helm/go-web-app-chart/templates/ingress.yaml @@ -0,0 +1,20 @@ +# Ingress resource for the application +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: go-web-app + annotations: + nginx.ingress.kubernetes.io/rewrite-target: / +spec: + ingressClassName: nginx + rules: + - host: go-web-app.local + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: go-web-app + port: + number: 80 diff --git a/helm/go-web-app-chart/templates/service.yaml b/helm/go-web-app-chart/templates/service.yaml new file mode 100644 index 000000000..19553d819 --- /dev/null +++ b/helm/go-web-app-chart/templates/service.yaml @@ -0,0 +1,15 @@ +# Service for the application +apiVersion: v1 +kind: Service +metadata: + name: go-web-app + labels: + app: go-web-app +spec: + ports: + - port: 80 + targetPort: 8080 + protocol: TCP + selector: + app: go-web-app + type: ClusterIP diff --git a/helm/go-web-app-chart/values.yaml b/helm/go-web-app-chart/values.yaml new file mode 100644 index 000000000..def143bda --- /dev/null +++ b/helm/go-web-app-chart/values.yaml @@ -0,0 +1,22 @@ +# Default values for go-web-app-chart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 1 + +image: + repository: visala123/go-web-app + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "v1" + +ingress: + enabled: false + className: "" + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: + - path: / + pathType: ImplementationSpecific \ No newline at end of file diff --git a/k8/manifests/deployment.yaml b/k8/manifests/deployment.yaml new file mode 100644 index 000000000..372305d6e --- /dev/null +++ b/k8/manifests/deployment.yaml @@ -0,0 +1,22 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: go-web-app + labels: + app: go-web-app +spec: + replicas: 1 + selector: + matchLabels: + app: go-web-app + template: + metadata: + labels: + app: go-web-app + spec: + containers: + - name: go-web-app + image: visala123/go-web-app:v1 + ports: + - containerPort: 8080 + diff --git a/k8/manifests/ingress.yaml b/k8/manifests/ingress.yaml new file mode 100644 index 000000000..39122ef40 --- /dev/null +++ b/k8/manifests/ingress.yaml @@ -0,0 +1,20 @@ +# Ingress resource for the application +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: go-web-app + annotations: + nginx.ingress.kubernetes.io/rewrite-target: / +spec: + ingressClassName: nginx + rules: + - host: go-web-app.local + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: go-web-app + port: + number: 80 diff --git a/k8/manifests/service.yaml b/k8/manifests/service.yaml new file mode 100644 index 000000000..a66cada18 --- /dev/null +++ b/k8/manifests/service.yaml @@ -0,0 +1,16 @@ +# Service for the application +apiVersion: v1 +kind: Service +metadata: + name: go-web-app + labels: + app: go-web-app +spec: + ports: + - port: 80 + targetPort: 8080 + protocol: TCP + nodePort: 32074 + selector: + app: go-web-app + type: NodePort diff --git a/main b/main new file mode 100644 index 000000000..7de8afe22 Binary files /dev/null and b/main differ