Jenkins post build step as a Function - function

In Jenkins, I know I can do this...
pipeline {
agent any
stages {
stage('Demo') {
steps {
MyFunction()
}
}
}
}
void MyFunction() {
sh 'ls /'
}
In this situation, the function is within the pipeline, but then I can always extract MyFunction into a shared library for reuse across pipelines.
But would it be possible to do this with a post-build step?
In this case, I would like to convert this into a function and extract it into a library.
post {
always {
/* clean up our workspace */
deleteDir()
/* clean up tmp directory */
dir("${workspace}#tmp") {
deleteDir()
}
/* clean up script directory */
dir("${workspace}#script") {
deleteDir()
}
dir("${workspace}#2") {
deleteDir()
}
dir("${workspace}#2#tmp") {
deleteDir()
}
}
}
I've tried this
post {
always{
test()
}
}
}
With Function
void test() {
{
/* clean up our workspace */
deleteDir()
/* clean up tmp directory */
dir("${workspace}#tmp") {
deleteDir()
}
/* clean up script directory */
dir("${workspace}#script") {
deleteDir()
}
dir("${workspace}#2") {
deleteDir()
}
dir("${workspace}#2#tmp") {
deleteDir()
}
}
}
But it doesn't seem to work.
Is this possible at all or am I just missing something really obvious?

Passing the name of the workspace as a parameter in the function will solve your issue. The below script works.
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
post{
always{
echo "In : ${env.WORKSPACE}"
test(env.WORKSPACE)
}
}
}
void test(workspace){
echo "In test : " + workspace
deleteDir()
dir("${workspace}#tmp") {
deleteDir()
}
}
Also, instead of calling deleteDir() for multiple tmp directories, if you call deleteDir() only once, then it will delete the workspace as well as tmp directories

The way that works for us to clean up the workspace after a specific stage and without trying to guess the folder name is to make your post function in stage:
pipeline {
agent any
stages {
stage('1') {
steps {
echo 'Hello World'
}
post {
cleanup {
script {
// Workspace Cleanup plugin
cleanWs deleteDirs: true,
notFailBuild: true,
cleanWhenAborted: true,
cleanWhenFailure: true,
cleanWhenSuccess: true
}
}
}
}
}
}
We use WorkspaceCleanup plugin.

Related

Can i call an another function from my function in Jenkins pipeline?

I have two separate functions in my Jenkins pipeline and i want to call 1st function from 2nd function.
I tried following code.
def first(){
return{
stages{
stage("test"){
steps{
echo "ok"
}
}
}
}
}
def second(){
return{
first().call()
}
}
pipeline {
agent any
stages{
stage("Run"){
steps{
script{
second().call()
}
}
}
}
}
is this possible or not. suggest me right way.
Yes, you can. Your Jenkinsfile will look like this:
def first(){
stage("test"){
println "executing first"
}
}
def second(){
println("calling first from second")
first()
}
pipeline {
agent any
stages{
stage("Run"){
steps{
second()
}
}
}
}

Gradle Kotlin Build Script unable to get SpringBoot Lib

