From 87e3a85c2c7d70ee9940cc2ab036fa24fafb0157 Mon Sep 17 00:00:00 2001 From: Gregory Hendrickson Date: Fri, 9 Jun 2023 15:46:21 -0700 Subject: [PATCH] with cred --- Jenkinsfile | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 80154d5..48060db 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,9 +2,8 @@ pipeline { agent any environment { DOCKERHUB_CREDENTIALS=credentials('dockerhub-cred') - SSH_CREDENTIALS=credentials('ssh-cred') } - + stages { stage('Build') { steps { @@ -23,33 +22,37 @@ pipeline { sh 'docker push aisthanestha/docker-test-image:latest' } } - + stage('Pull and Deploy') { steps { - script { - def remote = [:] - remote.name = 'ubuntu-kc' - remote.host = '172.16.11.90' - remote.user = "greg" - remote.password = "Password1!" - remote.allowAnyHosts = true - - writeFile file: 'run-pull-deploy.sh', text: ''' - docker pull aisthanestha/docker-test-image:latest - docker stop docker-test-image - docker rm docker-test-image - docker run -d --name docker-test-image -p 8082:80 aisthanestha/docker-test-image:latest - ''' - - // Transfer the script file to the remote host - sshPut remote: remote, from: 'run-pull-deploy.sh', into: 'run-pull-deploy.sh' + withCredentials([ + usernamePassword(credentialsId: 'ssh-cred', usernameVariable: 'SSH_USER', passwordVariable: 'SSH_PASSWORD') + ]) { + script { + def remote = [:] + remote.name = 'ubuntu-kc' + remote.host = '172.16.11.90' + remote.user = "${SSH_USER}" + remote.password = "${SSH_PASSWORD}" + remote.allowAnyHosts = true - // Execute the script file on the remote host - sshCommand remote: remote, command: 'chmod +x run-pull-deploy.sh && ./run-pull-deploy.sh' - } + writeFile file: 'run-pull-deploy.sh', text: ''' + docker pull aisthanestha/docker-test-image:latest + docker stop docker-test-image + docker rm docker-test-image + docker run -d --name docker-test-image -p 8082:80 aisthanestha/docker-test-image:latest + ''' + + // Transfer the script file to the remote host + sshPut remote: remote, from: 'run-pull-deploy.sh', into: 'run-pull-deploy.sh' + + // Execute the script file on the remote host + sshCommand remote: remote, command: 'chmod +x run-pull-deploy.sh && ./run-pull-deploy.sh' + } } } } + } post { always {