/* groovylint-disable LineLength, NglParseError */
@Library('epo-pipeline-library@master') _
def cloudId = 'epo-cicd'
def cicdNamespace = 'epo-cicd'

//
// If this regular expression matches the branch for this CICD flow, it will be deployed to the
// the expected environment. This approach is meant to eliminate a developer from having to
// change mulitple place within the JenkinsFile.
//
def tos1Match = ~/.*EPOBKOFF-6959.*/
def tos2Match = ~/.*EPOBKOFF-6068-disabled.*/
def tos3Match = ~/.*EPOBKOFF-4908-disabled.*/
def tos4Match = ~/.*EPOBKOFF-4908-disabled.*/
def tos5Match = ~/.*EPOBKOFF-4908-disabled.*/
def tos6Match = ~/.*EPOBKOFF-4908-disabled.*/

def HELM_SNAPSHOT_REPO = "epo-helm-snapshot-local"
def HELM_RELEASE_REPO = "epo-helm-release-local"
def DOCKER_SNAPSHOT_REPO = "epo-docker-snapshot-local"
def DOCKER_RELEASE_REPO = "epo-docker-release"

def HELM_DEPENDENCY_RELEASE_REPO = "@epo-helm-release"
def HELM_DEPENDENCY_DEV_REPO = "@epo-helm-dev"

def dependencyHelmRepo = HELM_DEPENDENCY_DEV_REPO

def buildAgent = agentBuilder(
  qualifier: 'azure',
  cloud: cloudId,
  namespace: cicdNamespace,
  agents: [
    epo.standardAgents()['maven-jdk11'],
        epo.standardAgents()["nodejs-14"],
    epo.standardAgents().oc,
    epo.standardAgents().helm3,
        epo.standardAgents()['sonar-scanner-jdk11'],
        epo.standardAgents()['blackduck-jdk11'],
    epo.standardAgents().veracode,
      epo.standardAgents().sysdig
  ]
)

def imageBuilder = openshiftImageBuilder(
  pushSecret: 'epo-artifactory-azure',
  pullSecret: 'epo-artifactory-azure',
  serviceAccount: 'epo-cicd-svc'
)

def pomVersion
def baseCharts = [:]
def implementationReleases = [:]
def dockerImages = [:]
def deployNamespace = "epo-profile-bkoffice"
def dockerRepository = DOCKER_SNAPSHOT_REPO
def helmRepository = HELM_SNAPSHOT_REPO

def implEnv = ""

debug = true
def globalHelmArgs = "--namespace ${deployNamespace}"
if (debug) {
  globalHelmArgs += " --debug -v 3"
}

