diff --git a/.github/workflows/ci-cd.yaml b/.github/workflows/ci-cd.yaml new file mode 100644 index 000000000..f104f08be --- /dev/null +++ b/.github/workflows/ci-cd.yaml @@ -0,0 +1,94 @@ +name: Spring PetClinic CI/CD + +on: + pull_request: + branches: [main] + push: + branches: [main] + +env: + IMAGE_NAME: spring-petclinic + +jobs: + checkstyle: + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + name: Checkstyle + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-java@v3 + with: + distribution: temurin + java-version: 17 + - name: Checkstyle + run: ./gradlew checkstyleMain checkstyleTest + - name: Upload Checkstyle Report + uses: actions/upload-artifact@v4 + with: + name: checkstyle-report + path: build/reports/checkstyle/ + + test: + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + needs: checkstyle + name: Run Tests + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-java@v3 + with: + distribution: temurin + java-version: 17 + - run: ./gradlew test + + build: + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + needs: test + name: Build App + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-java@v3 + with: + distribution: temurin + java-version: 17 + - run: ./gradlew build -x test + + docker-pr: + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + needs: build + name: Build & Push Docker Image (MR) + steps: + - uses: actions/checkout@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + - name: Build & Push + run: | + SHORT_SHA=$(echo $GITHUB_SHA | cut -c1-7) + docker build -t ${{ secrets.DOCKER_USERNAME }}/mr:${SHORT_SHA} . + docker push ${{ secrets.DOCKER_USERNAME }}/mr:${SHORT_SHA} + + docker-main: + if: github.ref == 'refs/heads/main' && github.event_name == 'push' + runs-on: ubuntu-latest + name: Build & Push Docker Image (Main) + steps: + - uses: actions/checkout@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + - name: Build & Push + run: | + docker build -t ${{ secrets.DOCKER_USERNAME }}/main:latest . + docker push ${{ secrets.DOCKER_USERNAME }}/main:latest + diff --git a/.github/workflows/deploy-and-test-cluster.yml b/.github/workflows/deploy-and-test-cluster.yml deleted file mode 100644 index 7353a604b..000000000 --- a/.github/workflows/deploy-and-test-cluster.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: Deploy and Test Cluster - -on: - push: - branches: [main] - paths: - - 'k8s/**' - pull_request: - branches: [main] - paths: - - 'k8s/**' - -jobs: - deploy-and-test-cluster: - runs-on: ubuntu-latest - steps: - - name: Check out the repository - uses: actions/checkout@v2 - - - name: Create k8s Kind Cluster - uses: helm/kind-action@v1 - - - name: Deploy application - run: | - kubectl apply -f k8s/ - - - name: Wait for Pods to be ready - run: | - kubectl wait --for=condition=ready pod -l app=demo-db --timeout=180s - kubectl wait --for=condition=ready pod -l app=petclinic --timeout=180s - diff --git a/.github/workflows/gradle-build.yml b/.github/workflows/gradle-build.yml deleted file mode 100644 index c24c121b1..000000000 --- a/.github/workflows/gradle-build.yml +++ /dev/null @@ -1,31 +0,0 @@ -# This workflow will build a Java project with Gradle, and cache/restore any dependencies to improve the workflow execution time -# For more information see: https://docs.github.com/en/actions/use-cases-and-examples/building-and-testing/building-and-testing-java-with-gradle - -name: Java CI with Gradle - -on: - push: - branches: [ main ] - pull_request: - branches: [ main ] - -jobs: - build: - - runs-on: ubuntu-latest - strategy: - matrix: - java: [ '17' ] - - steps: - - uses: actions/checkout@v4 - - name: Set up JDK ${{matrix.java}} - uses: actions/setup-java@v4 - with: - java-version: ${{matrix.java}} - distribution: 'adopt' - cache: maven - - name: Setup Gradle - uses: gradle/actions/setup-gradle@v4 - - name: Build with Gradle - run: ./gradlew build diff --git a/.github/workflows/maven-build.yml b/.github/workflows/maven-build.yml deleted file mode 100644 index a1ec4dab7..000000000 --- a/.github/workflows/maven-build.yml +++ /dev/null @@ -1,29 +0,0 @@ -# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time -# For more information see: https://docs.github.com/en/actions/use-cases-and-examples/building-and-testing/building-and-testing-java-with-maven - -name: Java CI with Maven - -on: - push: - branches: [ main ] - pull_request: - branches: [ main ] - -jobs: - build: - - runs-on: ubuntu-latest - strategy: - matrix: - java: [ '17' ] - - steps: - - uses: actions/checkout@v4 - - name: Set up JDK ${{matrix.java}} - uses: actions/setup-java@v4 - with: - java-version: ${{matrix.java}} - distribution: 'adopt' - cache: maven - - name: Build with Maven Wrapper - run: ./mvnw -B verify diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..70e5b0097 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM eclipse-temurin:17-jdk as builder +WORKDIR /app +COPY . . +RUN ./gradlew build -x test + +FROM eclipse-temurin:17-jre +WORKDIR /app +COPY --from=builder /app/build/libs/*.jar app.jar +EXPOSE 8080 +ENTRYPOINT ["java", "-jar", "app.jar"] +` diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 000000000..d34dbf2fa --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,72 @@ +pipeline { + agent none + + environment { + DOCKERHUB_USER = "prankumar313" + } + + stages { + stage('Checkstyle') { + agent { + docker { + image 'gradle:8.1.1-jdk17' + } + } + when { + expression { env.BRANCH_NAME != 'main' } + } + steps { + sh './gradlew checkstyleMain checkstyleTest' + archiveArtifacts artifacts: '**/build/reports/checkstyle/*.xml', allowEmptyArchive: true + } + } + + stage('Test') { + agent { + docker { + image 'gradle:8.1.1-jdk17' + } + } + when { + expression { env.BRANCH_NAME != 'main' } + } + steps { + sh './gradlew test' + } + } + + stage('Build (No Tests)') { + agent { + docker { + image 'gradle:8.1.1-jdk17' + } + } + when { + expression { env.BRANCH_NAME != 'main' } + } + steps { + sh './gradlew build -x test' + } + } + + stage('Build & Push Docker Image') { + agent { label 'docker-enabled' } // runs on a node with Docker installed + steps { + script { + def commit = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() + def repo = env.BRANCH_NAME == 'main' ? 'main' : 'mr' + def image = "${DOCKERHUB_USER}/${repo}:${commit}" + + sh "docker build -t ${image} ." + + withCredentials([usernamePassword(credentialsId: 'dockerhub-creds', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) { + sh """ + echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin + docker push ${image} + """ + } + } + } + } + } +} diff --git a/build.gradle b/build.gradle index a80a6e710..ddde947b5 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,17 @@ plugins { id 'java' - id 'org.springframework.boot' version '3.5.0' - id 'io.spring.dependency-management' version '1.1.7' - id 'org.graalvm.buildtools.native' version '0.10.6' - id 'org.cyclonedx.bom' version '2.3.1' - id 'io.spring.javaformat' version '0.0.46' + id 'checkstyle' + id 'org.springframework.boot' version '3.4.2' + id 'io.spring.dependency-management' version '1.1.6' + id 'org.graalvm.buildtools.native' version '0.10.3' + id 'org.cyclonedx.bom' version '1.10.0' + id 'io.spring.javaformat' version '0.0.43' id "io.spring.nohttp" version "0.0.11" + id 'pl.allegro.tech.build.axion-release' version '1.18.18' } +version=scmVersion.version + apply plugin: 'java' apply plugin: 'checkstyle' apply plugin: 'io.spring.javaformat' @@ -21,6 +25,14 @@ java { sourceCompatibility = JavaVersion.VERSION_17 } +checkstyle{ + toolVersion = '10.12.3' + configFile = file("${rootDir}/config/checkstyle/checkstyle.xml") + configProperties = [ + 'checkstyle.cache.file': "${buildDir}/checkstyle/cachefile" + ] +} + repositories { mavenCentral() } @@ -85,3 +97,4 @@ checkFormatAotTest.enabled = false formatAot.enabled = false formatAotTest.enabled = false + diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml new file mode 100644 index 000000000..e46f356d8 --- /dev/null +++ b/config/checkstyle/checkstyle.xml @@ -0,0 +1,448 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/settings.gradle b/settings.gradle index e60ee14fa..dc753dc81 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,2 @@ -rootProject.name = 'spring-petclinic' +rootProject.name = 'MyPetClinic' +