Jenkinsfile is not taking the build job parameter from job - jenkins

I don't why Jenkinsfile is not taking the parameterized value from the job. I have this parameter $BUILD_UI.
Jenkinsfile:
stages {
stage("Prepare") {
when {
expression { return $BUILD_UI == 'true' }
}
steps {
checkout([$class: 'GitSCM',
branches: [[name: '$UI_BRANCH_NAME' ]],
extensions: [[$class: 'CleanCheckout'],
[$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false],
[$class: 'RelativeTargetDirectory',
relativeTargetDir: 'defender-ui/']],
userRemoteConfigs: [[
url: 'git#bitbucket.org:xxx/xxxxx.git',
credentialsId: 'xxxxxx-xxxxx-xxxx-xxxx'
]]
])
}
}
But I keep getting this error. This is not working return $BUILD_UI == 'true' :
groovy.lang.MissingPropertyException: No such property: $BUILD_UI for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:270)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:291)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:295)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:271)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:271)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:271)
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:13)

You must remove the $ from the variable name, so it will read:
return BUILD_UI == 'true'
This is because you are accessing the variable directly on groovy, without needing to expand it

Related

Reading .json file from Workspace in Jenkins with Groovy script

I want to read a .json file in the stage Prepare Artifacts, which is there in workspace.
How can I read the workspace file path in the staging groovy and run the code?
The below code I used:
checkout scm: [
$class: 'GitSCM',
branches: [[name: "FETCH_HEAD"]],
extensions: [
[$class: 'RelativeTargetDirectory',
relativeTargetDir: repo2],
[$class: 'WipeWorkspace'],
[$class: 'CloneOption',
depth: 1,
noTags: true,
reference: '',
shallow: true,
honorRefspec: true],
[$class: 'CheckoutOption',
timeout: 30]],
gitTool: 'Default',
submoduleCfg: [],
userRemoteConfigs: [
[credentialsId: 'JENKINS_LOGIN',
]
]
]
def releasePackages = readJSON file: "./BuildAutomation/Jenkins/Pipeline//files/release_package.json"
println releasePackages
}
}
}
stage('Prepare Variable') {
steps {
script{
for(file in releasePackages[buildMod]['India']) {
bat("xcopy ..\\CALReleaseOutput\\${file} ..\\${IndiaReleaseOutputFolder}\\${file} /I /E /Y")
}
for(file in releasePackages[buildMod]['Russia']) {
bat("xcopy ..\\CALReleaseOutput\\${file} ..\\${RussiaReleaseOutputFolder}\\${file} /I /E /Y")
}
zip archive: false, dir: "..\\${b2bReleaseOutputFolder}", glob: '', zipFile: "..\\CALReleaseOutput_${tagFoldername}_B2B.zip"
}
}
}
}
}
}
}
When I run above one, I got error message as below
Console Output
If you are going to use variables between stages you have to define them as Global variables. Hence try defining releasePackages outside the pipeline. Following is an example.
def releasePackages
pipeline {
agent any
stages {
stage('SetVariable') {
steps {
script {
releasePackages = readJSON file: "./BuildAutomation/Jenkins/Pipeline//files/release_package.json"
echo "$releasePackages"
}
}
}
stage('UseVariable') {
steps {
echo "$releasePackages"
}
}
}
}

restarting one stage in jenkins pipeline wiping out existing directory

I am using jenkins declarative pipeline jenkinsfile for our project. we want to try the option restart at stage.
pipeline {
agent { label 'worker' }
stages {
stage('clean directory') {
steps {
cleanWs()
}
}
stage('checkout') {
steps {
checkout([$class: 'GitSCM', branches: [[name: 'develop']], extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: devops], [$class: 'LocalBranch', localBranch: "**"]], userRemoteConfigs: [[credentialsId: 'xxxxxx', url: git#github.com/test/devops.git]]])
checkout([$class: 'GitSCM', branches: [[name: 'develop']], extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: harness], [$class: 'LocalBranch', localBranch: "**"]], userRemoteConfigs: [[credentialsId: 'xxxxxx', url: git#github.com/test/harness.git]]])
checkout([$class: 'GitSCM', branches: [[name: 'develop']], extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: automation], [$class: 'LocalBranch', localBranch: "**"]], userRemoteConfigs: [[credentialsId: 'xxxxxx', url: git#github.com/test/automation.git]]])
}
}
stage('build initial commit to release train') {
steps {
sh '''#!/bin/bash
export TASK="build_initial_commit"
cd automation
sh main.sh
'''
}
}
stage('deploy application') {
steps {
sh '''#!/bin/bashexport TASK="deploy"
cd automation
sh main.sh
'''
}
}
}
}
and in jenkins I am using 'Pipeline script from SCM'. Jenkinsfile is present in automation.git repo (which is also defined in checkout stage)
Whenever I am restarting stage from GUI from 3rd one .. the workspace directory automatically gets cleaned up and it checksout automation.git ..
and the run fails as the other cloned repos were got cleaned...
how to handle this.. I want to restart the stage without wiping out the workspace dir..
if we just want to run the 3rd step 'deploy application' ..
I am not able to do , as the step depends on all 3 repos.. and
while restarting only 3rd stage the workspace is getting wiped out.. and as checkout is done in 1st stage(skipped) ... job is failing
how do I run only 3rd stage with retaining the old workspace ..
How about this:
SHOULD_CLEAN = true
pipeline {
agent { label 'worker' }
stages {
stage('clean directory') {
steps {
script {
if (SHOULD_CLEAN) {
cleanWs()
SHOULD_CLEAN = false
} else {
echo 'Skipping workspace clean'
}
}
}
}

Jenkins pipeline checkout based on parameter provided

I have Jenkins declarative pipeline and want to checkout a branch based on parameter provided
def envToBranch = [:]
envToBranch['dev'] = 'develop'
envToBranch['stg'] = 'stage'
envToBranch['prod'] = 'master'
pipeline {
parameters {
choice(name: 'ENV', choices: ['dev', 'stg', 'prod'], description: 'Application environment')
}
stages {
stage('Checkout') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: '<how-to-access-mapping-here>']],
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'SparseCheckoutPaths', sparseCheckoutPaths:[[$class:'SparseCheckoutPath', path:'ansible/']]]
],
submoduleCfg: [],
userRemoteConfigs: [
[credentialsId: 'my-creds',
url: 'git#github.com:MyOrg/my-repo.git']
]])
}
}
}
}
So how can I access a mapping within checkout directive? Am I able to do the same within script directive?

