Git operations checkout and push
Change-Id: I503b0835186dfcf9a8dd7290ebb7cb1df55cb72a
diff --git a/src/com/mirantis/mk/Git.groovy b/src/com/mirantis/mk/Git.groovy
index 22ca53d..8d24471 100644
--- a/src/com/mirantis/mk/Git.groovy
+++ b/src/com/mirantis/mk/Git.groovy
@@ -48,20 +48,6 @@
}
/**
- * Get remote URL
- *
- * @param name Name of remote (default any)
- * @param type Type (fetch or push, default fetch)
- */
-def getGitRemote(name = '', type = 'fetch') {
- gitRemote = sh (
- script: "git remote -v | grep '${name}' | grep ${type} | awk '{print \$2}' | head -1",
- returnStdout: true
- ).trim()
- return gitRemote
-}
-
-/**
* Change actual working branch of repo
*
* @param path Path to the git repository
@@ -78,12 +64,44 @@
}
/**
+ * Get remote URL
+ *
+ * @param name Name of remote (default any)
+ * @param type Type (fetch or push, default fetch)
+ */
+def getGitRemote(name = '', type = 'fetch') {
+ gitRemote = sh (
+ script: "git remote -v | grep '${name}' | grep ${type} | awk '{print \$2}' | head -1",
+ returnStdout: true
+ ).trim()
+ return gitRemote
+}
+
+/**
+ * Create new working branch for repo
+ *
+ * @param path Path to the git repository
+ * @param branch Branch desired to switch to
+ */
+def createGitBranch(path, branch) {
+ def git_cmd
+ dir(path) {
+ git_cmd = sh (
+ script: "git checkout -b ${branch}",
+ returnStdout: true
+ ).trim()
+ }
+ return git_cmd
+}
+
+/**
* Commit changes to the git repo
*
* @param path Path to the git repository
* @param message A commit message
*/
def commitGitChanges(path, message) {
+ def git_cmd
dir(path) {
sh(
script: 'git add -A',
@@ -101,20 +119,27 @@
/**
* Push git changes to remote repo
*
- * @param path Path to the git repository
+ * @param path Path to the local git repository
* @param branch Branch on the remote git repository
* @param remote Name of the remote repository
+ * @param credentialsId Credentials with write permissions
*/
-def pushGitChanges(path, branch = 'master', remote = 'origin') {
+def pushGitChanges(path, branch = 'master', remote = 'origin', credentialsId = null) {
+ def ssh = new com.mirantis.mk.Ssh()
+ sh "git config --global user.email 'jenkins@root'"
+ sh "git config --global user.name 'jenkins-slave'"
dir(path) {
- git_cmd = sh(
- script: "git push ${remote} ${branch}",
- returnStdout: true
- ).trim()
+ if (credentialsId == null) {
+ sh script: "git push ${remote} ${branch}"
+ }
+ else {
+ ssh.prepareSshAgentKey(credentialsId)
+ ssh.runSshAgentCommand("git push ${remote} ${branch}")
+ }
}
- return git_cmd
}
+
/**
* Mirror git repository, merge target changes (downstream) on top of source
* (upstream) and push target or both if pushSource is true
diff --git a/src/com/mirantis/mk/Python.groovy b/src/com/mirantis/mk/Python.groovy
index c38347c..6e89563 100644
--- a/src/com/mirantis/mk/Python.groovy
+++ b/src/com/mirantis/mk/Python.groovy
@@ -226,14 +226,14 @@
*
* @param path Path where virtualenv is created
*/
-def buildCookiecutterTemplate (template, context, path = none) {
+def buildCookiecutterTemplate(template, context, outputDir = '.', path = none) {
contextFile = "default_context.json"
contextString = "parameters:\n"
for (parameter in context) {
contextString = "${contextString} ${parameter.key}: ${parameter.value}\n"
}
writeFile file: contextFile, text: contextString
- command = ". ./${work_dir}/bin/activate; cookiecutter --config-file ${cookiecutter_context_file} --overwrite-if-exists --verbose --no-input ${template_dir}"
+ command = ". ${path}/bin/activate; cookiecutter --config-file ${contextFile} --output-dir ${outputDir} --overwrite-if-exists --verbose --no-input ${template}"
output = sh (returnStdout: true, script: command)
echo("[Cookiecutter build] Output: ${output}")
}