try { sh 'might fail' mail subject: 'all well', to: 'admin@somewhere', body: 'All well.'} catch (e) {def w =newStringWriter() e.printStackTrace(newPrintWriter(w)) mail subject: "failed with ${e.message}", to: 'admin@somewhere', body: "Failed: ${w}"throw e}
/** * check whether if the git refs exists in local repo or not * * @param name the git base references, can be branch name or revision or tag or pointer refs ( i.e. {@code HEAD}, {@code FETCH_HEAD}, ... )
* @param type the references type. can be {@code [ heads | tags | refs ]}. revision will be considered as {@code refs}
* @param dir the local repo location. using current directory by default * @param verbose whether or not to show debug information**/BooleanhasLocalReference( String name ,String type ='refs' ,String dir = pwd() ,Boolean verbose =true) { if ( ! [ 'heads', 'tags', 'refs' ].contains(type) ) println( "ERROR: invalid type ! available type : 'heads', 'tags' or 'refs'" )
if ( verbose ) println ( "~~> check whether if ${type} : '${name}' exists in local repository : '${dir}'" )String refs = [ 'heads', 'tags' ].contains(type) ?"refs/${type}/${name}": name sh ( returnStatus : true , script : """ set -x ; [ -d "${dir}" ] && \ git -C "${dir}" rev-parse --is-inside-work-tree >/dev/null 2>&1 && \ git -C "${dir}" cat-file -e ${refs} """ ) ==0}
or via show-ref
/** * check whether if the branch exists in local repository * * @param name the branch name will be checked * @param type to using {@code refs/${type}} to check branch or tags in local * @param dir the local repo location. using current directory by default * @param verbose whether or not to show debug information**/BooleanhasLocalBranch ( String name ,String type ,String dir = pwd() ,Boolean verbose =true) { if ( ! [ 'heads', 'tags' ].contains(type) ) util.showError( "ERROR: invalid type! available type : 'heads' or 'tags'" )
if ( verbose ) color.echo( LOGGER_COLOR, "~~> check whether if ${refName(type)} : '${name}' exists in local repository : '${dir}'" )
sh ( returnStatus : true , script : """ set +x; [ -d "${dir}" ] && \ git -C "${dir}" rev-parse --is-inside-work-tree >/dev/null 2>&1 && \ git -C '${dir}' show-ref --quiet refs/${type}/${name} """ ) ==0} // hasLocalBranch
Both `gitUsernamePassword` and `gitSshPrivateKey` bindings depend on the Credential Plugin to retrieve user’s credential using the Credentials API. Git SSH Private Key Binding The gitSshPrivateKey implementation provides git authentication support over SSH protocol using private key and passphrase credentials of a user. The binding uses two git specific environment variables depending upon the minimum CLI-git version - `GIT_SSH_COMMAND` - If version is greater than or equal to 2.3, then the GIT_SSH_COMMAND environment variable provides the ssh command including necessary options which are: path to the private key and host key checking, to authenticate and connect to the git server without using an executable script. - `SSH_ASKPASS` - If version is less than 2.3, an executable script is attached to the variable which provides the ssh command including necessary options which are: path to the private key and host key checking, to authenticate and connect to the git server
[!NOTE] If for any particular reason, the push must be done using a different method the URL needs to be configured accordingly:
git config url.git@github.com/.insteadOf https://github.com/ : if the checkout was done through HTTPS but push must be done using SSH
git config url.https://github.com/.insteadOf git@github.com/ : if the checkout was done through SSH but push must be done using HTTPS escaping characters in the ECHO commands of the askpass script:
importgroovy.io.FileTypeimport staticgroovy.io.FileType.*/** * Traverse the files or directories in the given path * * @param path the path to be traversed * @param filetype the type of files to be traversed. can be {@code [ files | directories | any ]} * @param depth the depth of the traversal. default is 1 * @return the list of files or directories in the given path**/@NonCPSdeftraverseInPath( String path, String filetype, Integer depth =1 ) {List<String> names = []if ( ! [ 'files', 'directories', 'any' ].contains(filetype) ) { currentBuild.description ="`filetype` support only ${[ 'files', 'directories', 'any' ].join(',')} !" currentBuild.result ='NOT_BUILT' currentBuild.getRawBuild().getExecutor().interrupt(Result.NOT_BUILT) }Closure sortByTypeThenName = { a, b -> a.isFile() != b.isFile() ? a.isFile() <=> b.isFile() : a.name.toLowerCase() <=> b.name.toLowerCase() }newFile(path).traverse( type : FileType.valueOf( filetype.toUpperCase() ), maxDepth : depth, sort : sortByTypeThenName ) { names << it }return names}
way to call
String path ='/path/to/folder'println ( ">> traverse all FILES/DIRECTORIES in ${path} in maxDepth 0 : " )println traverseInPath( path, 'any', 0 ).join('\n')println ( ">> traverse all FILES in ${path} in maxDepth 2 : " )println traverseInPath( path, 'files', 2 ).join('\n')