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 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

What is GEEK

Buddha Community

Zak Dyer

1548824661

You haven’t sh() method by default in Groovy. Seems to be that you need to install Pipeline Pluginfirst (it has the component Pipeline: Groovy). And then sh() method shall be accessible.

Alycia  Klein

Alycia Klein

1596715800

Jenkins Pipeline Groovy script - Part 2 Add a User to a Gitlab Group

My earlier post on Jenkins Pipeline Groovy script - Part 1 , discusses about how to create Gitlab group using Jenkins Pipeline Groovy script. In this post, I am discussing about how to add list of Users to newly created Gitlab group or existing group with specific permission granted to them. In companies while handling big projects and maintaining enormous code repositories, it is very much important to know what permissions supposed to be given to each member in the team.

Because giving wrong permission to team member, opens big path for problems if the team member really unaware about how efficiently the permission can be used for good purpose. Hence giving right permissions to the team member is very serious business for quick and reliable delivery. And yes, this also avoids team member intervening unnecessarily to the projects where they do not actually belong.

1. Gitlab User Restful API

Gitlab exposes it’s functionality via Restful APIs which can be consumed via curl script or any programming language like Groovy and Python., etc. To add user to Gitlab group, Gitlab Project members Restful APIs are consumed in this tutorial.

Adding group members to GitHub

So who are Users in Gitlab Project, Users are team members who work on code level changes as developer or owner to the specific Gitlab project repositories or can even play any role(mentioned section 1.1) depending on the project requirement.

1.1 Gitlab User Permission Level

Based on the responsibility and the role of the team members assigned in the project level, Gitlab offers following member permissions level.

  • Guest (10)
  • Reporter (20)
  • Developer (30)
  • Maintainer (40)
  • Owner (50)

Though the permission level varies for different role, all the users have permission to clone or download the project code, leave the comments and create the issues in Gitlab Project where they belong. suppose, if any of the team member leaves the project, merge request and created issues will be unassigned automatically.

2. Pipeline Groovy script to add user to Gitlab group

So how to add users to Gitlab group, my earlier post on Jenkins Pipeline Groovy script - Part 1, discusses on how to create Gitlab group using Pipeline Groovy script. Here I am explaining about how to add team members to a newly created Gitlab group or existing group. Let’s create groovy class with name addUserToGitlabGroup.groovy

What is this groovy class does:

  • Checks whether the user has access to Gitlab URL instance or not
  • Only if the user has access, then adds the user to mentioned Gitlab group with given access level

#groovy #gitlab #jenkins ci #jenkins automation #jenkins

Jenkins Is Getting Old — It’s Time to Move On

By far, Jenkins is the most adopted tool for continuous integration, owning nearly 50% of the market share. As so many developers are using it, it has excellent community support, like no other Jenkins alternative. With that, it has more than 1,500 plugins available for continuous integration and delivery purposes.

We love and respect Jenkins. After all, it’s the first tool we encountered at the beginning of our automation careers. But as things are rapidly changing in the automation field, Jenkins is** left behind with his old approach**. Even though many developers and companies are using it, most of them aren’t happy with it. Having used it ourselves on previous projects, we quickly became frustrated by its lack of functionality, numerous maintenance issues, dependencies, and scaling problems.

We decided to investigate if other developers face the same problems and quickly saw the need to create a tool ourselves. We asked some developers at last year’s AWS Summit in Berlin about this. Most of them told us that they chose Jenkins because it’s free in the first place. However, many of them expressed interest in trying to use some other Jenkins alternative.

#devops #continuous integration #jenkins #devops adoption #jenkins ci #jenkins pipeline #devops continuous integration #jenkins automation #jenkins scripts #old technology

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 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

Artem Salimov

Artem Salimov

1600175566

How to run any Jenkins Job from the Command Line

This is image title

The last 2 years I have been working with a lot of Jenkins environments and of course, I had to run a bunch of Jenkins jobs every day. And yes, you are right, for each job, you gotta go to a particular Jenkins Web interface, find a particular job and finally, pardon me, run this crappy Job. In short, it made me waste a lot of time on useless things.

