Groovy error in Jenkins declarative pipeline - jenkins

I have installed Jenkins on Docker and created a declarative pipeline from SCM. The Jenkinsfile is placed on Github and has following code:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
Now whenever I build the Jenkins job I get the following error
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: pipeline for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
Finished: FAILURE
And when I place the code from Jenkinsfile on Github directly to Jenkins, then it builds successfully. Not sure what is the issue, though the same thing had worked earlier(I have fresh installed Jenkins on Docker)

It worked for me after upgrading Script Security plugin to v1.46(latest)

Related

Trouble reading Secrets from Jenkins Credential Manager in Multi Branch Pipeline job using Credential Parameter

I have a Jenkins multi branch pipeline job that uses a secret value in Jenkinsfile:
pipeline {
agent any
stages {
stage('Test') {
steps {
echo "DOCKER_REGISTRY_USER is ${env.DOCKER_REGISTRY_USER_NSV}"
}
}
}
}
The secret value is stored in Credentials Manager as secret text with the ID DOCKER_REGISTRY_USER_NSV:
I'm trying to read this value in Jenkinsfile as shown above but I get the following output that prints out the value null for my secret:
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
DOCKER_REGISTRY_USER is null
[Pipeline] sh
I also tried referencing the secret text in my pipeline like this:
echo "DOCKER_REGISTRY_USER is ${DOCKER_REGISTRY_USER_NSV}"
But then I get this error when running the Jenkins job:
groovy.lang.MissingPropertyException: No such property: DOCKER_REGISTRY_USER_NSV for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:264)
I think I need to bind that credential to the job but I don't see an option to do that for a Multi-Branch Pipeline job, the way you can for a Freestyle or Pipeline job.
How can I use a secret credential in a Multi Branch Pipeline job?
You can use credentials() helper method to archive your purpose.
pipeline {
agent any
environment {
DOCKER_REGISTRY_USER = credentials('DOCKER_REGISTRY_USER_NSV')
// put the ID of credential as credentials()'s parameter.
}
stages {
stage('Test') {
steps {
echo "DOCKER_REGISTRY_USER is ${DOCKER_REGISTRY_USER}"
}
}
}
}

Running SonarQube Scanner on a Jenkins remote slave