def prepareFlow = {
  echo "------ prepareFlow() -------"

  imageVersion = ""
  chartVersion = ""
  implChartName = ""
  blackduckScanVersion = "temp"
  mbpHelpBaseChartName = 'mbp-help-base'
  mbpHelpUiChartName = 'mbp-help-ui-app'
  mbpHelpRestChartName = 'mbp-help-rest-app'

  pomVersion = mavenArtifactInfo().version
  echo "-------------"
  echo "pomVersion: ${pomVersion}"
  echo "-------------"

  // The POM Version will drive the versioning for the Docker Images and Charts.
  // This eliminate the need to change flow when reving versions.
  // The mavenArtifactInfo() method returns several pom.xml properties one being
  // version. This takes into account the "revision" property usage approach.
  // NOTE: The next statement is temporary and only necessary since the naming convention of
  //      MAINTPOC-SNAPSHOT was used to ensure no existing -SNAPSHOT version were referenced.
  semVer = pomVersion.replaceAll("-SNAPSHOT", "");

  //
  // A "Base Chart" and "Docker Image" will be created for each deployable project.
  //
  // In addition, for each environment(Sprint, Develop, etc.) the docker and chart version is alter to include
  // an environment designation. This is necessary to keep the environment separate from each other
  // since a Sprint and Develop deployment could be executing/referenced at the same time.
  //
  echo "Branch Name: ${env.BRANCH_NAME}"
  switch (env.BRANCH_NAME) {
    case tos1Match:
      implEnv='help-3-9-tos1'
      chartVersion = "${semVer}-${implEnv}-latest"
      imageVersion = "${semVer}-${implEnv}-springBoot"
      break
    case tos2Match:
      implEnv='help-3-9-tos2'
      chartVersion = "${semVer}-${implEnv}-latest"
      imageVersion = "${semVer}-${implEnv}-springBoot"
      break
    case tos3Match:
      implEnv='help-3-9-tos3'
      chartVersion = "${semVer}-${implEnv}-latest"
      imageVersion = "${semVer}-${implEnv}-springBoot"
      break
    case tos4Match:
      implEnv='help-3-9-tos4'
      chartVersion = "${semVer}-${implEnv}-latest"
      imageVersion = "${semVer}-${implEnv}-springBoot"
      break
    case tos5Match:
      implEnv='help-3-9-tos5'
      chartVersion = "${semVer}-${implEnv}-latest"
      imageVersion = "${semVer}-${implEnv}-springBoot"
      break
    case tos6Match:
      implEnv='help-3-9-tos6'
      chartVersion = "${semVer}-${implEnv}-latest"
      imageVersion = "${semVer}-${implEnv}-springBoot"
      break
    case ~/develop/:
      implEnv='help-3-9-dev'
      chartVersion = "${semVer}-${implEnv}-latest"
      imageVersion = "${semVer}-${implEnv}-springBoot"
      blackduckScanVersion ="${semVer}"
      break
    case ~/sprint/:
      implEnv='help-3-9-sprint'
      chartVersion = "${semVer}-${implEnv}-latest"
      imageVersion = "${semVer}-${implEnv}-springBoot"
      break
    case ~/maint\/[(\d)]{0,3}.[(\d)]{0,3}.[(\d)]{0,3}-img[(\d)]{0,3}/:
    case ~/maint\/[(\d)]{0,3}.[(\d)]{0,3}.[(\d)]{0,3}/:
      chartVersion = "${semVer}-${implEnv}-latest"
      imageVersion = "${semVer}-${implEnv}-springBoot"
      blackduckScanVersion ="${semVer}"
      break
    //
    // These selectors will match branches were Release Artifacts will be published.
    //
    case ~/release\/[(\d)]{0,3}.[(\d)]{0,3}.[(\d)]{0,3}-RC[(\d)]{0,3}/:
    case ~/release\/.*/:
    case ~/maint\/[(\d)]{0,3}.[(\d)]{0,3}.x/:
    case ~/master/:
      dependencyHelmRepo = HELM_DEPENDENCY_RELEASE_REPO
      chartVersion = "${semVer}"
      imageVersion = "${semVer}-springBoot"
      blackduckScanVersion ="${semVer}"
      break;
    default:
      echo('prepare Stage - unrecognized branch type')
      chartVersion = "${semVer}-latest"
      imageVersion = "${semVer}-latest-springBoot"
  }

  echo "-------------"
  echo "chartVersion: ${chartVersion}"
  echo "imageVersion: ${imageVersion}"
  echo "implEnv: ${implEnv}"
  echo "helmRepository: ${helmRepository}"
  echo "-------------"

  baseCharts.put("${mbpHelpUiChartName}", [contextDir:'./dev-ops/charts', version:chartVersion])
  baseCharts.put("${mbpHelpRestChartName}", [contextDir:'./dev-ops/charts', version:chartVersion])

  dockerImages.put('mbp-help-ui-app', [contextDir: './dev-ops/images/mbp-help-ui-app', version: "${imageVersion}", dockerfilePath: 'Dockerfile.springBoot', buildArgs: [APP_JAR_NAME:"mbp-help-app-${pomVersion}.jar"]])
  dockerImages.put('mbp-help-rest-app', [contextDir: './dev-ops/images/mbp-help-rest-app', version: "${imageVersion}", dockerfilePath: 'Dockerfile.springBoot', buildArgs: [APP_JAR_NAME:"mbp-help-rest-app-${pomVersion}.jar"]])

  //
  // These charts rely on the charts above therefore added to the map in this sequence
  //
  baseCharts.put("${mbpHelpBaseChartName}", [contextDir:'./dev-ops/charts', version:chartVersion])

  //
  // This will only be applicable for branches that deploy to an implementation environment.
  //
  if(implEnv) {
    deployValueOverrides = "-f ./dev-ops/charts-impl/help-development-base-values.yaml -f ./dev-ops/charts-impl/$implEnv/help-values.yaml"

    //
    // All dependent charts must proceed this chart.
    //
    baseCharts.put("help-dependency-stack", [contextDir:'./dev-ops/charts-dev-only', version:chartVersion])

    //
    // Add the appropriate implementation chart to support the branch
    //
    implementationReleases = [
      "${implEnv}":  [contextDir:'./dev-ops/charts-impl', version:chartVersion]
    ]
  }

  //
  // When executing a "release" the dependency repository must be set to @epo-helm-release and not @epo-helm-dev.
  // This will update the Chart.yaml dependency repository for the specified charts.
  //
  dir('dev-ops') {
    /*
    The following variables need to be applied to the BOD Shell UI/REST Chart.yaml.
    */
    sh "find . | grep -i 'Chart.yaml' | xargs sed -i 's/{{CHART_VERSION}}/${chartVersion}/g;s/{{HELM_DEPENDENCY_REPOSITORY}}/${dependencyHelmRepo}/g'"

    //
    // Update the docker version in each of the baseCharts, not solution charts. Therefore it's placed here in the flow.
    //
    sh "find . | grep -i 'values.yaml' | xargs sed -i 's/{{IMAGE_VERSION}}/${imageVersion}/g'"
  }

  echo "-------------"
  echo "Helm Repository: ${helmRepository}"
  echo "Base Charts: ${baseCharts}"
  echo "Implementation Releases: ${implementationReleases}"
  echo "-------------"
  echo "Docker Repository ${dockerRepository}"
  echo "Docker Images: ${dockerImages}"
  echo "-------------"
}

