Groovy Scripting for Jenkins Pipelines

This guide provides an introduction to writing Groovy scripts for Jenkins pipelines, covering common tasks and a sample script demonstrating file manipulation, compilation, and remote execution.

Why Groovy?

Groovy is a dynamic language similar to Java, offering a concise and readable syntax for scripting within Jenkins pipelines. It integrates seamlessly with Jenkins, allowing you to automate complex build and deployment processes.

Common Scripting Tasks:

  • File Manipulation: Groovy provides methods for creating, copying, renaming, and deleting files within the workspace.
  • Remote Execution: You can use Groovy to execute commands on remote servers via SSH or tools like rsync for file transfer.
  • Conditional Statements: Control the flow of your pipeline based on conditions, such as build success or failure.
  • Looping: Automate repetitive tasks by using loops to iterate over lists or directories.
  • Data Structures: Groovy supports various data structures like lists and maps, allowing for organized data handling.

Sample Script:

This script demonstrates various functionalities:

Groovy

node { // Define execution on a Jenkins agent

  // Define workspace directory
  stage('Checkout Code') {
    git branch: 'master', url: 'https://github.com/your-user/your-repo.git'
  }

  // Create a new directory
  stage('Create Directory') {
    sh 'mkdir build' // 'sh' executes shell commands
  }

  // Copy files to the new directory
  stage('Copy Files') {
    copy(from: 'src/main/java/**/*.java', into: 'build/')
  }

  // Rename a file
  stage('Rename File') {
    sh 'mv build/Main.java build/MyMainClass.java'
  }

  // Delete a directory (be cautious!)
  // stage('Delete Directory') {
  //   deleteDir dir: 'build' // Uncomment to enable deletion
  // }

  // SSH copy to remote server
  stage('Deploy to Server') {
    sh 'scp -r build/* [email protected]:/path/to/target/'
  }

  // Alternatively, use rsync for efficient file transfer
  // stage('Deploy to Server (rsync)') {
  //   sh 'rsync -avz build/ [email protected]:/path/to/target/'
  // }

  // Compile Java code (assuming a Maven project)
  stage('Compile Code') {
    sh 'mvn compile'
  }

}

Explanation:

  1. node Block: Defines the execution environment (Jenkins agent) for the script.
  2. Stages: Each stage represents a specific step in the pipeline.
  3. Git Checkout: Downloads code from a Git repository using the git command.
  4. sh Block: Executes shell commands within a stage.
    • mkdir: Creates a new directory.
    • copy: Copies files from one location to another.
    • mv: Renames a file.
    • deleteDir: Deletes a directory (use with caution!).
    • scp: Copies files to a remote server using SSH.
    • rsync: Efficiently transfers files between servers (alternative to scp).
    • mvn: Executes Maven commands for building Java projects.
See also  Working with Multiple Database Connections in Laravel

Remember:

  • Adapt the script to your specific project needs.
  • Be cautious with file deletion commands (commented out in the example).
  • Secure your credentials (username, password) for remote access.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.