Jenkins Pipeline - attachmentsPattern (outside workspace) - jenkins

GENERATES REPORT FILE into /tmp (outside Jenkins Workspace)
def publisher = LastChanges.getLastChangesPublisher null, "SIDE", "LINE", true, true, "", "", "", "", ""
publisher.publishLastChanges()
def htmlDiff = publisher.getHtmlDiff()
writeFile file: "/tmp/${APPNAME}-${ENVIRONMENT}-build.diff.html", text: htmlDiff
ATTACHEMENT ANT BLOB
attachmentsPattern: "/tmp/*${APPNAME}-${ENVIRONMENT}-build.diff.html",
ERROR
ERROR: Error accessing files to attach: Expecting Ant GLOB pattern, but saw '/tmp/*/tmp/aaa-development-build.diff.html'. See http://ant.apache.org/manual/Types/fileset.html for syntax

Solved by using dir
dir ("tmp"){
attachmentsPattern: "**/*${APPNAME}-${ENVIRONMENT}-build.diff.html",
}

Related

How to specify correct report-task.txt file in SonarQube Jenkins plugin?

I am running a SonarQube analysis of my Node.js project in Jenkins. Sonar analysis creates report-task.txt file with the result. Unfortunately some modules in the node_modules folder also contain their report-task.txt files which results in a warning in Jenkins:
WARN: Found multiple 'report-task.txt' in the workspace. Taking the first one.
/jenkins/workspace/.scannerwork/report-task.txt
/jenkins/workspace/node_modules/some_module/.scannerwork/report-task.txt
/jenkins/workspace/node_modules/some_other_module/.scannerwork/report-task.txt
Everything is fine until the Sonar analysis for my project fails: then Jenkins takes a different report-task.txt file from a module in node_modules as a result of a Sonar analysis:
WARN: Found multiple 'report-task.txt' in the workspace. Taking the first one.
/jenkins/workspace/node_modules/some_module/.scannerwork/report-task.txt <- wrong file
/jenkins/workspace/node_modules/some_other_module/.scannerwork/report-task.txt
Is there a way to specify that /jenkins/workspace/.scannerwork/report-task.txt is the only correct result file and Jenkins should ignore all the other? Preferably using Jenkins Pipelines
This is a snippet from my pipeline:
sh 'cat .scannerwork/report-task.txt'
// copy to a properties file so we can ingest as variables
sh 'cp .scannerwork/report-task.txt .scannerwork/report-task.properties'
def props = readProperties file: '.scannerwork/report-task.properties'
def ceTaskUrl= props['ceTaskUrl']
def sonarServerUrl=props['serverUrl']
// wait for analysis to complete
waitUntil {
def response = httpRequest consoleLogResponseBody: true, contentType: 'APPLICATION_JSON', ignoreSslErrors: true, url: "${ceTaskUrl}", wrapAsMultipart: false
println "Sent a request, got a $response response"
def body = readJSON text: response.content
body.each { key, value -> }
ceTask = body.task.status
echo "Status is "+ceTask
if ("FAILED".equals(ceTask)){
echo "failed = "+ceTask
throw new Exception(failed+" Sonar process failed!")
}
return "SUCCESS".equals(ceTask)
sleep 30
}

How to use an external Groovy WinRM library within a Jenkins Pipeline parameter?

I need to build a dynamic drop down list parameter for my Jenkins pipeline from the result of a WinRM request (Powershell command).
The Jenkins parameter has to be a drop down list of Organisational Units Distinguished names grabbed from an Active Directory domain controller.
WinRM is already configured and working properly (tested from other Windows boxes and from Jenkins using Python pywinrm library).
I don't want to call Python from Groovy and I'd preferably go with a Groovy WinRM client.
I found a Goovy library groovy-winrm-client that might be a good fit to execute WinRM commands from Groovy but I can't figure out where should I put the library itself on the Jenkins server file system, how to import it and how to make all work seamlessy in the context of the pipeline parameter.
Jenkins is running on a Centos 7.X server
Groovy version is 2.4.7
Target server is Windows 2012 R2 with WinRM configured
Jenkins Groovy installation
/var/lib/jenkins/tools/hudson.plugins.groovy.GroovyInstallation/my_groovy_version
Pipeline
// Untested
properties([
parameters([
choice(
name: 'USER_TYPE',
choices: ['Active Directory','Other'],
description: "User type"
),
[
$class: 'DynamicReferenceParameter',
name: 'OU',
choiceType: 'ET_FORMATTED_HTML',
omitValueField: true,
description: "OU distinguishedName",
referencedParameters: 'USER_TYPE',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script: "return['Error']"
],
script: [
classpath: [],
sandbox: false,
script: """
// External lib to be imported
import com.aestasit.infrastructure.winrm.client.WinRMClient
if (USER_TYPE.equals('Active Directory')) {
def html = '''<select name="value">
<option value="----">----</option>
'''
WinRMClient client = new WinRMClient(protocol: 'https', host: 'server.tld', port: 5986, user: 'my_user', password: '*********')
client.openShell()
String commandId = client.executeCommand("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe", "-command {get-organisationnalunit -filter * -properties * | % { $_.distinguishedname } | sort }")
CommandOutput output = client.commandExecuteResults(commandId)
output.output.eachLine{
html += '''<option value="${it}">${it}</option>'''
}
html += '''</select>'''
return html
}
"""
]
]
]
])
])

Excluding folders on CheckMarx scan

