How to add ant build properties in jenkinsfile - ant

I am trying to create a jenkinsfile using ant script to build a salesforce project. I need to provide some build properties to execute the ant script.
Here is my jenkinsfile:
pipeline{
agent any
environment{
def antVersion ='Ant 1.9.16'
}
stages{
stage('Checkout'){
steps{
git credentialsId: '******', url: 'https://********/test_repo.git', branch: 'develop'
}
}
stage('Execute Ant Script'){
steps{
withEnv ([
["ANT_HOME=${tool antVersion}"],["readFile('./abc.txt').split('\n') as List"]
])
{
sh 'ant compile'
}
}
}
}
post {
always {
cleanWs()
}
}
}
Jenkins build fails with below error:
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.EnvStep.overrides expects class java.lang.String but received class java.util.ArrayList
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:492)
Caused: java.lang.IllegalArgumentException: Could not instantiate {overrides=[[ANT_HOME=C:\Users\********\Documents\Softwares\apache-ant-1.9.16-bin], [readFile('./abc.txt').split('
') as List]]} for org.jenkinsci.plugins.workflow.steps.EnvStep
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:334)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:302)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:193)
Finished: FAILURE
Can anyone help me resolve this?

Related

Jenkins error when calling pipeline B from Pipeline Job A

I am calling a pipeline job (new_job) from pipeline job 'test' along with passing the variables, but it's failing with error.
Both jobs are running fine individually.
Test Job:
pipeline {
agent any
stages {
stage('Executing First job') {
steps {
sh 'echo "Running first job"'
}
}
stage('Running another job') {
when {
equals expected: true, actual: params.deploy2job
}
steps {
build job: 'new_job', parameters: [string(name: 'abc', value: qa), string(name: 'xyz', value: test)]
}
}
}
}
Error:
[Pipeline] { (Running another job)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: qa for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:251)
at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:353)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:357)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:333)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:333)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:19)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.delegateAndExecute(ModelInterpreter.groovy:137)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:666)
at
....
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
Finished: FAILURE
As the error indicates you are trying to refer to a nonexistent variable named qa in your Pipline. If this is not a variable declared within the Pipeline, and if you just need to pass the values qa and test to the new_job just wrap the values with quotes.
build job: 'new_job', parameters: [string(name: 'abc', value: 'qa'), string(name: 'xyz', value: 'test')]

is there way to we can validate a pipeline file before jenkins starts executing the pipeline

I would like to ensure that a particular stage is not skipped from a JenkinsFile. For example i would like to ensure that dev has not skipped the Code scanning in the pipeline. This should happen before the pipeline is executed by Jenkins.
I could manually check this via looking for the "Test Results" on the page that I have included the image for below. This indicates that the job has published Test Results to the JUnit plugin.
If I were to write a Jenkinsfile, it might look something like this. But it is possible to attach these to the JUnit pipeline via manual methods as well:
pipeline {
agent any
stages {
stage('Compile') {
steps {
// Login to Repository
configFileProvider([configFile(fileId: 'nexus_maven_configuration', variable: 'MAVEN_SETTINGS')]) {
sh 'mvn -s $MAVEN_SETTINGS compile'
}
}
}
stage('Test') {
steps {
configFileProvider([configFile(fileId: 'nexus_maven_configuration', variable: 'MAVEN_SETTINGS')]) {
sh 'mvn -s $MAVEN_SETTINGS test'
}
}
}
}
post {
always {
junit '**/target/surefire-reports/*.xml'
archive 'target/*.jar'
}
}
}

jenkinsfile copyArtifacts fails

I have Copy Artifact Plugin installed & trying to build and deploy through jenkins pipeline with following Jenkinsfile
Parameter DEPLOY_BUILD_NUMBER default to current build number. I want to make it such a way pipeline should build and deploy if DEPLOY_BUILD_NUMBER is current build number OR just deploy whatever build number specified for DEPLOY_BUILD_NUMBER
pipeline {
agent { label 'windows' }
parameters {
string(
name: 'DEPLOY_BUILD_NUMBER',
defaultValue: '${BUILD_NUMBER}',
description: 'Fresh Build and Deploy OR Deploy Previous Build Number'
)
}
stages {
stage ('Build') {
steps {
echo "Building"
}
post {
success {
archiveArtifacts artifacts: 'build.tar.gz', fingerprint: true
}
}
}
stage ('Deploy') {
steps {
echo "Deploying...."
script {
step ([$class: 'CopyArtifact',
projectName: '${JOB_NAME}',
filter: "*.tar.gz"]);
}
}
}
}
post {
always {
cleanWs()
}
}
}
When I run this pipeline I get following error
java.lang.UnsupportedOperationException: no known implementation of interface jenkins.tasks.SimpleBuildStep is named CopyArtifact
Also tried
stage ('Deploy') {
steps {
echo "Deploying...."
copyArtifacts filter: '*.tar.gz', fingerprintArtifacts: true, projectName: '${JOB_NAME}'
}
}
which failed with following error
java.lang.NoSuchMethodError: No such DSL method 'copyArtifacts' found among steps
and
stage ('Deploy') {
steps {
echo "Deploying...."
script {
copyArtifacts filter: '*.tar.gz', fingerprintArtifacts: true, projectName: '${JOB_NAME}'
}
}
}
which gave me
java.lang.NoSuchMethodError: No such DSL method 'copyArtifacts' found among steps
What is the correct syntax for copyArtifacts ? what I am missing here ?
I would check the version of the Copy Artifacts plugin you have installed (you can see that in /pluginManager/installed), the minimum version that supports pipeline is 1.39
CopyArtifact defines a step, copyArtifacts, that you can use directly.
Check the step reference here

Groovy error in Jenkins declarative pipeline

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)

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