From 4ff90c67c6d7de83a06a6635eeaed385d7c3c1fe Mon Sep 17 00:00:00 2001 From: Pavlov Makar Date: Sun, 8 Jun 2025 01:38:44 +0300 Subject: [PATCH] Refactor Jenkinsfile --- Jenkinsfile | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index cf14be9..aa18731 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -7,6 +7,8 @@ pipeline { artifactNumToKeepStr: '10', artifactDaysToKeepStr: '7' ) + ansiColor('xterm') + timestamps() } parameters { string(name: "target_host", defaultValue: "", trim: true, description: "Target host") @@ -31,6 +33,16 @@ pipeline { } } } + stage('Prepare inventory') { + steps { + script { + def hostsFile = new File("${WORKSPACE}/hosts.ini") + hostsFile.append('[all]') + hostsFile.append('\n' + params.target_host) + println hostsFile.getText('UTF-8') + } + } + } stage('Install Rproxy') { when { expression { @@ -44,7 +56,15 @@ pipeline { usernamePassword(credentialsId:'JENKINS_DEPLOYER_PASS', usernameVariable: 'username', passwordVariable: 'password') ]) { wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[password: env.password]]]) { - sh 'ansible-playbook rproxy.yml -i ${target_host}, -t install --private-key ${SSH_KEY} -u ${username} -e "ansible_password=${password} rproxy_service_name=${rproxy_service_name} rproxy_service_port=${rproxy_service_port} rproxy_service_address=${rproxy_service_address}"' + wrap([$class: 'AnsiColorBuildWrapper', colorMapName: "xterm"]) { + ansiblePlaybook( + playbook: 'rproxy.yml', + inventory: 'hosts.ini', + tags: 'install', + colorized: true, + extras: '--private-key ${SSH_KEY} -e "ansible_user=${username} ansible_password=${password} rproxy_service_name=${rproxy_service_name} rproxy_service_port=${rproxy_service_port} rproxy_service_address=${rproxy_service_address}"' + ) + } } } } @@ -63,11 +83,26 @@ pipeline { usernamePassword(credentialsId:'JENKINS_DEPLOYER_PASS', usernameVariable: 'username', passwordVariable: 'password') ]) { wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[password: env.password]]]) { - sh 'ansible-playbook rproxy.yml -i ${target_host}, -t add_config --private-key ${SSH_KEY} -u ${username} -e "ansible_password=${password} rproxy_service_name=${rproxy_service_name} rproxy_service_port=${rproxy_service_port} rproxy_service_address=${rproxy_service_address}"' + wrap([$class: 'AnsiColorBuildWrapper', colorMapName: "xterm"]) { + ansiblePlaybook( + playbook: 'rproxy.yml', + inventory: 'hosts.ini', + tags: 'add_config', + colorized: true, + extras: '--private-key ${SSH_KEY} -e "ansible_user=${username} ansible_password=${password} rproxy_service_name=${rproxy_service_name} rproxy_service_port=${rproxy_service_port} rproxy_service_address=${rproxy_service_address}"' + ) + } } } } } } } + post { + always { + cleanWs(patterns: [ + [pattern: 'hosts.ini', type: 'INCLUDE'] + ]) + } + } } \ No newline at end of file