Jenkins Menu Programming - hqzhang/cloudtestbed GitHub Wiki

Overall

brawse and upload a local file

stashedFile( name: 'FILE', description: 'brawse file'
 
withFileParameter('FILE') {
    sh """
         echo "upload:${FILE_FILENAME}"
         cp ${FILE} ${FILE_FILENAME}
         ls -al $FILE_FILENAME
         scp -i ~/.ssh/id_rsa $FILE_FILENAME [email protected]:~/workspace
      """
}

Simple Choice with Default value

choice( name: 'Environment', description: '', choices: buildDefault(envList,'BAT') ),
String buildDefault(List out,String dft){
   def ret=[]
   out.each{ if(it.contains(dft)){ ret.add(0, it) } else { ret.add(it) } }
   if (ret.isEmpty()) {return ['NotFound']}
   return ret
}

Branch List Choice with Default value

[$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', filterLength: 1, filterable: false,
         name: 'getBranchList', randomName: 'choice-parameter-16677189047440', referencedParameters: 'Environment', 
         script: [$class: 'GroovyScript', fallbackScript: [classpath: [], oldScript: '', sandbox: true, script: ''], 
         script: [classpath: [], oldScript: '', sandbox: true, 
          script: getBrancgetBranchScript(repo, dft) ]]],

String getBranchScript(String repo, String dft ){
   return """def ret=[]
   |def out = "git ls-remote --heads ${repo}".execute().text
   |out = out.readLines().collect { it.split()[1].replaceAll('refs/heads/', '') }
   |out.each{ if( it.contains(\"${dft}\") ){ ret.add(0,it) } else { ret.add(it) } }
   |return ret
   | """.stripMargin()
}

File List Choice from Github

[$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', filterLength: 1, filterable: false,
         name: 'getFileHubScript', randomName: 'choice-parameter-16677189047450', referencedParameters: 'Environment', 
         script: [$class: 'GroovyScript', fallbackScript: [classpath: [], oldScript: '', sandbox: true, script: ''], 
         script: [classpath: [], oldScript: '', sandbox: true, 
         script: getFileHubInstant("hqzhang/groovytest","release",'mytest') ]]],

String getFileHubInstant(String repo, String folder,String brch ){
   return """import groovy.json.JsonSlurper
   |def ret=[]
   |def out = "curl https://api.github.com/repos/${repo}/git/trees/${brch}?recursive=2".execute().text
   |def obj = new JsonSlurper().parseText(out)
   |obj['tree'].each{ if(it['path'].contains("${folder}/") ) { ret.add(it['path'])}  }
   |if (ret.isEmpty()) {return ['NotFound']}
   |return ret
   | """.stripMargin()
}

File List Choice from Bitbucket

    [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', filterLength: 1, filterable: false,
               name: 'getFileBitScript', randomName: 'choice-parameter-16677189047460', referencedParameters: 'Environment', 
               script: [$class: 'GroovyScript', fallbackScript: [classpath: [], oldScript: '', sandbox: true, script: ''], 
               script: [classpath: [], oldScript: '', sandbox: true, 
               script: getFileBitScript("wave-cloud/groovytest","release",brch) ]]],

def getFileBitScript(String repo, String folder, String brch){
    return """import groovy.json.JsonSlurper
    |def ret=[]
    |def repoPR="https://api.bitbucket.org/2.0/repositories/${repo}/src/${brch}/release"
    |def out="curl --request GET \${repoPR} ".execute().text
    |def obj=new JsonSlurper().parseText(out)
    |obj['values'].each { ret.add(it['path'])}
    |if (ret.isEmpty()) {return ['NotFound']}
    |return ret
    | """.stripMargin()
}

Multiple choice with Map reference

    [$class: 'CascadeChoiceParameter', choiceType: 'PT_MULTI_SELECT', filterLength: 1, filterable: false,
             name: 'Server', randomName: 'choice-parameter-16677189047472', referencedParameters: 'Environment', 
             script: [$class: 'GroovyScript', fallbackScript: [classpath: [], oldScript: '', sandbox: true, script: ''], 
             script: [classpath: [], oldScript: '', sandbox: true, 
             script: getServerList('Environment') ]]],

String getServerList(String refvar){
    def map=[:]
    envList.eachWithIndex{ it,index->
       map[it]=setDefault(serversList[it],defaultList[index])
    }
    return """def map=${map}
    |return map[${refvar}]
    |""".stripMargin()

File List from remote

  [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', filterLength: 1, filterable: false,
           name: 'Package', randomName: 'choice-parameter-16677189047489', referencedParameters: 'filebackup', 
           script: [$class: 'GroovyScript', fallbackScript: [classpath: [], oldScript: '', sandbox: true, script: ''], 
           script: [classpath: [], oldScript: '', sandbox: true, 
           script: getPackInstant('~/workspace/myscripts','myfile') ]]],

def getPackInstant(String mypath, String dft){
  return """ret=[]
  |def out="ssh ${target} ls ${mypath}/*.tar.gz".execute().text
  |out.readLines().each{ if( it.contains(\"${dft}\") ){ ret.add(0,it) } else { ret.add(it) } }
  |return ret
  |""".stripMargin()
}

File List from BitBucket

    [ $class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', filterLength: 1, filterable: false,
               name: 'SolutionConfig', randomName: 'choice-parameter-16677189047798', referencedParameters: 'filebackup', 
               script: [$class: 'GroovyScript', fallbackScript: [classpath: [], oldScript: '', sandbox: true, script: ''], 
               script: [classpath: [], oldScript: '', sandbox: true, 
               script: menu.getFileBitInstant(repo, brch, folder)  ]]],

def getFileBitInstant(){
    return """import groovy.json.JsonSlurper
    |def ret=[]
    |def branch="git ls-remote https://[email protected]/${repos}.git -b ${brch}".execute().text.substring(0,40)
    |def out="curl https://api.bitbucket.org/2.0/repositories/${repos}/src/\${branch}/${folder}".execute().text
    |def obj=new JsonSlurper().parseText(out)
    |obj['values'].each { ret.add(it['path'])}
    |if (ret.isEmpty()) {return ['NotFound']}
    |return ret
    | """.stripMargin()
}

File Content Show with reference

    [$class: 'DynamicReferenceParameter', choiceType: 'ET_FORMATTED_HTML', omitValueField: false, 
            name: 'Component', randomName: 'choice-parameter-138673186839723', referencedParameters: 'SolutionConfig', 
            script: [$class: 'GroovyScript', fallbackScript: [classpath: [], oldScript: '', sandbox: false, script: ''],
            script: [classpath: [], oldScript: '', sandbox: true, 
            script: menu.getContentInstant('SolutionConfig')]] ],

def getContentScript(String wksp, String refvar,String brch ){
    return """
    |def out="curl https://bitbucket.org/wave-cloud/groovytest/raw/${brch}/\${${refvar}}".execute().text
    |out=out.replaceAll('components:\\n','')
    |return \"\"\" <textarea name=\"value\"  value  class=\"setting-input  \" type=\"text\" rows="10" cols="25">\${out}</textarea> \"\"\"
    | """.stripMargin()
}

File Content Show with reference

    [$class: 'DynamicReferenceParameter', choiceType: 'ET_FORMATTED_HTML', omitValueField: false, 
             name: 'Components', randomName: 'choice-parameter-138673186839723', referencedParameters: 'SolutionConfig', 
             script: [$class: 'GroovyScript', fallbackScript: [classpath: [], oldScript: '', sandbox: false, script: ''],
             script: [classpath: [], oldScript: '', sandbox: true, 
             script: menu.getContentTable('SolutionConfig' )]] ],

def getContentTable(String refvar){
    def urlext=""
    def repo=getRepo()
    def brch=getBranch()
    return """import org.yaml.snakeyaml.Yaml
    |def wksp=\"${wksp}\"
    |def url=\"${url}\"
    |def ret=''
    |ret="curl -k https://raw.githubusercontent.com/${repo}/${brch}/release/\${${refvar}}${urlext}".execute().text
    |ret=(Map)new Yaml().load(ret)
    |def rendered = "<table><tr>"
    |ret['components'].each { mark="-"; 
    | it.each { kk,vv->
    |  if ( kk != "name") {  mark="&nbsp;&nbsp;" }
    |  rendered = \"\"\"\${rendered}<tr>
    |  <td><input name=\"value\" alt=\"\${kk}\" json=\"\${kk}\" type=\"checkbox\" style=\"opacity:0\" class=\" \" checked>
    |  <span>\${mark}&nbsp;</span>
    |  <label name=\"value\" class=\" \" value=\"\${kk}\">\${kk}</label></td>
    |  <td><input  type=\"text\" class=\" \" name=\"value\" value=\"\${vv}\"> </br> </td></tr> \"\"\"    } }
    |return "\${rendered}</tr></table>"
    |""".stripMargin()
}

String with Dynamic Default

    [$class: 'DynamicReferenceParameter', choiceType: 'ET_FORMATTED_HTML',  omitValueField: false, 
          name: 'filebackup',randomName: 'choice-parameter-138673186839723', referencedParameters: 'SolutionConfig', 
          script: [$class: 'GroovyScript', fallbackScript: [classpath: [], oldScript: '', sandbox: false, script: ''],
          script: [classpath: [], oldScript: '', sandbox: false, 
          script: menu.getString('SolutionConfig')]] ],

def getString(String refvar){
    return """return \"\"\" <textarea name=\"value\"  value  class=\"setting-input  \" type=\"text\" rows="1" cols="1">\${${refvar}}</textarea> \"\"\" """
}
⚠️ **GitHub.com Fallback** ⚠️