From a86e3a6ca361fcc3229bfc3e73a135a0d95a6bdb Mon Sep 17 00:00:00 2001 From: "venkeyboda07@gmail.com" Date: Thu, 6 Feb 2025 12:04:29 +0530 Subject: [PATCH] added pipeline and dockerfile --- Dockerfile | 14 ++++++++ azure-pipelines.yml | 40 +++++++++++++++++++++ setup.md | 88 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 Dockerfile create mode 100644 azure-pipelines.yml create mode 100644 setup.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..0d75a25af --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM maven:3.9-eclipse-temurin-17 AS builder +COPY . /spc +WORKDIR /spc +RUN mvn package + +FROM eclipse-temurin:17-alpine +LABEL org="spc build" author="venkat" +ARG USERNAME=spc +RUN apk add --no-cache bash +RUN adduser -D -h /apps -s /bin/bash/ ${USERNAME} +USER ${USERNAME} +COPY --from=builder --chown={USERNAME}:{USERNAME} /spc/target/spring-petclinic-3.4.0-SNAPSHOT.jar /apps/spring-petclinic-3.4.0-SNAPSHOT.jar +WORKDIR /apps +EXPOSE 8080 \ No newline at end of file diff --git a/azure-pipelines.yml b/azure-pipelines.yml new file mode 100644 index 000000000..b0003c03a --- /dev/null +++ b/azure-pipelines.yml @@ -0,0 +1,40 @@ +pool: Default + +pr: + - develop + +stages: + - stage: CI + jobs: + - job: Build + displayName: 'Building and Testing' + steps: + +steps: + - task: Maven@4 + displayName: 'build and publish the maven code' + inputs: + mavenPomFile: pom.xml + goals: 'clean package' + testResultsFiles: '**/surefire-reports/TEST-*.xml' + publishJUnitResults: true + + - task: CopyFiles@2 + inputs: + contents: '**/*.jar' + targetFolder: '$(Build.ArtifactStagingDirectory)' + + - task: PublishBuildArtifacts@1 + inputs: + pathToPublish: '$(Build.ArtifactStagingDirectory)' + artifactName: drop + + - task: Docker@2 + displayName: 'Build docker image' + inputs: + command: 'buildAndPush' + Dockerfile: './Dockerfile' + repository: venkeyboda/spring-petclinic + tags: '$(Build.BuildId)' + + \ No newline at end of file diff --git a/setup.md b/setup.md new file mode 100644 index 000000000..4e1fd18aa --- /dev/null +++ b/setup.md @@ -0,0 +1,88 @@ +# Steps + +### DEVELOP BRANCH PULL BASED +----------------------------- +* Java-based Spring Petclinic application +* Pull request-based development +* Create a multi-stage Dockerfile for the Spring Petclinic application on the `develop` branch +* Execute Dockerfile Using Command +```bash +# Create image command +docker image build -t spc:1.0 . +docker image build -t (dockerfile name) +# Check if the image was created +docker image ls +# Check if your image is running in a container (for verification purposes) +docker container run -P -d --name mytest spc:1.0 +docker container run -P -d --name +# Check if the container is running (to verify container status) +docker container ls +``` +* Image Scanning with Trivy +* Now, to scan your image with Trivy, follow these steps: +* First, install Trivy on your VM or EC2 instance. +```bash +sudo apt-get install wget apt-transport-https gnupg lsb-release +wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | sudo tee /usr/share/keyrings/trivy.gpg > /dev/null +echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee -a /etc/apt/sources.list.d/trivy.list +sudo apt-get update +sudo apt-get install trivy -y +``` +### After Trivy is installed, scan your image using the following command: +```bash +trivy image spc:1.0 +trivy image +``` +############################################################################################################################################################################# + +### RELEASE BRANCH PUSH BASED +----------------------------- +* Push Based `Release` +* Multiple developers have made changes, so we will scan the Docker image again. +* On the release branch, scan the Docker image with Trivy. +### After Trivy is installed, use the following command: +```bash +trivy image spc:1.0 +trivy image +``` +* The output will show vulnerabilities or reports. +* Pushing the Image to a Registry +* Once the image scan is completed, we can push the image to a registry such as Docker Hub, ECR, or ACR. +### To push to Docker Hub, follow these steps: +```bash +docker login +docker image tag /: +docker image tag spc:1.0 longflew/javaimagecicd:1.0 +docker image push longflew/javaimagecicd:1.0 +``` +##### Note: Ensure k8s Cluster(AKS or EKS) will be created +### Installing helm +* [Refer Here](https://helm.sh/docs/intro/install/) for installing Helm +* Once installation completed run the following commands +* To Create a new helm chart repo +`helm create ` +`helm create spc-chart` +* Deploy the application in k8s using helm , the command will be + `helm install ` + `helm install spc-release spc-chart` +* Check the appliocation running or not using the k8s commands +```bash +kubectl get po +kubectl get svc +``` +* Take the IP or DNS of the service and open new Browser and paste the IP or DNS. + +### Upgrade docker image in helm +* Whenever we upgrade the Docker image in Helm, the deployment manifest file is also automatically updated with the new image +* After making further changes, we updated the values.yaml file to modify the image tag. +* use the following command: +```bash +# docker image upgrade +helm upgrade spc-release spc-chart -f values.yaml --set image.tag=new-image-tag + +# Verify the Upgrade +helm get all +helm get all spc-release +``` + +