How to start jobs(parallel) from the main job with different inputs in jenkins?

I am using jenkins and having scripted syntax in jenkinsfile
In the main job after source checkout I need to run other job n times (parallel) with different inputs .
Any tips to start this?
def checkout(repo, branch) {
checkout(changelog: false,
poll: false,
scm: [$class : 'GitSCM',
branches : [[name: "*/${branch}"]],
doGenerateSubmoduleConfigurations: false,
recursiveSubmodules : true,
extensions : [[$class: 'LocalBranch', localBranch: "${branch}"]],
submoduleCfg : [], userRemoteConfigs: [[credentialsId: '', url: "${repo}"]]])
withCredentials([[$class : '',
credentialsId : '',
passwordVariable: '',
usernameVariable: '']]) {
sh "git clean -f && git reset --hard origin/${branch}"
}
}
node("jenkins02") {
stage('Checkout') {
checkout gitHubRepo, gitBranch
}
}
We do this by storing all the jobs we want to run in a Map and then pass it into the parallel step for execution. So you just setup the different params and add each definition into the map, then execute.
Map jobs = [:]
jobs.put('job-1', {
stage('job-1') {
node {
build(job: "myorg/job-1/master", parameters: [new StringParameterValue('PARAM_NAME','VAL1')], propagate: false)
}
}
})
jobs.put('job-2', {
stage('job-2') {
node {
build(job: "myorg/job-2/master", parameters: [new StringParameterValue('PARAM_NAME','VAL2')], propagate: false)
}
}
})
parallel(jobs)

SparseCheckout in Jenkinsfile pipeline

In a jenkinsfile, I have specified the folderName through SparseCheckoutPaths which I want to checkout. But I am getting a whole branch checkout instead.
checkout([$class: 'GitSCM',
branches: [[name: '*/branchName']],
extensions: [[$class: 'SparseCheckoutPaths', path: 'FolderName']],
userRemoteConfigs: [[credentialsId: 'someID',
url: 'git#link.git']]])
Here comes the answer to my own question. For a bit of background how does it work, there is flag/configuration for git client called sparsecheckout which is responsible for this kind of checkout. Additionally, a sparse-checkout named file is also required. For more info look here.
My problem was the syntax for the Jenkinsfile and correct one is as follows:
checkout([$class: 'GitSCM',
branches: [[name: '*/branchName']],
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'SparseCheckoutPaths', sparseCheckoutPaths:[[$class:'SparseCheckoutPath', path:'folderName/']]]
],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'someID',
url: 'git#link.git']]])
for more info, here comes the github-link
You can define a custom step sparseCheckout in a shared library that adds on top of the existing checkout scm.
vars/sparseCheckout.groovy:
def call(scm, files) {
if (scm.class.simpleName == 'GitSCM') {
def filesAsPaths = files.collect {
[path: it]
}
return checkout([$class : 'GitSCM',
branches : scm.branches,
doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
extensions : scm.extensions +
[[$class: 'SparseCheckoutPaths', sparseCheckoutPaths: filesAsPaths]],
submoduleCfg : scm.submoduleCfg,
userRemoteConfigs : scm.userRemoteConfigs
])
} else {
// fallback to checkout everything by default
return checkout(scm)
}
}
Then you call it with:
sparseCheckout(scm, ['path/to/file.xml', 'path2])
Your syntax looks good, but, as seen in "jenkinsci/plugins/gitclient/CliGitAPIImpl.java", did you specify the right configuration?
private void sparseCheckout(#NonNull List<String> paths) throws GitException, InterruptedException {
boolean coreSparseCheckoutConfigEnable;
try {
coreSparseCheckoutConfigEnable = launchCommand("config", "core.sparsecheckout").contains("true");
} catch (GitException ge) {
coreSparseCheckoutConfigEnable = false;
}
In other words, is git config core.sparsecheckout equal to true in the repo you are about to checkout?

Resources