Jenkins - "zip" Pipeline Utility - how to exclude Multiple files? - jenkins

I want to build "zip" file and exclude multiple files: Jenkinsfile and test1.txt
I am able to exclude one specific file. This works:
stage ('First stage') {
steps {
zip zipFile: 'test.zip', archive: false, exclude: 'Jenkinsfile'
}
But how I can add to exclude test1.txt file as well?
I tried:
exclude: 'Jenkinsfile', 'test1.txt'
exclude: 'Jenkinsfile' 'test1.txt'
But it doesn't work!!!
Suggested solution on this link How to exclude Jenkinsfile and automation scripts in zip file pipeline utility plugin in Jenkins pipeline is NOT APPLICABLE!!!
Because exclude option is supported as explained on this link:
https://www.jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#zip-create-zip-file
Thank you!!!

Related

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

Copy key file to folder using Jenkingfile

I am using jenkins scripted file.
I have .key file stored in jenkins files ( Where all env files are present ).
And I need to copy that file to code folder.
Like i want to store device.key in src/auth/keys.
Then will run test on code in pipeline.
I am using scripted Jenkinsfile. And i am unable to find any way to this.
node{
def GIT_COMMIT_HASH
stage('Checkout Source Code and Logging Into Registry') {
echo 'Logging Into the Private ECR Registry'
checkout scm
sh "git rev-parse --short HEAD > .git/commit-id"
GIT_COMMIT_HASH = readFile('.git/commit-id').trim()
# NEED TO COPY device.key to /src/auth/key
}
stage('TEST'){
nodejs(nodeJSInstallationName:'node'){
sh 'npm install'
sh 'npm test'
}
}
}
How I solved this:
I installed Config File Provider Plugin
I added the files as custom files for each environment
In the JenkinsFile I replace the configuration file from the project with the one comming from jenkins:
stage('Add Config files') {
steps {
configFileProvider([configFile(fileId: 'ID-of-Jenkins-stored-file', targetLocation: 'relative-path-to-destination-file-in-the-project')]) {
// some block , maybe a friendly echo for debugging
} } }
Please see the plugin doc as it is capable of replacing tokens in XML and json files and many others.

Jenkinsfile two stages two testng results

I have one Jenkinsfile with two testing stages.
stage("test 1") {
dir("test 1") {
git url: "repo"
sh "${mvnHome}/bin/mvn -f pom.xml (...)"
git url: "repo"
sh "${mvnHome}/bin/mvn -f pom.xml (...)"
step([$class: 'Publisher', reportFilenamePattern: "${workspace}/(path)/testng-results-mod.xml"])
}
}
stage("test 2") {
dir("test_2") {
git url: "repo"
sh "${mvnHome}/bin/mvn -f pom.xml (...)"
git url: "repo"
sh "${mvnHome}/bin/mvn -f pom.xml (...)"
step([$class: 'Publisher', reportFilenamePattern: "${workspace}/(path)/testng-results-mod.xml"])
}
}
This is working fine. Both stages generates testng results file. But unfortunately i end up with just one xml result file, because first file is being overwriten by second test. Both test use same name.
There are two link in jenkins (build) to TestNG results, but they all lead to this same xml file. In build directory there is also just one file.
Is there any option to generate each test under different name? So they both will be avaliable in the end?
Ok. So after testing... it looks like you can have multiple xml files in output directory. TestNG pluging is displaying them as one report.
So Ive just added option to rename output file to _1.xml and _2.xml and it works.

Jenkins Pipeline Groovy MSBuild Artifacts

I am trying to build a Jenkins pipeline for my company to pull from Gitlab, do a MSBuild, Publish Artifacts to a directory and then zip up all the artifacts to push to a repository or file share. The application is a web app.
My issue occurs when I try to implement the zip function from AntBuilder. No matter what I specify for my basedir, the job fails saying that directory is not found. My code is as follows.
#!groovy​
node('dotnet') { //select any node with a doctnet label
stage('Checkout') { //checkout the code from source
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], gitTool: 'Windows_Gtt', submoduleCfg: [], userRemoteConfigs: [[credentialsId: '99999999-9999-9999-99999', url: 'git#repos.somecompany.com:api/sharedservices.git']]])
}
stage('Build') { //build steps go here
bat 'nuget restore LandingPageSvc.sln' //restore nuget sources
bat "\"${tool 'MSBUILD46_64_Pipeline'}\" LandingPageSvc.sln /m /p:VisualStudioVersion=14.0" //msbuild
}
// stage('NUnit Tests') { //Unit testing steps go here
// bat '"C:\\Program Files (x86)\\NUnit.org\\nunit-console\\nunit3-console.exe" ./LandingPageSvc.Test/bin/Debug/LandingPageSvc.Tests.dll --result nunit-result.xml'
// }
stage('Packaging') {
bat "\"${tool 'MSBUILD46_64_Pipeline'}\" LandingPageSvc\\LandingPageSvc.csproj /m /p:VisualStudioVersion=14.0;Configuration=Debug;PublishDestination=..\\Publish\\Dev /t:PublishToFileSystem"
def ant = new AntBuilder()
ant.zip(destfile: "test.zip", basedir: "./Publish/Dev")
}
stage('Publish') {
// build nuget package
//push nuget pcakage to artifactory
}
}
So, everything else resolves to the workspace on the dotnet build agent just fine and I have even tried using ${WORKSPACE} in the path and it still complains.
Not sure why or what context this particular function is running under, but it appears to not be on the dotnet build agent or in the project workspace.
Appreciate any suggestions.
Could it be that you are referencing two different file locations? In one you use ..\Publish\Dev and in the other you use ./Publish/Dev. I think the "." and the ".." are going to resolve to two different file locations. The ".." refers to the parent folder of the folder you are in whereas the "." refers to the folder you are in.
Jenkins/ProjectBasedir/Publish/Dev (.)
vs
Jenkins/Publish/Dev (..)