def buildImages = { parameters = [:] ->
  echo "---- Build Images ----"
  shouldPublish = parameters.containsKey('publish') ? parameters.publish : true
  echo "Docker Images: **[${dockerImages}]**"
  parallel(dockerImages.collectEntries { imageName, imageProps ->
    [
      "${imageName}": {
        echo "Image Name: **[${imageName}]** Image Props: **[${imageProps}]**"
        imageBuilder(
          name: "${dockerRepository}.docker.fis.dev/mbp/help/${imageName}",
          tag: imageProps.version,
          contextDir: imageProps.contextDir,
          publish: shouldPublish,
          dockerfilePath: imageProps.dockerfilePath,
          buildArgs: imageProps.buildArgs
        //
        // buildArgs: [APP_JAR_NAME:"mbp-help-rest-app-${semVer}-SNAPSHOT.jar", WEB_APP_JAR_NAME:"mbp-help-app-${semVer}-SNAPSHOT.jar"]
        )
      }
    ]
  })
}

def buildCharts = { parameters = [:] ->
  echo "---- Build Charts ----"
  shouldPublish = parameters.containsKey('publish') ? parameters.publish : true
  charts = parameters.containsKey('charts') ? parameters.charts : true
  helmRepo = parameters.containsKey('helmRepo') ? parameters.helmRepo : HELM_SNAPSHOT_REPO

  chartBuilder = helmChartBuilder(repo: helmRepo)
  echo "Charts: ${charts}"
  container('helm3') {
    charts.each { chartName, chartProps ->
      dir(chartProps.contextDir) {
        sh """
        pwd
        """
        echo "Chart Name: ${chartName}"
        echo "Chart Props: ${chartProps}"
        echo "should Publish: ${shouldPublish}"
        chartBuilder(
          name: chartName,
          version: chartProps.version,
          publish: shouldPublish
        )
      }
    }
  }
}

