diff --git a/.helmignore b/.helmignore new file mode 100644 index 000000000..0e8a0eb36 --- /dev/null +++ b/.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/Chart.yaml b/Chart.yaml new file mode 100644 index 000000000..93778c104 --- /dev/null +++ b/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: hello-world-war +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/Dockerfile b/Dockerfile new file mode 100644 index 000000000..ec3d4c45c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM maven:3.3-jdk-8 as mavenbuilder +ARG DIR=/home/devops +WORKDIR $DIR +COPY . . +RUN /usr/bin/mvn clean install +RUN ls target/ +RUN which mvn +FROM tomcat:9.0 +ARG DIR=/home/devops/target +COPY --from=mavenbuilder ${DIR}/hello-world-war-1.0.0.war /usr/local/tomcat/webapps/ diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 000000000..d3b147a06 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,49 @@ +pipeline { + agent any + + stages { + stage('checkout') { + steps { + sh 'rm -rf hello-world-war' + sh 'git clone -b k8s https://github.com/tarundanda147/hello-world-war.git' + } + } + + stage('build') { + steps { + dir("hello-world-war") { + sh 'echo "inside build"' + sh "docker build -t tomcat-war:${BUILD_NUMBER} ." + } + } + } + + stage('push') { + steps { + withCredentials([usernamePassword(credentialsId: '773e6289-72b6-476b-9e54-19702f9fb5d3', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) { + sh "docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}" + sh "docker tag tomcat-war:${BUILD_NUMBER} tarundanda147/tomcat:${BUILD_NUMBER}" + sh "docker push tarundanda147/tomcat:${BUILD_NUMBER}" + } + } + } + + stage('Helm Deploy') { + steps { + // Authenticate with AWS using IAM credentials stored in Jenkins + withCredentials([[ + $class: 'AmazonWebServicesCredentialsBinding', + credentialsId: '03bb86f5-d824-42dd-b9c7-da3dc566f56c', + accessKeyVariable: 'AWS_ACCESS_KEY_ID', + secretKeyVariable: 'AWS_SECRET_ACCESS_KEY' + ]]) { + sh "aws eks --region us-east-1 update-kubeconfig --name eks-cluster" + echo 'Deploying to Kubernetes using Helm' + // Deploy Helm chart to Kubernetes cluster + sh "helm upgrade first /var/lib/jenkins/workspace/eks-docker/hello-world-war --namespace hello-world-war --set image.tag=$BUILD_NUMBER --dry-run" + sh "helm upgrade first /var/lib/jenkins/workspace/eks-docker/hello-world-war --namespace hello-world-war --set image.tag=$BUILD_NUMBER" + } + } + } + } +} diff --git a/artifactory b/artifactory new file mode 100644 index 000000000..9024f0ad4 --- /dev/null +++ b/artifactory @@ -0,0 +1,60 @@ +pipeline { + agent { label 'java' } + stages { + stage('checkout') { + steps { + sh 'rm -rf bus_booking' + sh 'git clone https://github.com/tarundanda147/bus_booking.git' + } + } + + stage('build') { + steps { + script { + sh "mvn clean install" + } + } + } + stage('Deploy to JFrog Artifactory') { + steps { + // Remember this is the step which I followed for free style project. + script { + rtServer( + id: "Artifact", + url: "http://13.238.154.172:8081/artifactory", + username: "jenkins", + password: "passwd" + ) + } + } + } + + stage('Upload') { + steps { + script { + // For my undertanding rtUpload is a part of jFrog Artifactory plugin to upload artifacts to artifacts repo + rtUpload ( + serverId: 'Artifact', + spec: '''{ + "files": [ + { + "pattern": "target/*.jar", + "target": "libs-release-local/" + } + ] + }''' + ) + } + } + } + + stage('Publish build info') { + steps { + script { + // For my understanding to publish build info + rtPublishBuildInfo serverId: "Artifact" + } + } + } + } +} diff --git a/templates/deployment.yml b/templates/deployment.yml new file mode 100644 index 000000000..1feefacf4 --- /dev/null +++ b/templates/deployment.yml @@ -0,0 +1,38 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: myreplica + labels: + app: {{ .Values.deploymentLabel }} +spec: + replicas: {{ .Values.replicas }} + strategy: + type: RollingUpdate + rollingUpdate: + maxSurge: 1 + maxUnavailable: 0 + selector: + matchLabels: + app: {{ .Values.deploymentLabel }} + template: + metadata: + name: {{ .Values.deploymentLabel }} + labels: + app: {{ .Values.deploymentLabel }} + spec: + containers: + - name: {{ .Values.deploymentLabel }} + image: {{ .Values.imageName }}:{{ .Values.image.tag }} + imagePullSecrets: + - name: regcred + env: + - name: TOMCAT_OPTS + value: --prefix=/tomcat + volumeMounts: + - name: {{ .Values.volumeName }} + mountPath: /usr/local/tomcat/webapps + volumes: + - name: {{ .Values.volumeName }} + persistentVolumeClaim: + claimName: {{ .Values.claimName }} + diff --git a/templates/ingress-route.yml b/templates/ingress-route.yml new file mode 100644 index 000000000..322f229b2 --- /dev/null +++ b/templates/ingress-route.yml @@ -0,0 +1,16 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: {{ .Values.ingressName }} +spec: + ingressClassName: nginx + rules: + - http: + paths: + - path: /hello-world-war-1.0.0 + pathType: Prefix + backend: + service: + name: {{ .Values.serviceName }} + port: + number: 8090 diff --git a/templates/pvc.yml b/templates/pvc.yml new file mode 100644 index 000000000..bd311b320 --- /dev/null +++ b/templates/pvc.yml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: {{ .Values.claimName }} +spec: + accessModes: + - ReadWriteOnce + storageClassName: {{ .Values.storageClass }} + resources: + requests: + storage: {{ .Values.requestStorage }} \ No newline at end of file diff --git a/templates/secrets.yml b/templates/secrets.yml new file mode 100644 index 000000000..1ef0c16cb --- /dev/null +++ b/templates/secrets.yml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: Secret +metadata: + name: regcred +type: kubernetes.io/dockerconfigjson +data: + .dockerconfigjson: |- + ewogICAgImF1dGhzIjogewogICAgICAgICJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOiB7CiAgICAgICAgICAgICJ1c2VybmFtZSI6ICJ0YXJ1bmRhbmRhMTQ3IiwKICAgICAgICAgICAgInBhc3N3b3JkIjogImRja3JfcGF0XzEtMk9nVFczNFlIMnFDb00zWnFvSTBKU2lJRSIsCiAgICAgICAgICAgICJlbWFpbCI6ICJob25leWNvY28xNDdAZ21haWwuY29tIgogICAgICAgIH0KICAgIH0KfQoK diff --git a/templates/service.yml b/templates/service.yml new file mode 100644 index 000000000..1b9ba0478 --- /dev/null +++ b/templates/service.yml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Values.serviceName }} +spec: + type: ClusterIP + selector: + app: {{ .Values.deploymentLabel }} + ports: + - name: podport + protocol: TCP + port: 8090 + targetPort: 8080 \ No newline at end of file diff --git a/templates/storageclass.yml b/templates/storageclass.yml new file mode 100644 index 000000000..bbecc5dc1 --- /dev/null +++ b/templates/storageclass.yml @@ -0,0 +1,6 @@ +apiVersion: storage.k8s.io/v1 +kind: StorageClass +metadata: + name: {{ .Values.storageClass }} +provisioner: ebs.csi.aws.com +volumeBindingMode: WaitForFirstConsumer \ No newline at end of file diff --git a/values.yaml b/values.yaml new file mode 100644 index 000000000..9014fe830 --- /dev/null +++ b/values.yaml @@ -0,0 +1,11 @@ +deploymentName: "my-tomcat" +deploymentLabel: "tomcat" +replicas: 1 +volumeName: "volume-tomcat" +claimName: "claim-tomcat" +storageClass: "tomcat-ebs" +requestStorage: "5Gi" +serviceName: "tomcat" +ingressName: "tomcat-ingress" +imageName: "tarundanda147/tomcat" +imageTag: "lts"