Add key authentication for SSH in JenkinsFile
authorKrzysztof Wieclaw/IoT & UI Sample (PLT) /SRPOL/Engineer/Samsung Electronics <k.wieclaw@samsung.com>
Fri, 5 Feb 2021 14:52:56 +0000 (15:52 +0100)
committerLukasz Stanislawski <l.stanislaws@samsung.com>
Thu, 11 Feb 2021 09:59:30 +0000 (09:59 +0000)
Change-Id: I3dbeff2a4cf871e5231ffe48d12a53985689ab10
Signed-off-by: Krzysztof Wieclaw/IoT & UI Sample (PLT) /SRPOL/Engineer/Samsung Electronics <k.wieclaw@samsung.com>
Jenkinsfile

index 86e4538..25a94c9 100644 (file)
@@ -52,7 +52,8 @@ pipeline {
             script {
               def return_code = sh(script: "dotnet format --check --report dotnet-format-report ${SOLUTION_PATH}", returnStatus: true)
               DOTNET_FORMAT_RESULT_CODE = return_code
-              withCredentials([usernamePassword(credentialsId: 'http_server_password', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
+
+              withCredentials([sshUserPrivateKey(credentialsId: 'http_server_ssh', keyFileVariable: 'SSH_KEY', passphraseVariable: 'PASSPHRASE', usernameVariable: 'USERNAME')]) {
                 def user_host = "${USERNAME}@${HTTP_SERVER}"
                 def www_path = ""
                 if(BRANCH_NAME == "tizen") {
@@ -62,11 +63,14 @@ pipeline {
                   www_path = "${HTTP_DOTNET_FORMAT_PATH}/${PROJECT_NAME}_dotnet_format_output/${BRANCH_NAME}/${GIT_COMMIT_REV}"
                 }
                 def path = "~/www/${www_path}"
+                def KEY_FILE = SSH_KEY
+                //sh "cat > ${KEY_FILE} <<EOF\n${SSH_KEY}\nEOF"
+                sh "chmod 400 ${KEY_FILE}"
                 if(BRANCH_NAME == "tizen") {
-                  sh "ssh -o StrictHostKeychecking=no ${user_host} rm -rf \"${path}\""
+                  sh "ssh -i ${KEY_FILE} -o StrictHostKeychecking=no ${user_host} rm -rf \"${path}\""
                 }
-                sh "ssh -o StrictHostKeychecking=no ${user_host} mkdir -p \"${path}\""
-                sh "scp ${SOLUTION_DIR}/dotnet-format-report/format-report.json \"${user_host}:${path}\""
+                sh "ssh -i ${KEY_FILE} -o StrictHostKeychecking=no ${user_host} mkdir -p \"${path}\""
+                sh "scp -i ${KEY_FILE} ${SOLUTION_DIR}/dotnet-format-report/format-report.json \"${user_host}:${path}\""
                 DOTNET_FORMAT_RESULT_URL="${HTTP_BASE_URL_PATH}/${www_path}/format-report.json"
               }
             }
@@ -95,7 +99,7 @@ pipeline {
                 sh "echo Clang format exited with unknown error"
                 currentBuild.result = 'FAILURE'
               }
-              withCredentials([usernamePassword(credentialsId: 'http_server_password', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
+              withCredentials([sshUserPrivateKey(credentialsId: 'http_server_ssh', keyFileVariable: 'SSH_KEY', passphraseVariable: 'PASSPHRASE', usernameVariable: 'USERNAME')]) {
                 def user_host = "${USERNAME}@${HTTP_SERVER}"
                 def www_path = ""
                 if(BRANCH_NAME == "tizen") {
@@ -105,11 +109,14 @@ pipeline {
                   www_path = "${HTTP_CLANG_FORMAT_PATH}/${PROJECT_NAME}_clang_format_output/${BRANCH_NAME}/${GIT_COMMIT_REV}"
                 }
                 def path = "~/www/${www_path}"
+                def KEY_FILE = SSH_KEY
+                //sh "cat > ${KEY_FILE} <<EOF\n${SSH_KEY}\nEOF"
+                sh "chmod 400 ${KEY_FILE}"
                 if(BRANCH_NAME == "tizen") {
-                  sh "ssh -o StrictHostKeychecking=no ${user_host} rm -rf \"${path}\""
+                  sh "ssh -i ${KEY_FILE} -o StrictHostKeychecking=no ${user_host} rm -rf \"${path}\""
                 }
-                sh "ssh -o StrictHostKeychecking=no ${user_host} mkdir -p \"${path}\""
-                sh "scp clang_format.diff \"${user_host}:${path}\""
+                sh "ssh -i ${KEY_FILE} -o StrictHostKeychecking=no ${user_host} mkdir -p \"${path}\""
+                sh "scp -i ${KEY_FILE} clang_format.diff \"${user_host}:${path}\""
                 CLANG_RESULT_URL="${HTTP_BASE_URL_PATH}/${www_path}/clang_format.diff"
               }
             }
@@ -124,7 +131,7 @@ pipeline {
               archiveArtifacts("${PROJECT_NAME}_SAM.zip")
               sh "cp output/html/*.html output/html/report.html"
               sh "cp output/html/*.png output/html/result.png"
-              withCredentials([usernamePassword(credentialsId: 'http_server_password', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
+              withCredentials([sshUserPrivateKey(credentialsId: 'http_server_ssh', keyFileVariable: 'SSH_KEY', passphraseVariable: 'PASSPHRASE', usernameVariable: 'USERNAME')]) {
                 def user_host = "${USERNAME}@${HTTP_SERVER}"
                 def www_path = ""
                 if(BRANCH_NAME == "tizen") {
@@ -134,12 +141,15 @@ pipeline {
                   www_path = "${HTTP_SAM_PATH}/${PROJECT_NAME}_SAM_output/${BRANCH_NAME}/${GIT_COMMIT_REV}"
                 }
                 def path = "~/www/${www_path}"
+                def KEY_FILE = SSH_KEY
+                //sh "cat > ${KEY_FILE} <<EOF\n${SSH_KEY}\nEOF"
+                sh "chmod 400 ${KEY_FILE}"
                 if(BRANCH_NAME == "tizen") {
-                  sh "ssh -o StrictHostKeychecking=no ${user_host} rm -rf \"${path}\""
+                  sh "ssh -i ${KEY_FILE} -o StrictHostKeychecking=no ${user_host} rm -rf \"${path}\""
                 }
-                sh "ssh -o StrictHostKeychecking=no ${user_host} mkdir -p \"${path}\""
-                sh "scp -r output \"${user_host}:${path}\""
-                sh "scp ${PROJECT_NAME}_SAM.zip \"${user_host}:${path}\""
+                sh "ssh -i ${KEY_FILE} -o StrictHostKeychecking=no ${user_host} mkdir -p \"${path}\""
+                sh "scp -i ${KEY_FILE} -r output \"${user_host}:${path}\""
+                sh "scp -i ${KEY_FILE} ${PROJECT_NAME}_SAM.zip \"${user_host}:${path}\""
                 SAM_RESULT_URL="${HTTP_BASE_URL_PATH}/${www_path}/output/html/report.html"
               }
             }