Skip to content

Practice 2a ‐ Simple Gradle CI CD

Ármin Zavada edited this page Sep 19, 2024 · 3 revisions

Practice 2a - Simple Gradle CI/CD

Prerequisites

  • Personal GitHub account
  • JDK 21, IntelliJ, Git, Gradle

Tasks

Task 0 - Preparations

  1. Create a new public, personal GitHub repository
    • Essential to be public to use GitHub Actions!
    • Init the repository and clone to your machine.
    • Pull starting project state using the following commands:
      git remote add ase-labs https://github.com/ftsrg-edu/ase-labs.git
      git fetch ase-labs
      git switch practice-2a
      git push -u origin
      
  2. Examine src directory

Task 1 - Create Gradle project

  1. Execute gradle init in the root directory of the project
    • Continue overwrite
    • Basic project
    • Name: Shingler
    • All else default
  2. Examine generated files
  3. Set gradlew executable: git update-index --chmod=+x ./gradlew
  4. Commit and push added files.

Task 2 - Configure Gradle build

  1. Setup Java compilation

    • Add the Java plugin
      plugins { 
          java
      }
    • Configure JDK version to Java 21
      java.toolchain {
          languageVersion.set(JavaLanguageVersion.of(21))
      }
    • Add missing dependencies required for compilation
      repositories {
          mavenCentral()
      }
      
      val slf4jVersion = "1.7.36"
      val log4jVersion = "2.23.1"
      val picoCliVersion = "4.7.6"
      val junitVersion = "5.10.0"
      
      dependencies {
          // implementation -> build and runtime
          implementation("org.slf4j:slf4j-api:$slf4jVersion") // logging API: slf4j
          implementation("info.picocli:picocli:$picoCliVersion")
          
          // not used during build time
          runtimeOnly("org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion") // logging implementation: log4j
          
          // just for test source set
          testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
      
          testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
      }
    • CHECK: ./gradlew assemble task should execute without fail.
  2. Setup runnable distribution creation

    • Add Application plugin
      plugins { 
          application
      }
    • Configure application plugin
      application {
          mainClass = "hu.bme.mit.ase.shingler.similarity.SimilarityApp"
      }
    • CHECK: Run ./gradlew assemble task again, build/distributions should contain a tar file with all necessary artifacts packaged.
  3. Setup testing, add Jacoco plugin

    • Add Jacoco plugin and configure test tasks
      plugins { 
          jacoco
      }
      
      tasks {
          test {
              useJUnitPlatform()
              testLogging.showStandardStreams = true
              finalizedBy(jacocoTestReport)
          }
      
          jacocoTestReport {
              inputs.files(test.get().outputs)
          }
      }
    • CHECK: ./gradlew build task should execute without fail - assemble and check.
  4. Commit and push added files.

Task 3 - Setup GitHub Actions CI

  1. Create new .github/workflows directory
  2. Create new build.yml file in .github/workflows directory
     name: Continuous Integration
     on:
       push:
         branches:
           - '**'
     jobs:
       build:
         name: Build
         runs-on: ubuntu-latest
         permissions:
           contents: read
         steps:
           - name: Set up JDK 21
             uses: actions/setup-java@v4
             with:
               java-version: 21
               distribution: adopt
           - name: Checkout code
             uses: actions/checkout@v4
           - name: Setup Gradle
             uses: gradle/actions/setup-gradle@v4
           - name: Gradle build
             run: ./gradlew build
           - name: Upload Artifacts
             uses: actions/upload-artifact@v4
             with:
               path: |
                 build/distributions/*
                 build/reports/*
  3. Commit and push added files.
  4. CHECK: GitHub should automatically run the whole build process.

Supplementary Material