I'm working on implementing Checkmarx scans in our code repository. I'm using Jenkins and the CheckMarx plugin to accomplish this task. There are some folders I want to exclude from the scan; referencing the Jenkins plugin documentation it seems like all I have to do is add the folder names in the 'excludeFolders' field. However that doesn't appear to work, or maybe I don't have the value entered correctly.
I've tried '/test', 'test/', '!/test//*' but none work and the folder is still registered and zipped before it is uploaded to our CheckMarx server.
Below is what I have in my pipeline:
stage("Running CheckMarks for Layer"){
steps{
script{
def layer_dir = readFile file: 'layer-list'
def layer_list = layer_dir.split('\\r?\\n')
println (layer_list)
layer_list.each { layer ->
print (layer)
dir("${env.WORKSPACE}/layers/layer-name/$layer"){
step([
$class: 'CxScanBuilder',
comment: 'Layer scanning',
credentialsId: 'XXXX',
excludeFolders: 'test',
exclusionsSetting: 'global',
failBuildOnNewResults: false,
failBuildOnNewSeverity: 'MEDIUM',
filterPattern: '''!**/_cvs/**/*, !Checkmarx/Reports/*.*''',
fullScanCycle: 10,
incremental: true,
fullScansScheduled: true,
generatePdfReport: true,
preset: '36',
teamPath: "\\path\\to\\codebase",
projectName: "$layer",
sastEnabled: true,
sourceEncoding: '1',
vulnerabilityThresholdResult: 'FAILURE',
waitForResultsEnabled: true
])
}
}
}
}
}
Any suggestions on how to exclude the 'test' folder?
You should change your exclusionsSetting to 'job' instead of 'global', we can't override the global configurations.
Then you can add more filters in the filterPattern.
the filtering is really flakey - did you have any luck????
try add it to the filterPattern as !Test/*.* as well and play around with that...
Modify like below to exclude both target and test folders.
excludeFolders: 'target, test'
Jenkins console log:
[Cx-Debug]: Excluded Dir: src/test
[Cx-Debug]: Excluded Dir: target
If you are running on Windows you need to use the following pattern: !**\\test\\**\\*
On Linux: !**/test/**/*

jenkins pipline : How to get the list of file from workspace?

In a jenkinsfile i want to get the list of files in a directory in workspace and put them in parameters.
I tried with:
stage('select'){
def workspace = pwd()
files = []
new File ("$workspace/buildFile").eachFile(FileType.FILES) { files << it.name }
BuildFile = input( id: 'userInput', message: 'Sélectionner un backup', parameters: [ [$class: 'ChoiceParameterDefinition', choices: files , description: 'Properties', name: 'param'] ])
}
but i get message error "java.io.FileNotFoundException:"
The problem is that the pipeline scripts are executed on the master, so when you do new File(...) you create a file pointer on the master and not the slave/node/agent workspace context. Instead you should use findFiles which is available in the Pipeline Utility Steps plugin.

How to get svn version number from checkout for use in dsl

I created a pipeline job and would like to get the svn version number to enable further downstream processing in a call to a shell script. I am using a pipeline script similar to the following:
node {
// Mark the code checkout 'stage'....
stage 'Checkout'
// Get some code from a SVM repository
checkout(
[
$class: 'SubversionSCM',
additionalCredentials: [],
excludedCommitMessages: '',
excludedRegions: '',
excludedRevprop: '',
excludedUsers: '',
filterChangelog: false,
ignoreDirPropChanges: false,
includedRegions: '',
locations: [
[
...
]
],
workspaceUpdater: [$class: 'UpdateUpdater']
]
)
def svnversionnumber=${SVN_VERSION}
sh "/.../someshellscript ${svnversionnumber};"
}
Is there documentation on the checkout function available? Is it possible to get hold of the svn revision number? I can see that the revision is output to the log.
I had the same issue, but you can solve it by using the map that is returned from calling SCM checkout. It contains a value for SVN_REVISION.
// Get some code from a SVM repository
def scmVars = checkout(
...
)
def svnversionnumber = scmVars.SVN_REVISION
In Groovy pipeline script it's possible to get results of checkout scm command into TreeMap variable and then get what you need:
def checkoutResults = checkout([
poll: false,
scm: [
$class: 'SubversionSCM',
...
]
])
echo 'checkout results' + checkoutResults.toString()
echo 'checkout revision' + checkoutResults['SVN_REVISION']
echo 'checkout revision' + checkoutResults['SVN_REVISION_1']
echo 'checkout revision' + checkoutResults['SVN_REVISION_2']
I ended up invoking a shell to get the svn revision number as follows
def svnVersionNumber = sh(
script: "svn info --show-item last-changed-revision $url",
returnStdout: true
)
This was the only way I could get it to work correctly.
this code work for me in jenkins pipeline:
String url = 'svn+ssh:...'
SVN_REVISION_IN = sh returnStdout: true, script: 'svn info --show-item last-changed-revision ' + url
currentBuild.displayName = "Rev: ${SVN_REVISION_IN}"
There is a file called revision.txt in the build dir. The SubversionSCM provides methods to read this file.
//Here remote returns url#revision but the revision part is across the entire repo
//We will use the url part to get the revision for our branch
def remote = scm.locations.first().remote
def url = remote.split('#').first()
//The revision file has the revision for our branch. Parse returns a map.
def revmap = scm.parseRevisionFile(currentBuild.rawBuild)
revmap[url]
The scm variable is available on Jenkinsfiles. If you are not using a Jenkinsfile you should be able to create the scm object and pass it into the checkout method.
I think one of the best choice can be use a simple little "groovy console script" to get the revision number then put into a Jenkins variable..
Something like this to give you an idea:
Link
Take also a look at this question: Link

Resources