I have a Docker container running Jenkins (2.150.1) and another Docker container running SonarQube (7.4). Jenkins is using the SonarQube Scanner for Jenkins plugin and the scanning is done on the Jenkins container. The Jenkinsfile for the project looks like this:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'building...'
}
}
stage('Test') {
steps {
echo 'testing...'
withSonarQubeEnv ('SonarQube') {
sh '/var/jenkins_home/sonar-scanner/sonar-scanner-3.2.0.1227-linux/bin/sonar-scanner'
}
echo 'really finished testing2'
}
}
stage("Quality Gate") {
steps {
timeout(time: 1, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
stage('Deployment') {
steps {
echo 'deploying...'
}
}
}
}
To get the scanning to work as part of a Jenkins pipeline job, I manually installed sonar-scanner on the Jenkins container by downloading the zip file and unzipping it to: /var/jenkins_home/sonar-scanner/sonar-scanner-3.2.0.1227-linux
This is working well, but I want to improve it by:
taking out the harcoded path to sonar-scanner from my Jenkinsfile
specify a non local location of sonar-scanner because I now need to run the scan on another VM/container instead of on the Jenkins container
I tried using Manage Jenkins > Global Tool Configuration > SonarQube Scanner and updated my Jenkinsfile to use SONAR_RUNNER_HOME instead of the hard coded path, but that didn't work and I got an error that sonar-scanner can't be found.
In Manage Jenkins > Global Tool Configuration > SonarQube Scanner check install automatically.
Then go to Manage Jenkins > Configure System and add the following
The Name should be the same as the parameter in the line in your Jenkinsfile: withSonarQubeEnv('SonarQube')

using msbuild in a Jenkins multi branch project

How can I use MSBuild in a Jenkins multi-branch project?
Here's my Jenkinsfile:
pipeline {
agent any
stages {
stage('restore') {
steps {
sh "echo 'TODO RUN TEST'"
}
}
stage('build') {
steps {
bat "\"${tool 'MSBuild'}\" .\\src\\MySollutionName.sln /p:Configuration=Release /p:Platform=\"Any CPU\" /p:ProductVersion=1.0.0.${env.BUILD_NUMBER}"
}
}
stage('test') {
steps {
sh "echo 'TODO RUN TEST'"
}
}
}
}
But I'm getting an error message:
No tool named MSBuild found
Is it possible to use MSBuild in Jenkins multi-branch project?
You need to first tell Jenkins what the MSBuild tool is:
https://wiki.jenkins.io/display/JENKINS/Custom+Tools+Plugin
Have you installed the MSBuild plugin?
https://wiki.jenkins.io/display/JENKINS/MSBuild+Plugin

Jenkins pipeline step happens on master instead of slave

I am getting started with Jenkins Pipeline. My pipeline has one simple step that is supposed to run on a different agent - like the "Restrict where this project can be run" option.
My problem is that it is running on master.
They are both Windows machines.
Here's my Jenkinsfile:
pipeline {
agent {label 'myLabel'}
stages {
stage('Stage 1') {
steps {
echo pwd()
writeFile(file: 'test.txt', text: 'Hello, World!')
}
}
}
}
pwd() prints C:\Jenkins\workspace\<pipeline-name>_<branch-name>-Q762JIVOIJUFQ7LFSVKZOY5LVEW5D3TLHZX3UDJU5FWYJSNVGV4Q.
This folder is on master. This is confirmed by the presence of the test.txt file.
I expected test.txt to be created on the slave agent.
Note 1
I can confirm that the pipeline finds the agent because the logs contain:
[Pipeline] node
Running on MyAgent in C:\Jenkins\workspace\<pipeline-name>_<branch-name>-Q762JIVOIJUFQ7LFSVKZOY5LVEW5D3TLHZX3UDJU5FWYJSNVGV4Q
But this folder does not exist on MyAgent, which seems related to the problem.
Note 2
This question is similar to Jenkins pipeline not honoring agent specification
, except that I'm not using the build instruction so I don't think the answer applies.
Note 3
pipeline {
agent any
stages {
stage('Stage 1') {
steps {
echo "${env.NODE_NAME}"
}
}
stage('Stage 2') {
agent {label 'MyLabel'}
steps {
echo "${env.NODE_NAME}"
}
}
}
}
This prints the expected output - master and MyAgent. If this is correct, then why is the workspace located in a different folder on master instead of being on MyAgent?
here is an example
pipeline {
agent none
stages {
stage('Example Build') {
agent { label 'build-label' }
steps {
sh 'env'
sh ' sleep 8'
}
}
stage('Example Test') {
agent { label 'deploy-label' }
steps {
sh 'env'
sh ' sleep 5'
}
}
}
}
I faced similar issue and the following pipeline code worked for me (i.e. the file got created on the Windows slave instead of Windows master),
pipeline {
agent none
stages {
stage("Stage 1") {
steps {
node('myLabel'){
script {
writeFile(file: 'test.txt', text: 'Hello World!', encoding: 'UTF-8')
}
// This should print the file content on slave (Hello World!)
bat "type test.txt"
}
}
}
}
}
I'm debugging a completely unrelated issue and this fact was thrown in my face. Apparently the pipeline is processed in the built-in node (previously known as the master node), with the steps being forwarded to the agent.
So even though echo runs on the agent, but pwd() will run on the built-in node. You can do sh 'pwd' to get the path on the agent.

Jenkins Pipeline SonarQube key name

When building a multibranch pipeline, I send each of my projects to SonarQube using the SonarQube plugin like so:
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr:'20'))
timeout(time: 30, unit: 'MINUTES')
}
tools {
maven 'Maven 3.3.9'
jdk 'JDK 1.8'
}
stages {
stage('Checkout') {
steps {
echo 'Checking out..'
checkout scm
echo "My branch is: ${env.BRANCH_NAME}"
}
}
stage('Build') {
steps {
echo 'Building..'
bat 'mvn clean verify -P!local'
}
}
stage('SonarQube analysis'){
steps{
echo 'Analysing...'
withSonarQubeEnv('SonarQube') {
bat 'mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar'
}
}
}
}
}
It works fine, but one thing I need it to do is to change the name of the project in SonarQube to be projectName/builtBranch instead of just the project name. Is there a way I can do this using the pipeline?
This doesn't seem to be a Jenkins issue; rather you should be able to set the various sonar.* properties (e.g. sonar.projectName or sonar.branch) when running the Maven plugin.
The documentation seems to have a full list:
https://docs.sonarqube.org/display/SONAR/Analysis+Parameters

Resources