def stages = [
  prepare: {
        container('maven-jdk11') {
      prepareFlow();
        }
  },
  buildMvn: {
    container('maven-jdk11') {
      sh """mvn clean package -V -U -B -DskipTests"""
    }
  },
  publishMvn: {
    container('maven-jdk11') {
      sh """mvn clean deploy -V -U -B -DskipTests"""
    }
  },
  buildNpm: {
    container('nodejs-14') {
      dir('mbp-help-ui/mbp-help-client') {
        npmScript = 'build-prod'
        switch(env.BRANCH_NAME){
          case tos1Match:
          case tos2Match:
          case tos3Match:
          case tos4Match:
          case tos5Match:
          case tos6Match:
            npmScript = 'build-sprint'
            break;
        }
        sh """
        node -v
        npm -v
        npm install
        npm run ${npmScript}
        ls dist
        ls dist/apps/mbp-help-ui
        ls dist/apps/mbp-help-ui/assets
        """
      }
    }
  },
  sonarQualityDontFailBuild: {
    container('sonar-scanner-jdk11') {
      sonarScan(breakBuild:false)
    }
  },
  sonarQualityFailBuild: {
    container('sonar-scanner-jdk11') {
      sonarScan()
    }
  },
  veracode:{
    print "*********** Executing VeraCode ***********"
    container('veracode') {
      veracodeScan(
        appName: 'MBP_Backoffice_Help',
        exclusions: []
      )
    }
  },
  sysdig: {
    container('sysdig') {
      echo "Docker Images-sysdig: **[${dockerImages}]**"
      parallel(dockerImages.collectEntries { imageName, imageProps ->
      [
      "${imageName}": {
          //        the docker repository that is used to pull the image.
          sysdigImage = "${dockerRepository}.docker.fis.dev/mbp/help/${imageName}:" + "${imageVersion}"
          echo "---SYSIMAGE --- ${sysdigImage}"
          sysdigScan(
            image: sysdigImage,  // Image to be scanned or evaluated
            operation: 'add'  // Operation: 'add' or 'evaluation'
          )
      }
      ]
      })
    }
  },
  blackduck:{
    container('blackduck-jdk11') {
      blackduckScan(
        name: 'mbp_help_16100',
        version:"${pomVersion}",
        exclusions: []
      )
    }
  },
    publishImages: {
    container('maven-jdk11') {
      // Prepare MBP - Help UI Docker Image
      sh """
        rm -rf ./dev-ops/images/mbp-help-ui-app/deployments
        mkdir ./dev-ops/images/mbp-help-ui-app/deployments
        cp ./mbp-help-ui/mbp-help-server/mbp-help-app/target/mbp-help-app-${pomVersion}.jar ./dev-ops/images/mbp-help-ui-app/deployments
      """

      // Prepare MBP - Help REST Docker Image
      sh """
        rm -rf ./dev-ops/images/mbp-help-rest-app/deployments
        mkdir ./dev-ops/images/mbp-help-rest-app/deployments
        cp mbp-help-rest/mbp-help-rest-app/target/mbp-help-rest-app-${pomVersion}.jar ./dev-ops/images/mbp-help-rest-app/deployments
        """
    }

    container('oc') {
      buildImages(
        publish: true
      )
    }
  },
    publishCharts: {

    container('maven-jdk11') {
      //
      // Copy common chart artifacts to each chart
      //
      sh """
        cp ./dev-ops/charts/common/templates/* ./dev-ops/charts/mbp-help-rest-app/templates -r
        cp ./dev-ops/charts/common/templates/* ./dev-ops/charts/mbp-help-ui-app/templates -r
      """
    }

    //
    // NOTE: Base Charts must be publish prior to implementation charts,
    //       since the implementation chart will depend on the base charts.
    //
    container('helm3') {
      buildCharts(
        charts: baseCharts,
        publish: true,
        helmRepo: helmRepository
      )
      // Always use the snapshot repository for implementation charts
      buildCharts(
        charts: implementationReleases,
        publish: true,
        helmRepo: HELM_SNAPSHOT_REPO
      )
    }
  },
  lintHelmChart : {
      container('helm3') {
        sh "helm repo update ${globalHelmArgs}"

        echo "*** Linting Base/Solution Charts ***"
        baseCharts.each { chartName, chartProps ->
          dir("${chartProps.contextDir}") {
            echo "chartName: ${chartName}"
            echo "chartProps: ${chartProps}"

            sh """
            helm dependency update ${chartName} ${globalHelmArgs}
            helm lint ${chartName} ${globalHelmArgs}
            helm template ${chartName} ${globalHelmArgs}
            """
          }
        }

        echo "*** Linting Implementation Charts ***"
        implementationReleases.each { chartName, chartProps ->
          dir("${chartProps.contextDir}") {
            echo "chartName: ${chartName}"
            echo "chartProps: ${chartProps}"

            sh """
            helm dependency update ${chartName} ${globalHelmArgs}
            pwd
            helm lint ${chartName} -f help-development-base-values.yaml -f ${chartName}/help-values.yaml ${globalHelmArgs}
            helm template ${chartName} -f help-development-base-values.yaml -f ${chartName}/help-values.yaml ${globalHelmArgs}
            """
          }
        }
      }
  },
  /* groovylint-disable-next-line NglParseError */
  def promoteImage = { parameters = [:] ->
    echo "---- Promote Images ----"
    echo "Docker Images: **[${dockerImages}]**"
    parallel(dockerImages.collectEntries { imageName, imageProps ->
      [
        "${imageName}": {
          echo "Image Name: **[${imageName}]** Image Props: **[${imageProps}]**"
          basicAuthCredentials('epo-cicd-svc-artifactory') { userId, password ->
          promoteImage(
            name: "${dockerRepository}.docker.fis.dev/mbp/help/${imageName}",
            tag: imageProps.version,
            qualityCheck: false,
            username: userId,
            password: password
        )
          }
        }
    ]
    }
  )
},

def promoteCharts = { parameters = [:] ->
  echo "---- Promote Charts ----"
  container('helm3') {
    basicAuthCredentials('epo-cicd-svc-artifactory') { userId, password ->
    charts.each { chartName, chartProps ->
      dir(chartProps.contextDir) {
        sh """
        pwd
        """
        echo "Chart Name: ${chartName}"
        echo "Chart Props: ${chartProps}"
        promoteChart(
          name: chartName,
          version: chartProps.version,
          username: userId,
          password: password
        )
      }
    }
    }
  }
},
  deploy: {
    echo "---- Deploy Stage ----"
    echo "Deploying Implementation charts"
    container('helm3') {
      sh "helm repo update ${globalHelmArgs}"
      implementationReleases.each { chartName, chartProps ->
        echo "*** Deploying Chart ***"
        echo "chartName: ${chartName}"
        echo "chartProps: ${chartProps}"

        exitStatus = sh(returnStatus: true, script: "helm history $chartName ${globalHelmArgs}")
        if(exitStatus == 0) {
          echo "Uninstalling chart name: [${chartName}]"
          sh "helm uninstall $chartName ${globalHelmArgs}"
        } else {
          echo "Note: chart name: [${chartName}] is not installed therefore will not need to be uninstalled."
        }

        sh """
          helm upgrade $chartName 'epo-helm-dev/$chartName' --namespace $deployNamespace  $deployValueOverrides  --debug --version ${chartProps.version} --install --force
        """
      }
    }
  }
]

