diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..9f57a2fb2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +FROM golang:1.22.5 as base + +WORKDIR /app + +COPY go.mod ./ + +RUN go mod download + +COPY . . + +RUN go build -o main . + + +FROM gcr.io/distroless/base + +COPY --from=base /app/main . + +COPY --from=base /app/static ./static + +EXPOSE 8080 + +CMD [ "./main" ] \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 000000000..43d4efda7 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,105 @@ +pipeline { + agent any + + tools { + go 'go-1.22.5' + } + + environment { + SCANNER_HOME = tool 'sonar-scanner' + SONAR_TOKEN = credentials('sonar-cred') + GITHUB_TOKEN = credentials('git-cred') // GitHub token + DOCKER_CRED = credentials('docker-cred') // Docker credentials + } + + stages { + stage('Checkout Code') { + steps { + script { + // Checkout the repository + checkout scm + + // Set the remote URL to HTTPS and use the GitHub token + sh """ + git remote set-url origin https://vinnu2251:${GITHUB_TOKEN}@github.com/vinnu2251/go-web-app.git + git config --global user.email "vinaychowdarychitturi@gmail.com" + git config --global user.name "vinay chitturi" + """ + } + } + } + + stage('Build') { + steps { + sh "go build -o go-web-app" + } + } + + stage('Unit Test') { + steps { + sh "go test ./..." + } + } + + stage('Run SonarQube Analysis') { + steps { + withSonarQubeEnv('sonar') { + sh "${SCANNER_HOME}/bin/sonar-scanner -Dsonar.projectKey=gowebapp -Dsonar.projectName=gowebapp" + } + } + } + + stage('Docker Build & Tag') { + steps { + script { + withDockerRegistry(credentialsId: 'docker-cred', toolName: 'docker') { + sh 'docker build -t vinay7944/go-web-app:v1 .' + } + } + } + } + + stage('Docker Push Image') { + steps { + script { + withDockerRegistry(credentialsId: 'docker-cred', toolName: 'docker') { + sh 'docker push vinay7944/go-web-app:v1' + } + } + } + } + + stage('Update Helm Chart Tag') { + steps { + script { + // Get the commit ID + def commitId = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() + echo "Current commit ID: ${commitId}" + + // Get the current branch name + def branchName = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim() + echo "Current branch: ${branchName}" + + // Update the Helm chart with the new tag + sh """ + sed -i 's/tag: .*/tag: "${commitId}"/' helm/go-web-app-chart/values.yaml + """ + + // Add, commit, and push the changes using the GitHub token + sh """ + git add helm/go-web-app-chart/values.yaml + git commit -m "Update tag in Helm chart with commit ID ${commitId}" + git push https://vinnu2251:${GITHUB_TOKEN}@github.com/vinnu2251/go-web-app.git HEAD:refs/heads/${branchName} + """ + } + } + } + } + + post { + always { + echo "Pipeline complete" + cleanWs() // Clean workspace after the build + } + } +} 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..c1967ce73 --- /dev/null +++ b/helm/go-web-app-chart/templates/deployment.yaml @@ -0,0 +1,21 @@ +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: vinay7944/go-web-app:v1 + 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..567a904a1 --- /dev/null +++ b/helm/go-web-app-chart/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: go-web-app + labels: + app: go-web-app +spec: + selector: + app: go-web-app + type: ClusterIP + ports: + - port: 80 + targetPort: 8080 + protocol: TCP + diff --git a/helm/go-web-app-chart/values.yaml b/helm/go-web-app-chart/values.yaml new file mode 100644 index 000000000..6f079f3d4 --- /dev/null +++ b/helm/go-web-app-chart/values.yaml @@ -0,0 +1,23 @@ +# 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: vinay7944/go-web-app + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "820de59" + +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..955174f4e --- /dev/null +++ b/k8/manifests/deployment.yaml @@ -0,0 +1,21 @@ +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: vinay7944/go-web-app:{{ .Values.image.tag }} + 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..567a904a1 --- /dev/null +++ b/k8/manifests/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: go-web-app + labels: + app: go-web-app +spec: + selector: + app: go-web-app + type: ClusterIP + ports: + - port: 80 + targetPort: 8080 + protocol: TCP + diff --git a/main.go b/main.go index b04eee299..4d7c60351 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ func homePage(w http.ResponseWriter, r *http.Request) { } func coursePage(w http.ResponseWriter, r *http.Request) { - // Render the course html page + // Render the course html page of the chaneg by vinay http.ServeFile(w, r, "static/courses.html") }