From 911665d384a57aacf8dd20d2bcb712a923269fd5 Mon Sep 17 00:00:00 2001 From: Samuel Rubin Date: Mon, 15 Apr 2019 10:32:51 -0400 Subject: [PATCH] Adding git HTTPS support Change the credentials parameter the GIT_CREDENTIALS instead of SSH_CREDENTAILS It now checks the git repo for https:// or git@ to determine if it is https or ssh Fixed #3 Signed-off-by: Samuel Rubin --- Jenkins_jobs/GetInventoryList.groovy | 36 +++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/Jenkins_jobs/GetInventoryList.groovy b/Jenkins_jobs/GetInventoryList.groovy index bb0b859..c02594e 100644 --- a/Jenkins_jobs/GetInventoryList.groovy +++ b/Jenkins_jobs/GetInventoryList.groovy @@ -17,7 +17,7 @@ Parameters: SETUP_LABEL: String - the label of the node that this job runs on. The default is worker LABEL: String - the label of the machines that this job will look for SLACK_CHANNEL: String - What channel a list of problematic machines gets sent to. If this is empty, no slack message will be sent -SSH_CREDENTIALS: Credentials - The credentials needed to push the files to git +GIT_CREDENTIALS: Credentials - The credentials needed to push the files to git GIT_REPO: String - The git repo to push the files to. If this is not set, the files will be archived to Jenkins GIT_BRANCH: String - The git branch to push the files to. It defaults to master INI_FILE: String - full path to the ini file including the filename @@ -67,16 +67,36 @@ node(setupLabel){ if(params.GIT_REPO && params.GIT_BRANCH){ def date = new Date() dir('git_repo'){ - git url: params.GIT_REPO, branch: params.GIT_BRANCH, credentials: params.SSH_CREDENTIALS + git url: params.GIT_REPO, branch: params.GIT_BRANCH, credentials: params.GIT_CREDENTIALS outputFiles.each() { file -> sh "cp ${workspace}/${file} ${file}" } - sshagent([params.SSH_CREDENTIALS]) { - sh """\ - git add ${outputFiles.join(' ')} - git commit -m "This File has been updated on ${date} from Jenkins" - git push origin ${params.GIT_BRANCH} - """ + if (params.GIT_REPO.contains('git@')){ + sshagent([params.GIT_CREDENTIALS]) { + sh """\ + git add ${outputFiles.join(' ')} + git commit -m "This File has been updated on ${date} from Jenkins" + git push origin ${params.GIT_BRANCH} + """ + } + } else if (params.GIT_REPO.contains('https://')){ + withCredentials([usernamePassword(credentialsId: params.GIT_CREDENTIALS, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) { + def GIT_PUSH_URL = 'https://' + GIT_USERNAME + ':' + GIT_PASSWORD + '@' + (params.GIT_REPO - 'https://') + + sh """\ + git add ${outputFiles.join(' ')} + if output=$(git status --porcelain --untracked-files=no) && [ -z "$output" ] + then + echo 'No changes found...' + else + echo 'Updates found' + git commit -m "This File has been updated on ${date} from Jenkins" + git push ${GIT_PUSH_URL} ${params.GIT_BRANCH} + fi + """ + } + } else { + error "There was a problem with the git repo. It does not contain 'https://' or 'git@'" } } }