How to use service dependencies for Gradle daemon to speed up builds | Harness CIE Builds

16d12d54b3b4ae7aabc8a93417570bce0984e3c9
How to use service dependencies for Gradle daemon to speed up builds | Harness CIE Builds

If you want to use service dependencies for Gradle daemon to speed up builds | Harness CIE Builds, then as part of this article, we will be answering how to achieve this in Harness CIE.

  • Let’s say we have a below usecase.

If multiple steps using Gradle via CLI run step, then each one will start up a separate Gradle daemon (unless --no-daemon is set)

I was wondering if there was a way to use a service dependency (or any alternative way) to allow the gradle daemon to created only once and then re-used in each subsequent gradle step in order to speed up the overall build.

  • In case of multiple steps using Gradle, can we have the Gradle daemon running as a service dependency to speed up the build time?

  • You can achieve this by adding a service dependency with the following spec in your pipeline :

  stages:
    - stage:
        name: Build Gradle
        identifier: Build_Micro_Integrator
        type: CI
        spec:
          cloneCodebase: true
          infrastructure:
            type: KubernetesDirect
            spec:
              connectorRef: account.immutable75326k8sconnector
              namespace: harness-delegate-ng-immutable-75326
              automountServiceAccountToken: true
              nodeSelector: {}
              os: Linux
          execution:
            steps:
              - step:
                  type: Run
                  name: Check Gradle
                  identifier: Check_Gradle
                  spec:
                    connectorRef: DockerHubfjunior87
                    image: gradle:6.9.2
                    shell: Sh
                    command: |-
                      sleep 2
                      gradle --status
                      until (gradle --status | grep -q IDLE); do echo "Waiting for Gradle daemon..."; done
                    privileged: true
                    runAsUser: "0"
                  when:
                    stageStatus: Success
                  failureStrategies: []
              - step:
                  type: Run
                  name: Build Car File
                  identifier: Build_Docker_Image
                  spec:
                    connectorRef: DockerHubfjunior87
                    image: gradle:6.9.2
                    shell: Sh
                    command: |-
                      cd complete
                      gradle build --info --stacktrace
                      ls -ltra ~/.gradle/
                    privileged: true
                    imagePullPolicy: IfNotPresent
                    runAsUser: "0"
                    resources:
                      limits:
                        memory: 1Gi
                  failureStrategies: []
                  when:
                    stageStatus: All
          sharedPaths:
            - /var/run
            - /root/.gradle
          serviceDependencies:
            - identifier: Gradle_Daemon
              name: Gradle Daemon
              type: Service
              spec:
                connectorRef: DockerHubfjunior87
                image: gradle:6.9.2
                privileged: true
                envVariables:
                  GRADLE_USER_HOME: /harness/.gradle
                  GRADLE_OPTS: "-Dorg.gradle.jvmargs=\"-Xms1024m -Xmx2048m\""
                entrypoint:
                  - gradle
                runAsUser: "0"
                resources:
                  limits:
                    memory: 1G
        variables:
          - name: GRADLE_USER_HOME
            type: String
            description: ""
            value: /harness/.gradle
          - name: GRADLE_OPTS
            type: String
            description: ""
            value: "-Dorg.gradle.jvmargs=\"-Xms1024m -Xmx2048m\""
pipeline:
  allowStageExecutions: false
  projectIdentifier: xxxxxxxxxxxx
  orgIdentifier: default
  tags: {}
  properties:
    ci:
      codebase:
        connectorRef: account.General_Github
        repoName: spring-guides/gs-gradle
        build: <+input>
  stages:
    - stage:
        name: Build Gradle
        identifier: Build_Micro_Integrator
        type: CI
        spec:
          cloneCodebase: true
          infrastructure:
            type: KubernetesDirect
            spec:
              connectorRef: account.immutable75326k8sconnector
              namespace: harness-delegate-ng-immutable-75326
              automountServiceAccountToken: true
              nodeSelector: {}
              os: Linux
          execution:
            steps:
              - step:
                  type: Run
                  name: Check Gradle
                  identifier: Check_Gradle
                  spec:
                    connectorRef: DockerHubfjunior87
                    image: gradle:6.9.2
                    shell: Sh
                    command: |-
                      sleep 2
                      gradle --status
                      until (gradle --status | grep -q IDLE); do echo "Waiting for Gradle daemon..."; done
                    privileged: true
                    runAsUser: "0"
                  when:
                    stageStatus: Success
                  failureStrategies: []
              - step:
                  type: Run
                  name: Build Car File
                  identifier: Build_Docker_Image
                  spec:
                    connectorRef: DockerHubfjunior87
                    image: gradle:6.9.2
                    shell: Sh
                    command: |-
                      cd complete
                      gradle build --info --stacktrace
                      ls -ltra ~/.gradle/
                    privileged: true
                    imagePullPolicy: IfNotPresent
                    runAsUser: "0"
                    resources:
                      limits:
                        memory: 1Gi
                  failureStrategies: []
                  when:
                    stageStatus: All
          sharedPaths:
            - /var/run
            - /root/.gradle
          serviceDependencies:
            - identifier: Gradle_Daemon
              name: Gradle Daemon
              type: Service
              spec:
                connectorRef: DockerHubfjunior87
                image: gradle:6.9.2
                privileged: true
                envVariables:
                  GRADLE_USER_HOME: /harness/.gradle
                  GRADLE_OPTS: "-Dorg.gradle.jvmargs=\"-Xms1024m -Xmx2048m\""
                entrypoint:
                  - gradle
                runAsUser: "0"
                resources:
                  limits:
                    memory: 1G
        variables:
          - name: GRADLE_USER_HOME
            type: String
            description: ""
            value: /harness/.gradle
          - name: GRADLE_OPTS
            type: String
            description: ""
            value: "-Dorg.gradle.jvmargs=\"-Xms1024m -Xmx2048m\""
  notificationRules: []
  variables: []
  identifier: GradleCacheExample_Customer
  name: GradleCacheExample Customer

You can achieve this by adding the stage variables and referring to them in the environment variables section of each step in the stage that requires Gradle, as explained in the above pipeline example.

So this is how you can use service dependencies for Gradle daemon to speed up your builds in Harness CIE.

3 Likes