i tried few way on my build script but still unsuccessful to get the springboot lib in to my jar, may i know what's wrong with my script as below ?
Parent Gradle Kotlin DSL
import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
extra["kotlin.version"] = "1.3.31"
plugins {
java
idea
id("org.springframework.boot") version "2.1.4.RELEASE" apply false
kotlin("jvm") version "1.3.31" apply false
kotlin("plugin.spring") version "1.3.31" apply false
}
subprojects {
apply(plugin = "org.jetbrains.kotlin.jvm")
apply(plugin = "org.jetbrains.kotlin.plugin.spring")
apply(plugin = "io.spring.dependency-management")
the<DependencyManagementExtension>().apply {
imports {
mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
}
}
group = "com.company.market"
version = System.getenv("VERSION") ?: "0.2-SNAPSHOT"
java {
sourceCompatibility = JavaVersion.VERSION_1_8
}
repositories {
maven {
credentials {
username = "anonymous"
password = "anonymous"
}
url = uri("http://repo1.maven.apache.org/maven2/")
}
}
dependencies {
implementation(kotlin("reflect"))
implementation(kotlin("stdlib-jdk8"))
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
tasks.getByName<KotlinCompile>("compileKotlin") {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
tasks.getByName<KotlinCompile>("compileTestKotlin") {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
}
Child (API) Gradle Kotlin DSL
Here are the child module that will used for deployment
plugins {
`java`
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.security.oauth:spring-security-oauth2:2.3.3.RELEASE")
implementation("org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.4.RELEASE")
implementation("org.springframework.boot:spring-boot-devtools")
implementation("org.apache.commons:commons-dbcp2")
implementation("org.mariadb.jdbc:mariadb-java-client")
implementation("org.springframework.boot:spring-boot-loader:2.1.4.RELEASE")
implementation(project(":app-expr"))
implementation(project(":app-data"))
}
i can success build the api module but jar size is small and realize the lib were not build inside the jar.
i added the springboot lib in my sub proj and solved
plugins {
`java`
id("org.springframework.boot")
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.security.oauth:spring-security-oauth2:2.3.3.RELEASE")
implementation("org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.4.RELEASE")
implementation("org.springframework.boot:spring-boot-devtools")
implementation("org.apache.commons:commons-dbcp2")
implementation("org.mariadb.jdbc:mariadb-java-client")
implementation("org.springframework.boot:spring-boot-loader:2.1.4.RELEASE")
implementation(project(":expr"))
implementation(project(":data"))
}

use modifier $addToSet, $push or $pull with feathersjs and NeDB

How can I use the modifiers $addToSet, $pull or $push with a NeDB adapter for feathersjs? Following did not work and I cannot find anything in the documentation.
service('projects').update("<id>", { $addToSet: { assignedIds: "<newId>" } });
service('projects').patch("<id>", { $addToSet: { assignedIds: "<newId>" } });
service('projects').update("<id>", { query: { $addToSet: { assignedIds: "<newId>" } } });
I also tried to put the operator in the params like this (as stated in the document)
service('projects').update("<id>", { }, {
query: { $addToSet: { assignedIds: "<newId>" } }
});
But the only thing I got back is unknown logical operator $addToSet
As per comment, this was a bug in featherjs that got patched

Purifycss not deleting unused css class

I have this code trying to delete the class
hello3:
var purify = require('purify-css');
var content = '<div class="hello"></div><div class="hello2"></div>';
var css = '.hello { color: green; } .hello3 { display: block; }';
var options = {
output: 'purified.css',
// Will minify CSS code in addition to purify.
minify: true,
// Logs out removed selectors.
rejected: true
};
purify(content, css, options);
The output in purified.css is the same as the variable css:
.hello { color: green; } .hello3 { display: block; }
How to solve it?
I ran a test and confirmed that purify-css doesn't like class names that contain numbers.
My command... purifycss css/main.css page1.html --out css/purified.css --info --rejected took my main.css file and incompletely purified it into:
.page1-h1 {
color: red;
}
.page2-h1 {
color: blue;
}
This included an unused class (.page2-h1). But when I renamed my class names so that there were no number characters in it and then ran the same command again got the main.css that I expected which contained only:
.pageone-hone {
color: red;
}
This seems to be a known problem too.

store and retrieve game state using HTML5 DOM storage and JSON

I am using helper functions to store and retrieve game state using HTML5 DOM storage and the JSON features built into ECMAScript5, my code is:
function saveState(state) {
window.localStorage.setItem("gameState",state);
}
function restoreState() {
var state = window.localStorage.getItem("gameState");
if (state) {
return parse(state);
} else {
return null;
}
}
but anyhow I am not getting desired output, as i am new to JSON its hard to resolve. HELP please !
Try below code:
function saveState(state) {
window.localStorage.setItem("gameState", JSON.stringify(state));
}
function restoreState() {
var state = window.localStorage.getItem("gameState");
if (state) {
return JSON.parse(state);
} else {
return null;
}
}