Cannot run a Groovy sh command in Jenkins

Cannot run a Groovy sh command in Jenkins

I cannot run Groovy scripts in Jenkins. Let me give you an overview of my setup.

I cannot run Groovy scripts in Jenkins. Let me give you an overview of my setup.

I run the official Jenkins docker image with the follow docker compose:

version: '2'
services:
  jenkins:
    image: jenkins/jenkins:lts
    container_name: jenkins
    ports:
      - 8080:8080
    volumes:
      - ./home:/var/jenkins_home

Jenkins is up & running as it should. I installed also the Groovy plugin http://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin .

Now I tried to run a simple Groovy script like this:

LSOUT = sh (
    script: 'ls -al',
    returnStdout: true
).trim()
echo "${LSOUT}"

But I cannot make it working. This is the error I am getting:

Building in workspace /var/jenkins_home/workspace/test_groovy
[test_groovy] $ groovy /var/jenkins_home/workspace/test_groovy/hudson4423271822305767083.groovy
Caught: groovy.lang.MissingMethodException: No signature of method: hudson4423271822305767083.sh() is applicable for argument types: (java.util.LinkedHashMap) values: [[script:ls -al, returnStdout:true]]
Possible solutions: use([Ljava.lang.Object;), is(java.lang.Object), run(), run(), any(), use(java.util.List, groovy.lang.Closure)
groovy.lang.MissingMethodException: No signature of method: hudson4423271822305767083.sh() is applicable for argument types: (java.util.LinkedHashMap) values: [[script:ls -al, returnStdout:true]]
Possible solutions: use([Ljava.lang.Object;), is(java.lang.Object), run(), run(), any(), use(java.util.List, groovy.lang.Closure)
    at hudson4423271822305767083.run(hudson4423271822305767083.groovy:1)
Build step 'Execute Groovy script' marked build as failure
Finished: FAILURE

Groovy is also installed as package in the container. Any idea?

docker jenkins

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Docker Explained: Docker Architecture | Docker Registries

Following the second video about Docker basics, in this video, I explain Docker architecture and explain the different building blocks of the docker engine; docker client, API, Docker Daemon. I also explain what a docker registry is and I finish the video with a demo explaining and illustrating how to use Docker hub.

WordPress in Docker. Part 1: Dockerization

This entry-level guide will tell you why and how to Dockerize your WordPress projects.

Integrating SonarQube with Jenkins

SonarQube is an open-source platform for continuous inspection of code quality. Here, We will discuss integrating SonarQube with Jenkins to achieve CI with fully automated code analysis.

Docker manifest - A peek into image's manifest.json files

The docker manifest command does not work independently to perform any action. In order to work with the docker manifest or manifest list, we use sub-commands along with it. This manifest sub-command can enable us to interact with the image manifests. Furthermore, it also gives information about the OS and the architecture, that a particular image was built for. The image manifest provides a configuration and a set of layers for a container image. This is an experimenta

List all containers in Docker(Docker command)

We can get a list of all containers in docker using `docker container list` or `docker ps` commands.