buildAgent {
  epo.runStages(stages, ['prepare'])
  switch(env.BRANCH_NAME) {
    case tos1Match:
    case tos2Match:
    case tos3Match:
    case tos4Match:
    case tos5Match:
    case tos6Match:
      if(pomVersion.endsWith("-SNAPSHOT")) {
        //epo.runStages(stages, ['buildNpm','buildMvn','sonarQualityDontFailBuild','publishImages','publishCharts','deploy'])
        //epo.runStages(stages, ['buildNpm','buildMvn','publishImages','publishCharts','lintHelmChart','blackduck'])
        epo.runStages(stages, ['buildNpm','buildMvn','publishImages','publishCharts','lintHelmChart','deploy','veracode'])
      //epo.runStages(stages, ['buildNpm','buildMvn','publishImages','publishCharts','lintHelmChart'])
      //epo.runStages(stages, ['buildNpm','buildMvn','publishImages','publishCharts','lintHelmChart', 'deploy'])
      //epo.runStages(stages, ['publishCharts','lintHelmChart'])
      //epo.runStages(stages, ['publishCharts','lintHelmChart','deploy'])
      //epo.runStages(stages, ['publishCharts','deploy'])
      //epo.runStages(stages, ['deploy'])
      //epo.runStages(stages, ['buildNpm','publishMvn','blackduck'])
      //epo.runStages(stages, ['buildNpm','buildMvn','blackduck','publishImages','publishCharts','sysdig','deploy'])
      } else {
        epo.runStages(stages, [])
      }
      break;
    case ~/^feature\/.*/:
    case ~/^bugfix\/.*/:
      epo.runStages(stages, [])
      //epo.runStages(stages, ['buildNpm','buildMvn','publishImages','publishCharts','lintHelmChart'])
      break
    case ~/^PR-.*/:
      epo.runStages(stages, ['buildNpm','buildMvn'])
      break
    case ~/maint\/[(\d)]{0,3}.[(\d)]{0,3}.[(\d)]{0,3}/:
    case ~/maint\/[(\d)]{0,3}.[(\d)]{0,3}.[(\d)]{0,3}-img[(\d)]{0,3}/:
      if(pomVersion.endsWith("-SNAPSHOT")) {
        epo.runStages(stages, ['buildNpm','publishMvn'])
      /* groovylint-disable-next-line LineLength */
      //epo.runStages(stages, ['buildNpm','publishMvn','sonarQualityFailBuild','publishImages','publishCharts','deploy'])
      //epo.runStages(stages, ['buildNpm','publishMvn','sonarQualityFailBuild','publishImages','publishCharts'])
      //epo.runStages(stages, ['buildNpm','publishMvn','sonarQualityFailBuild','sysdig'])
      } else {
        epo.runStages(stages, [])
      }
      break
    case ~/sprint/:
      epo.runStages(stages, ['buildNpm','buildMvn','sonarQualityFailBuild','publishImages','publishCharts','sysdig','deploy'])
      break
    case ~/develop/:
      epo.runStages(stages, ['buildNpm','publishMvn','sonarQualityFailBuild','blackduck','veracode','publishImages','publishCharts','sysdig','deploy'])
      break
    //case ~/maint\/[(\d)]{0,3}.[(\d)]{0,3}.x/:
    case ~/master/:
      //
      // Releases are accomplished via the release branch.
      //
      epo.runStages(stages, ['buildNpm','buildMvn','sonarQualityDontFailBuild'])
      break
    //
    // All Release branches will only execute the prepare stage until the branch is ready to be released.
    // This assumes all releases are completed via the release branch.
    //
    case ~/release\/[(\d)]{0,3}.[(\d)]{0,3}.[(\d)]{0,3}-RC[(\d)]{0,3}/:
    case ~/release\/.*/:
      if(pomVersion.endsWith("-SNAPSHOT")) {
        epo.runStages(stages, [])
      } else {
        //epo.runStages(stages, [])
        //epo.runStages(stages, ['buildNpm','publishMvn','sonarQualityFailBuild','blackduck','veracode','publishImages','publishCharts','sysdig'])
        //epo.runStages(stages, ['buildNpm','buildMvn','blackduck','veracode','publishImages','publishCharts','sysdig'])
        epo.runStages(stages, ['buildNpm','publishMvn','publishImages','publishCharts','promoteImage','promoteChart'])
      }
      break
    default:
      echo('unrecognized branch type')
  }
}
 