Jenkinfile DSL how to specify target directory

I'm exploring Jenkins 2.0 pipelines. So far my file is pretty simple.
node {
stage "checkout"
git([url:"https://github.com/luxengine/math.git"])
stage "build"
echo "Building from pipeline"
}
I can't seem to find any way to set the directory that git will checkout to. I also can't find any kind of documentation related to that. I found https://jenkinsci.github.io/job-dsl-plugin/ but it doesn't seem to match what I see on other tutorials.
Clarification
Looks like you are trying to configure Pipeline job (formerly known as Workflow). This type of job is very distinct from Job DSL.
The purpose of Pipeline job is to:
Orchestrates long-running activities that can span multiple build slaves. Suitable for building pipelines (formerly known as workflows) and/or organizing complex activities that do not easily fit in free-style job type.
Where as Job DSL:
...allows the programmatic creation of projects using a DSL. Pushing job creation into a script allows you to automate and standardize your Jenkins installation, unlike anything possible before.
Solution
If you want to checkout your code to specific directory then replace git step with more general SCM checkout step.
Final Pipeline configuration should look like that:
node {
stage "checkout"
//git([url:"https://github.com/luxengine/math.git"])
checkout([$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory',
relativeTargetDir: 'checkout-directory']],
submoduleCfg: [],
userRemoteConfigs: [[url: 'https://github.com/luxengine/math.git']]])
stage "build"
echo "Building from pipeline"
}
As a future reference for Jenkins 2.0 and Pipeline DSL please use built-in Snippet Generator or documentation.
This can be done by using the directive of dir:
def exists = fileExists '<your target dir>'
if (!exists){
new File('<your target dir>').mkdir()
}
dir ('<your target dir>') {
git url: '<your git repo address>'
}
First make clear that you are using Jenkins Job DSL.
You can do this like this:
scm {
git {
wipeOutWorkspace(true)
shallowClone(true);
remote {
url("xxxx....")
relativeTargetDir('checkout-folder')
}
}
}
https://jenkinsci.github.io/job-dsl-plugin/
This above address gives you the chance simply to type in upper left aread for example 'scm' and than it will show in which relationships 'scm' can be used. Than you can select 'scm-freestylejob' and afterwards click on the '***' than you can see the details.
The general start point for Jenkins Job DSL is here:
https://github.com/jenkinsci/job-dsl-plugin/wiki
You can of course ask here on SO or on Google Forum:
https://groups.google.com/forum/#!forum/job-dsl-plugin
pipeline {
agent any
stages{
stage("Checkout") {
steps {
dir('def exists = fileNotExists \'git\'') {
bat label: '', script: 'sh "mkdir.sh'
}
dir ('cm') {
git branch: 'dev',
credentialsId: '<your credential id>',
url: '<yours git url>'
}
}
} //End of Checkout stage
stage("TestShellScript") {
steps {
bat label: '', script: 'sh "PrintNumber.sh"'
}
}
}//End of stages
} // End of pipeline
Note: cat mkdir.sh
#!/bin/bash
#Create a directory
mkdir git
You are using the Pipeline Plugin, not the Job DSL Plugin. In the Pipeline Plugin, if you want to define something, where there is not yet a function available in the Pipeline syntax, you can define it yourself.

Resources