Advertisement
jimklimov

Sample structure for optional pipeline steps

Nov 25th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 2.56 KB | None | 0 0
  1. #!groovy
  2.  
  3. /* Sample structure for optional parametrized DDSL pipeline steps
  4.  * Copyright (C) 2016 by Jim Klimov on the terms of MIT license
  5.  */
  6. pipeline {
  7.     agent label:"myproject-builder"
  8.     parameters {
  9.         booleanParam(defaultValue: false, description: 'Removes workspace completely before checkout and build', name: 'action_DistcleanRebuild')
  10.         booleanParam(defaultValue: false, description: 'Wipes untracked files from workspace before checkout and build', name: 'action_GitcleanRebuild')
  11.         booleanParam(defaultValue: true,  description: 'Run Git to checkout or update the project sources', name: 'action_DoSCM')
  12. /* other boolean toggles and/or string parameter definitions for prep, build, check, publish, etc. stages and other behaviors */
  13.     }
  14.     stages {
  15.         stage("WORKSPACE:DESTROY") {
  16.             when {
  17.                 if (params["action_DistcleanRebuild"] == true) {
  18.                     if (fileExists(file: "${env.WORKSPACE}/.git/config")) {
  19.                         return true;
  20.                     }
  21.                 }
  22.                 return false;
  23.             }
  24.             steps {
  25.                 echo "Removing '${env.WORKSPACE}' at '${env.NODE_NAME}'"
  26.                 dir("${env.WORKSPACE}") {
  27.                     deleteDir()
  28.                 }
  29.             }
  30.         }
  31.         stage("WORKSPACE:GITWIPE") {
  32.             when {
  33.                 if (params["action_DistcleanRebuild"] == false && params["action_GitcleanRebuild"] == true) {
  34.                     if (fileExists(file: "${env.WORKSPACE}/.git/config")) {
  35.                         return true;
  36.                     }
  37.                 }
  38.                 return false;
  39.             }
  40.             steps {
  41.                 dir("${env.WORKSPACE}") {
  42.                     echo "Git-cleaning '${env.WORKSPACE}' at '${env.NODE_NAME}'"
  43.                     sh 'git checkout -f'
  44.                     sh 'git clean -d -ff -x'
  45.                 }
  46.             }
  47.             post {
  48.                 failure {
  49.                     echo "ERROR: Git clean failed in '${env.WORKSPACE}' at '${env.NODE_NAME}', removing workspace completely"
  50.                     dir("${env.WORKSPACE}") {
  51.                         deleteDir()
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.         stage("WORKSPACE:CHECKOUT") {
  57.             when {
  58.                 params["action_DoSCM"] == true
  59.             }
  60.             steps {
  61.                 sh "mkdir -p '${env.WORKSPACE}'"
  62.                 checkout scm
  63.             }
  64.         }
  65. /* other stages for prep, build, check, publish, etc. */
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement