diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..5773cee --- /dev/null +++ b/build.gradle @@ -0,0 +1,46 @@ +apply plugin: 'groovy' + +group = 'tech.avroid' +version = '0.1.0' +description = 'Jenkins Shared Library' + +repositories { + mavenCentral() + maven { + url "https://repo.jenkins-ci.org/releases/" + } + maven { + url "https://repo.jenkins-ci.org/public/" + } +} + +dependencies { + implementation 'org.codehaus.groovy:groovy-all:3.0.2' + implementation 'com.cloudbees:groovy-cps:1.32' + implementation 'org.jenkins-ci.main:jenkins-core:2.164.1' +} + +sourceSets { + main { + groovy { + srcDirs = ['src', 'vars'] + } + } +} + +groovydoc { + // Set document and window titles. + docTitle = "Avroid Shared library" + windowTitle = "Avroid Shared library" + + header = '''\ + +

Sample project

+ '''.stripIndent() + + // Set custom footer for generated documentation. + footer = """\ + """.stripIndent() +} diff --git a/src/tech/avroid/scm/Git.groovy b/src/tech/avroid/scm/Git.groovy new file mode 100644 index 0000000..abd6907 --- /dev/null +++ b/src/tech/avroid/scm/Git.groovy @@ -0,0 +1,57 @@ +package tech.avroid.scm + +/** + * Class for working with Git + */ +class Git implements Serializable { + + private Script script + + Git(Script script) { + this.script = script + } + + /** + * Method to clone a Git repository + * @param args Map - Arguments for cloning + * - urlRepo: String - HTTP URL of the repository + * - branch: String - Branch name, default "main" + * - path: String - Directory to clone into, default "./" + * - disableSubmodules: Boolean - Disable submodules, default true + * - recursiveSubmodules: Boolean - Get submodules recursively, default true + * - parentCredentials: Boolean - Use parent repository credentials, default true + * - trackingSubmodules: Boolean - Use last commit in .gitmodules, default false + * - listFiles: Boolean - List cloned files, default false + */ + public clone(Map args = [:]) { + Map defaultArgs = [ + branch: 'main', + disableSubmodules: false, + recursiveSubmodules: true, + parentCredentials: true, + trackingSubmodules: false, + path: './', + listFiles: false + ] + + defaultArgs.each { k, v -> + if (args[k] == null || args[k] == '' || args[k] == []) { + args[k] = v + } + } + + script.checkout([$class: 'GitSCM', + branches: [[name: "${args.branch}"]], + extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: "${args.path}"], + [$class: 'SubmoduleOption', disableSubmodules: args.disableSubmodules, + recursiveSubmodules: args.recursiveSubmodules, + parentCredentials: args.parentCredentials, + trackingSubmodules: args.trackingSubmodules]], + userRemoteConfigs: [[url: "${args.urlRepo}"]] + ]) + + if (args.listFiles) { + this.script.sh "ls -la ${args.path}" + } + } +} diff --git a/src/tech/avroid/ssh/Ssh.groovy b/src/tech/avroid/ssh/Ssh.groovy new file mode 100644 index 0000000..e3c7ed2 --- /dev/null +++ b/src/tech/avroid/ssh/Ssh.groovy @@ -0,0 +1,46 @@ +package tech.avroid.ssh + +/** + * Class for work with SSH client + */ +class Ssh implements Serializable { + + private Script script + private String sshKey + private String fileSshKey + + Ssh(Script script, String sshKey, String fileSshKey) { + this.script = script + this.fileSshKey = fileSshKey + this.sshKey = sshKey + } + + /** + * Add file with private Ssh key and create ssh config with disable "StrictHostKeyChecking" for all hosts + * @param sshKey String - Variable with private SSH key + * @param fileSshKey String - name of file private SSH key's in catalog ~/.ssh + */ + public void configureSshClient() { + script.println 'Configure SSH-client with private key for current user' + script.sh """ + [ -d ~/.ssh ] || mkdir ~/.ssh && chmod 0700 ~/.ssh + cat $sshKey > ~/.ssh/$fileSshKey + chmod 600 ~/.ssh/$fileSshKey + echo 'Host *' > ~/.ssh/config + echo ' StrictHostKeyChecking no' >> ~/.ssh/config + echo ' UserKnownHostsFile=/dev/null' >> ~/.ssh/config + """ + } + + /** + * Remove file with private Ssh Key and ssh config + * @param fileSshKey String - name of file private SSH key's in catalog ~/.ssh + */ + public void cleanupSshConfiguration() { + script.println 'Unconfigure SSH-client with private key for current user' + script.sh """ + rm -f ~/.ssh/$fileSshKey + rm -f ~/.ssh/config + """ + } +}