pipline script that i can’t seem to understand - jenkins-pipeline

i have a declarative pipeline that was causing me some problems lately. i put my hand on the block of script that was the source of the problem but since i’m new to jenkins i can’t figure out what it does exactly and here is the block :
checkout changelog: true, poll: true, scm: [
$class: 'GitSCM',
branches: [[name: "origin/${env.gitlabSourceBranch}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'PreBuildMerge', options: [fastForwardMode: 'NO_FF', mergeRemote: 'origin', mergeStrategy: 'DEFAULT', mergeTarget: "${env.gitlabTargetBranch}"]]],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: "${env.GIT_REPO_CREDENTIAL_ID}",name: 'origin', url: pipelineParams.scmUrl ]]
]
can someone please help me figure out what this does exactly when i run it through a jenkins build ?
P.S : this block is inside of a try/catch

Related

How to refer to the parameters in groovy pipeline script?

I merged this as vars/gitCheckout.goovy add this as library into the jenkins
def call(String branch = '*/master') {
checkout([$class: 'GitSCM',
branches: [[name: ${branch}]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: false,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false]],
submoduleCfg: [],
userRemoteConfigs: [[url: 'https://my-server.com/some/project.git']]])
}
Calling this method as below from jenkins Pipeline Script:
#Library('jenkins-library#master') _
pipeline {
agent { label 'my-server' }
stages {
stage('Git Checkout') {
steps {
gitCheckout()
}
}
}
}
This fails with error java.lang.NoSuchMethodError: No such DSL method '$' found among steps [ArtifactoryGradleBuild, MavenDescriptorStep, ....
I tried $branch, params.branch, but it didn't work, the code otherwise works if I don't use parameter and hardcode the branch name. Also, whenever I make any update to this .groovy script, should I test it by merging and running it as jenkins job? is there any other way to test before merging the groovy script ?
Replace ${branch} in the 3rd line with just branch. You use $ with a variable name when you e.g. interpolate variables inside Groovy strings:
def value = "current branch is: ${branch}" // produces: current branch is */master
If you forgot to use $ in string interpolation, nothing would happen:
def value = "current branch is: branch" // produces: current branch is branch

Jenkins Pipeline Choose Specific Branch but take from default(master) branch

I have a Jenkins Pipeline that I would like to have a user input on to checkout a specific branch of their choosing. i.e. If I create a branch 'dev' and commit it in git,but Jenkins take a default branch(master)
Can any one please help me take a code from 'dev' branch code
Thanks much in advance.
stage('Git Checkout') {
steps {
checkout(
[$class: 'GitSCM',
branches: [[name: '*/dev']],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId:'987654322234245676543',
url:'http://repo.xyz.com/user/devop.git']]]
)
}
}
You can try pipeline step: git
stage('Git Checkout') {
steps {
git(branch: 'dev',
credentialsId: '987654322234245676543',
url: "http://repo.xyz.com/user/devop.git")
}
}

WebStorm does not recognize `browser`, `element` etc in Protractor test spec

Following Protractor guide I wanted to create my first test. While the test works unfortunately JetBrains WebStorm does not recognize all of my variables in given test
I have enabled in Libraries/JavaScript:
jasmine
karma
karma-jasmine
HTML
Node.js Core
selenium-webdriver
As seen above Node.js Core library is enabled.
I have also visited this question but unfortunately the angular-protractor is no longer available.
What am I missing?
Your editor will understand it if its imported. Elese it will know where to find browser ot by
Add import statement at top of your file.
import {by, element} from 'protractor';
Use JS Hint RC. It will work like magic.
You can find this by going to
Settings -> Languages and Frameworks -> Javascript(select ECMA Script 6) ->Code Quality Tools- >JS Hint - Enable, use config file.
As for config file, save the bellow file, with following name: '.jshintrc'.
Rate the answer as positive if this worked for you!
{
"jasmine": true,
"mocha": true,
"esversion":6,
"loopfunc": true,
"node": true,
"globals": {
"esversion": 6,
"angular": false,
"browser": false,
"inject": false,
"_": false,
"driver": false,
"protractor": false,
"$": false,
"$$": false,
"element": false,
"by": false,
"list": false
}
}

Jenkinsfile conditions in Stage phase

should it possible to define several conditions in a Stage phase of Jenkins file??
Let me to explain my problem: I want to publishHtml in my Jenkins pipeline only if a condition is valid:
stage(Documentation)
{
agent any
steps
{
sh"./documents.sh " //This generate the documentation of several modules
if(firstModule) { //Is it possible to publishHTML only if documentation of this module was generated???
publishHTML (target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: "${env.WORKSPACE}/firstModule//html",
reportFiles: 'index.html',
reportName: "Fist Module Documentation"
])
}
if(secondModule) { //Is it possible to publishHTML only if documentation of this module was generated???
publishHTML (target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: "${env.WORKSPACE}/secondModule/html",
reportFiles: 'index.html',
reportName: "Second Modul Documentation"
])
}
}
I don't know if firstModule was built or not... this is what I would like to know.
I don't want to create different stages, but just one "Documentation" visible in the jenkins view.
Thank for your suggestion.
Prisco
it sounds like you want to publish documentation for firstModule only if "${env.WORKSPACE}/firstModule/html/index.html exists.
instead of:
if(firstModule) {
try:
script {
if (fileExists("${env.WORKSPACE}/firstModule/html/index.html")) {
# publishHTML and whatnot
}
}

Wildcard for directory name in Jenkinsfile using publishHTML not recognized

In my Jenkinsfile I use publishHTML to publish report for PIT. My step is as follows
stage('Results') {
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false,
keepAll: false, reportDir: 'target/pit-reports/*/', reportFiles: 'index.html', reportName: 'PIT Report'])
}
The directory of the index.html is something like this \target\pit-reports\201612081633. That last part 201612081633 is of course different every time. Using target/pit-reports/*/ on a windows machine results in the following error.
ERROR: Specified HTML directory 'D:\David\Tools\Jenkins\workspace\jenkinsSandbox\target\pit-reports\*' does not exist.
The wildcard * or ** does not work. How can I use a wildcard for a directory name in the jenkinsfile and is there any difference when doing this on windows or unix?
I solved this using a workaround because the publishHTML plugin seems to be unable to handle * or **. I now run pitest with -DtimestampedReports=false org.pitest:pitest-maven:mutationCoverage, which disables the timestamped folders. The result step now looks like this.
stage('Results') {
publishHTML([allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: 'target/pit-reports',
reportFiles: 'index.html',
reportName: 'PIT Report'
])
}
For those who are interested, the full version of my Jenkinsfile can be found on my github repo.

Resources