Jenkinsfile conditions in Stage phase - bash

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
}
}

Related

Common tests are excluded in suite configuration of protractor

I could see the common tests are excluded in a suite configuration of protractor. Below is my config.js and there are two scenarios configured in suites.
I'm expecting the test to complete the scenario1 successfully and then Login again as part of scenario2. But, I could see the test ignores 'Login.js', 'CustomerSelection.js', 'Create.js' of Scenario2 and directly proceeds with 'ProductSelection.js'.
Any idea why is that so ? Am I missing anything in conf.js to work the way the scenarios configured ?
Config.js:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
capabilities: {
'browserName': 'chrome'
},
framework: 'jasmine' ,
showColors: true,
suites : {
scenario1: [
'Login.js',
'CustomerSelection.js',
'Create.js',
'View.js',
],
scenario2: [
'Login.js',
'CustomerSelection.js',
'Create.js',
'ProductSelection.js',
]
},
jasmineNodeOpts: {
isVerbose: true,
showColors: true,
print: function () {
},
includeStackTrace: true,
defaultTimeoutInterval: 700000
},
onPrepare: function() {
browser.manage().window().maximize();
browser.manage().timeouts().implicitlyWait(5000);
}
};
Below are the versions I'm using:
protractor: Version 5.4.0
Jasmine: Version 3.2.0
Node: v8.11.1
NPM: Version 5.6.0
If this tests are something that you run always before all. You can place them like this into View.js and ProductSelection.js as part of beforeAll, I'm placing login beforeAll (loginPage is page where my functions are placed, Login() is function in loginPage that logins in application if you send correct username and password to it) like this:
beforeAll(function() {
loginPage.Login(username, password);
});

Jenkins: Run Serenity acceptance tests without failure

I'm trying to achieve the following:
Run a set of Serenity (plus Cucumber) tests as part of a build pipeline
Collect the reports regardless of whether all tests passed or not (they are especially useful in failures obviously)
In the case of test failures only, then email the contributors
Never fail the build because of a failed acceptance test as this pipeline is for the commit CI. Only want to fail if there are broken acceptance tests in the Nightly.
So with all that in mind I set off attempting to configure the build:
stage ('Serenity') {
steps {
// For the Delivery CI build don't fail on regression failure
sh 'mvn clean verify -pl regression -DskipCuke=false'
}
post {
always {
publishHTML([allowMissing: true, alwaysLinkToLastBuild: true,
keepAll: true, reportDir: 'regression/target/site/serenity',
reportFiles: 'index.html', reportName: 'Serenity',
reportTitles: ''])
}
failure{
echo 'There are regression suite failures.'
script {
currentBuild.result = 'SUCCESS'
}
emailext attachLog: true, body: 'Find Attached',
compressLog: true, recipientProviders: [[$class:
'CulpritsRecipientProvider']], subject: 'Broken Regression Tests',
to: 'dennis#dennis.ru'
}
}
}
However it does not work as I cannot reset the value of currentBuild.result to 'SUCCESS'. So I could all || true to the mvncommand, but that would mean that I can't email about the broken regression tests.
So I am wondering if anyone else out their has dealt with this in some clever way. Do I need to assign an exit code or something, and would that involve overriding the default shell parameters in Jenkins?
Any help much appreciated.
I think you would need to put a try/catch around the shell (so run it in a script{} block), and do your email in the catch. Then you can keep the build set to SUCCESS.
I actually solved this in a slightly different manner to #Rob's suggestion, but the key to it was understanding that what I wanted to do needed to use the script block with the returnStatus flag. I prefer this to a try-catch, as I am actually expecting (unfortunately) this to fail from time to time, and so would prefer to branch this below.
stage ('Serenity') {
steps {
script{
// For the Delivery CI build don't fail on regression failure
def bddPassed = ( sh ( returnStatus:true, script:'mvn clean verify -pl regression -DskipCuke=false') == 0 )
if( !bddPassed ){
echo 'There are regression suite failures.'
def mySubject = "Regression Test Failure: ${env.JOB_NAME} - Build# ${env.BUILD_NUMBER}"
def myBody = "Hi<br/>Please go to <a href='${env.BUILD_URL}Serenity'>the Serenity Report</a> to see more<br/>";
emailext attachLog: true,
mimeType: 'text/html',
body: myBody,
compressLog: true,
recipientProviders: [[$class: 'CulpritsRecipientProvider']],
subject: mySubject,
to: 'xxxxxxx'
}
publishHTML([allowMissing: true, alwaysLinkToLastBuild: true,
keepAll: true, reportDir: 'regression/target/site/serenity', reportFiles: 'index.html',
reportName: 'Serenity', reportTitles: ''])
}
}
}

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
}
}

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.

Groovy SwingBuilder() apple.awt.CToolkit exception

I am using newest Mac OS X and I am creating a GUI element inside a Gradle file. I am currently using jdk1.7.0_55 and I have imported groovy.swing.SwingBuilder, when I run the project I am getting the following error:
java.awt.AWTError: "Toolkit not found: apple.awt.CToolkit
I have tried running the script as a headless server using System.setProperty('java.awt.headless', 'true')
I would like to have a solution that I can include directly in the Gradle project file, instead of trying to figure out what is in my accesibilities.properties file (that may not exist on a particular system, like it does not on my system).
Also the project must use an internal solution, external libraries are not allowed.
Would really appreciate any help on this matter.
Edited: Sample Code
gradle.taskGraph.whenReady { taskGraph ->
if(taskGraph.hasTask(':CustomApp:assembleRelease')) {
def pass = ''
if(System.console() == null) {
new SwingBuilder().edt { // Error occurs here.
dialog(modal: true,
alwaysOnTop: true,
resizable: false,
locationRelativeTo: null,
pack: true,
show: true
)
{
vbox {
label(text: "Enter password:")
input = passwordField()
button(defaultButton: true, text: 'OK', actionPerformed: {
pass = input.password;
dispose();
})
}
}
}
}
}
I've faced same issue with Android Studio 0.8.6 and solved it with custom gradle installation.
Just downloaded gradle 1.12 and set path to it in preferences.
The question is a few years old, but with the following gradle build file (which is essentially the same as the OPs):
import groovy.swing.SwingBuilder
task doit {}
gradle.taskGraph.whenReady { taskGraph ->
if(taskGraph.hasTask(doit)) {
def pass = ''
new SwingBuilder().edt { // Error occurs here.
dialog(modal: true,
alwaysOnTop: true,
resizable: false,
locationRelativeTo: null,
pack: true,
show: true)
{ vbox
{ label(text: "Enter password:")
input = passwordField()
button(defaultButton: true, text: 'OK', actionPerformed: {
pass = input.password;
dispose();
})
}
}
}
}
}
executing:
~> gradle doit
results in the following screen:
in other words, at least with this version of gradle, operating system, java etc this seems to work.

Resources