diff --git a/Dockerfile b/Dockerfile index 07a0b87..c05784e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,11 @@ -FROM amazoncorretto:11-al2023-jdk⁠ -WORKDIR /app -COPY target/maven-standalone-application*.jar maven-standalone-application.jar -CMD ["java","-jar","maven-standalone-application.jar"] +# ---------- Stage 1: Runtime only ------- +FROM eclipse-temurin:21-jre-jammy +# Set working directory inside the container +WORKDIR /app + +# The JAR will be specified dynamically via build args +ARG JAR_FILE +COPY ${JAR_FILE} app.jar + +EXPOSE 8080 +ENTRYPOINT ["java", "-jar", "app.jar"] diff --git a/Jenkinsfile b/Jenkinsfile index 8b13789..4e4e9e5 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1 +1,35 @@ - +pipeline { + agent any + stages { + stage('Build') { + agent { + docker { + image 'maven:amazoncorretto' + reuseNode true + } + } + steps { + sh ''' + echo "Cleaning maven cache and target folder..." + rm -rf target + mvn clean install + mvn package + mvn --version + echo "Hello Welcome to Jenkins" + ls -la + ''' + } + } + stage('Test') { + steps { + echo "Running tests..." + sh ''' + mvn test + # Ensure we're using bash + echo "Running tests..." + bash -c 'a=(1 3 4 5); echo "Array elements: ${a[@]: -2}"' + ''' + } + } + } +} diff --git a/Jenkinsfile1 b/Jenkinsfile1 new file mode 100644 index 0000000..e9914c3 --- /dev/null +++ b/Jenkinsfile1 @@ -0,0 +1,44 @@ +pipeline{ + agent any + tools{ + maven "MAVEN" + jdk "JDK17" + } + stages{ + stage("Fetch code from GitHub"){ + steps{ + echo "========fetching code from GitHub========" + git branch: 'master', url: 'https://github.com/Bashsha12/maven-standalone-application.git' + } + + } + stage('Unittest'){ + steps{ + echo "========Running unit tests========" + sh 'mvn clean test' + } + } + stage('Build'){ + steps{ + echo "========Building the project========" + sh ''' + mvn install -DskipTests=true + ''' + + echo "========Build completed========" + } + post{ + success{ + echo "========Build was successful========" + echo "========Archiving artifacts========" + archiveArtifacts artifacts: '**/target/*.jar' + echo "========Artifacts archived========" + } + failure{ + echo "========Build failed========" + } + } + +} + } +} \ No newline at end of file diff --git a/Jenkinsfile2 b/Jenkinsfile2 new file mode 100644 index 0000000..022ceb5 --- /dev/null +++ b/Jenkinsfile2 @@ -0,0 +1,92 @@ +pipeline { + agent any + tools { + maven "MAVEN" + jdk "JDK17" + } + stages { + stage("Fetch code from GitHub") { + steps { + echo "========fetching code from GitHub========" + git branch: 'master', url: 'https://github.com/Bashsha12/maven-standalone-application.git' + } + } + stage("Build") { + steps { + echo "========building the application========" + + sh 'mvn clean install -DskipTests=true' + sh 'mvn package' + sh 'mvn clean package' + } + post { + success { + echo "========Build was successful========" + echo "========Archiving artifacts========" + archiveArtifacts artifacts: '**/target/*.jar' + echo "========Artifacts archived========" + } + failure { + echo "========Build failed========" + } + } + } + stage("Unit Test") { + steps { + echo "========Running unit tests========" + sh 'mvn test' + } + } + stage("Checkstyle") { + steps { + echo "========Running checkstyle========" + sh 'mvn checkstyle:checkstyle' + } + } + stage("SonarQube") { + environment { + scannerHome = tool 'sonar7.1' + } + steps { + echo "========Running SonarQube analysis========" + withSonarQubeEnv('sonarsever') { + sh ''' + ${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=maven-standalone-application \ + -Dsonar.projectName=maven-standalone-application \ + -Dsonar.projectVersion=1.0 \ + -Dsonar.sources=src/ \ + -Dsonar.java.binaries=target/classes \ + -Dsonar.junit.reportPaths=target/surefire-reports/ \ + -Dsonar.jacoco.reportPaths=target/jacoco.exec \ + -Dsonar.java.checkstyle.reportPath=target/checkstyle-result.xml \ + ''' + } + } + } + stage("SonarQube Quality Gate") { + steps { + timeout(time: 1, unit: 'HOURS') { + waitForQualityGate abortPipeline: true + } + } + } + stage('Upload to Nexus') { + steps { + echo "========Uploading artifacts to Nexus========" + nexusArtifactUploader artifacts: [ + [artifactId: 'maven-standalone-application', + classifier: '', + file: 'target/maven-standalone-application-0.0.1-SNAPSHOT.jar', type: 'jar'] + ], + credentialsId: 'nexuslogin', + groupId: 'QA', + nexusUrl: '172.31.41.195:8081', + nexusVersion: 'nexus3', + protocol: 'http', + repository: 'maven-standalone-application-repo', + version: "${env.BUILD}-${env.BUILD_TIMESTAMP}" + echo "========Upload to Nexus completed========" + } + } + } +} \ No newline at end of file diff --git a/Jenkinsfile3 b/Jenkinsfile3 new file mode 100644 index 0000000..8e68a69 --- /dev/null +++ b/Jenkinsfile3 @@ -0,0 +1,138 @@ +def COLOR_MAP = [ + 'SUCCESS': 'good', + 'FAILURE': 'danger', + 'ABORTED': '#808080', + 'UNSTABLE': 'warning' +] +pipeline{ + agent { + label 'master' + } + tools { + maven 'Maven' + jdk 'Java21' + } + environment{ + sonarqube_server = 'Sonar' + DOCKERHUB_CRED= 'DOCKERHUB_CRED' + DOCKER_REGISTRY= "bashadockerhubdemo" + DOCKER_IMAGE_NAME= "maven-standalone-app" + AWSRegion= "ap-south-1" + ClusterName= "my-ecs-cluster" + ServiceName= "maven-standalone-service" + } + stages{ + stage("Checkout"){ + steps{ + git url: 'https://github.com/Bashsha12/maven-standalone-application.git' + } + } + stage("Build stage"){ + steps{ + sh 'mvn clean package -DskipTests=true' + stash includes: 'target/*.jar', name: 'app-jar' + } + } + stage("Testing stage"){ + parallel{ + stage("Unit Test"){ + steps{ + sh 'mvn test' + } + } + stage("Integration Test"){ + steps{ + sh 'mvn verify -DskipUnitTests=true' + } + } + stage("Checkstyle Report"){ + steps{ + sh 'mvn checkstyle:checkstyle' + } + } + } + } + stage("SonarQube Analysis"){ + steps{ + script { + def scannerHome = tool 'Sonar' + withSonarQubeEnv("${sonarqube_server}") { + sh """ + ${scannerHome}/bin/sonar-scanner \ + -Dsonar.projectKey=maven-standalone-application \ + -Dsonar.projectName=maven-standalone-application \ + -Dsonar.projectVersion=1.0 \ + -Dsonar.sources=src/ \ + -Dsonar.java.binaries=target/classes \ + -Dsonar.java.checkstyle.reportPaths=target/checkstyle-result.xml \ + -Dsonar.java.coveragePlugin=jacoco \ + -Dsonar.jacoco.reportPaths=target/jacoco.exec \ + -Dsonar.junit.reportPaths=target/surefire-reports/ + """ + } + } + } + + } + stage("Quality Gate"){ + steps{ + script { + timeout(time: 1, unit: 'MINUTES') { + def qg = waitForQualityGate() + if (qg.status != 'OK') { + error "Pipeline aborted due to quality gate failure: ${qg.status}" + } + } + } + } + } + /* + stage("Build and Push Docker Image"){ + steps{ + unstash 'app-jar' + script { + def imageTag = "${DOCKER_REGISTRY}/${DOCKER_IMAGE_NAME}:${env.BUILD_NUMBER}" + def jarFile = sh(script: "ls target/*.jar | head -n 1", returnStdout: true).trim() + withCredentials([usernamePassword(credentialsId: "${DOCKERHUB_CRED}", usernameVariable: 'DOCKERHUB_USER', passwordVariable: 'DOCKERHUB_PASS')]) { + sh "docker build --cache-from --build-arg JAR_FILE=${jarFile} -t ${imageTag} ${WORKSPACE}" + sh 'echo $DOCKERHUB_PASS | docker login -u $DOCKERHUB_USER --password-stdin' + sh "docker push ${imageTag}" + } + } + } + } + stage("Deploy to AWS ECS"){ + steps{ + script { + sh "aws ecs update-service --cluster ${ClusterName} --service ${ServiceName} --force-new-deployment --region ${AWSRegion}" + } + } + }*/ + } + post { + always { + script { + def result = currentBuild.currentResult + def emoji = "" + if (result == "SUCCESS") { + emoji = ":smile:" + } else if (result == "FAILURE") { + emoji = ":cry:" + } else if (result == "ABORTED") { + emoji = ":no_entry_sign:" + } else { + emoji = ":warning:" + } + + slackSend( + channel: '#devopscicd', + color: COLOR_MAP[result], + message: "${emoji} *${result}*: Job `${env.JOB_NAME}` build #${env.BUILD_NUMBER} \nCheck console: ${env.BUILD_URL}" + ) + } + } + } + + +} + diff --git a/test b/test new file mode 100644 index 0000000..998f829 --- /dev/null +++ b/test @@ -0,0 +1 @@ +sample \ No newline at end of file