Groovy online compiler

Write, Run & Share Groovy code online using OneCompiler's Groovy online compiler for free. It's one of the robust, feature-rich online compilers for Groovy language, running the latest Groovy version 2.6. Getting started with the OneCompiler's Groovy editor is easy and fast. The editor shows sample boilerplate code when you choose language as Groovy and start coding.

Read inputs from stdin

OneCompiler's Groovy online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Groovy program which takes name as input and prints hello message with your name.

def name = System.in.newReader().readLine()
println "Hello " + name

About Groovy

Groovy is an object-oriented programming language based on java. Apache Groovy is a dynamic and agile language which is similar to Python, Ruby, Smalltalk etc.

Key Features

  • It's not a replacement for java but it's an enhancer to Java with extra features like DSL support, dynamic typing, closures etc.
  • Accepts Java code as it extends JDK
  • Greater flexibility
  • Concise and much simpler compared to Java
  • Can be used as both programming language and scripting language.

Syntax help

Data Types

Data typeDescriptionRange
StringTo represent text literalsNA
charTo represent single character literalNA
intTo represent whole numbers-2,147,483,648 to 2,147,483,647
shortTo represent short numbers-32,768 to 32,767
longTo represent long numbers-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
doubleTo represent 64 bit floating point numbers4.94065645841246544e-324d to 1.79769313486231570e+308d
floatTo represent 32 bit floating point numbers1.40129846432481707e-45 to 3.40282346638528860e+38
byteTo represent byte value-128 to 127
booleanTo represent boolean values either true or falseTrue or False

Variables

You can define variables in two ways

Syntax:

data-type variable-name;

[or]

def variable-name;

Loops

0.upto(n) {println "$it"}

or

n.times{println "$it"}

where n is the number of loops and 0 specifies the starting index

Decision-Making

1. If / Nested-If / If-Else:

When ever you want to perform a set of operations based on a condition or set of conditions, then If / Nested-If / If-Else is used.

if(conditional-expression) {
  // code
} else {
  // code
}

2. Switch:

Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 //code to be executed when all the above cases are not matched;    
} 

List

List allows you to store ordered collection of data values.

Example:

def mylist = [1,2,3,4,5];
List MethodsDescription
size()To find size of elements
sort()To sort the elements
add()To append new value at the end
contains()Returns true if this List contains requested value.
get()Returns the element of the list at the definite position
pop()To remove the last item from the List
isEmpty()Returns true if List contains no elements
minus()This allows you to exclude few specified elements from the elements of the original
plus()This allows you to add few specified elements to the elements of the original
remove()To remove the element present at the specific position
reverse()To reverse the elements of the original List and creates new list