Based on this, I decided to make life easier and develop a tool that will help me get rid of these meaningless jobs.

Either this is familiar to you, or if you just would like to simplify your work, then let me take you through a guide on how to get started.

Jenkins-job-cli

This is an open-source CLI tool written in GO to run any Jenkins Job/Build from the Command-line/Terminal: https://github.com/gocruncher/jenkins-job-cli.

Alt Text

Installation

Fetch the latest release for your platform:

Linux

sudo wget https://github.com/gocruncher/jenkins-job-cli/releases/download/v1.1.0/jenkins-job-cli-1.1.0-linux-amd64 -O /usr/local/bin/jj
sudo chmod +x /usr/local/bin/jj

OS X brew

brew tap gocruncher/tap
brew install jj

OS X bash

sudo curl -Lo /usr/local/bin/jj https://github.com/gocruncher/jenkins-job-cli/releases/download/v1.1.0/jenkins-job-cli-1.1.0-darwin-amd64
sudo chmod +x /usr/local/bin/jj

Configure Access to Multiple Jenkins

jj set dev_jenkins --url "https://myjenkins.com" --login admin --token 11aa0926784999dab5  

where the token is available in your personal configuration page of the Jenkins. Go to the Jenkins Web Interface and click your name on the top right corner on every page, then click “Configure” to see your API token.

In case, when Jenkins is available without authorization:

jj set dev_jenkins --url "https://myjenkins.com"  

or just run the following command in dialog execution mode:

jj set dev_jenkins

Shell autocompletion

As a recommendation, you can enable shell autocompletion for convenient work. To do this, run following:

# for zsh completion:
echo 'source <(jj completion zsh)' >>~/.zshrc

# for bash completion:
echo 'source <(jj completion bash)' >>~/.bashrc

if this does not work for some reason, try following command that might help you to figure out what is wrong:

jj completion check

Examples

# Configure Access to the Jenkins
jj set dev-jenkins

# Start 'app-build' job in the current Jenkins
jj run app-build

# Start 'web-build' job in Jenkins named prod
jj run -n prod web-build

# makes a specific Jenkins name by default
jj use PROD  

So, I hope you find this tool helpful. I would be very glad if you haven’t already, please check it out and let me know what you think or what problems you are facing.

So, I hope you find this tool helpful. I would be pretty happy, if you haven’t already, please check it out and let me know what you think or what problems did you face.

##jenkins ##job ##bash ##command-line

Kira  Nienow

Kira Nienow

1595181780

Jenkins — How Can We Get Started Jobs in Jenkins (Part 3)

In the previous article, we learned how to create Users + Manage + Assign Roles in Jenkins. In this article, I will be highlighting how can we create some basic JOBS in Jenkins. Furthermore, these are the points which I am going to explain.

  • How to create a basic Job in Jenkins.
  • Basic Job Configurations.
  • How to run the Job remotely.
  • How to chain Job Execution

How to create a basic Job in Jenkins?

First of all **click **on New Item on the Jenkins Dashboard. Then Give a name to your project. Then you can select a **Freestyle **or **Multi-configuration **project. For this tutorial please go with the Freestyle project. Then click on the **Okay **button.

As soon as you complete these simple steps you will be taken to the **configuration **window. Here you can provide different details about Jobs. There are 5 sections available. **General, Source Code Management, Build Triggers, Build, **and Post-build Actions.

Image for post

General Section

Here you can provide a quick description of your project or Job. You can preview it also. You can also use HTML tags inside the description.

Image for post

Source Code Management Section

So here in case you want to take the build from any source code system like Git, Bitbucket, or any other source code management portals. We need to add plugins to those source code repositories. But in the coming tutorials, we will see how exactly we can get our build and trigger our Job from a source code management system.

#jenkins-job-builder #jenkins #jenkins-pipeline