coursework/ansible/Jenkinsfile

124 lines
4.6 KiB
Plaintext
Raw Permalink Normal View History

2024-11-12 20:14:17 +00:00
pipeline {
agent any
2024-11-17 16:29:04 +00:00
options {
ansiColor('xterm')
}
2024-11-12 20:14:17 +00:00
environment {
ANSIBLE_HOST_KEY_CHECKING = 'false'
VAULT_PASSWORD = credentials('vault_password')
}
parameters {
2024-11-17 16:02:14 +00:00
choice(
name: 'DEPLOY_TARGET',
2024-11-29 14:23:59 +00:00
choices: ['SiteA', 'SiteB', 'proxy', 'SiteA&proxy', 'SiteB&proxy', 'All'],
description: 'Выберите цели для развертывания'
2024-11-17 16:02:14 +00:00
)
2024-11-17 16:19:57 +00:00
2024-11-29 14:23:59 +00:00
string(
2024-11-29 14:28:17 +00:00
name: 'ADDITIONAL_CONTENT_H2',
2024-11-29 14:23:59 +00:00
defaultValue: 'Новое сообщение',
2024-11-29 14:28:17 +00:00
description: 'Дополнительный <h2> контент'
)
string(
name: 'ADDITIONAL_CONTENT_P',
defaultValue: 'Дополнительный параграф',
description: 'Дополнительный <p> контент'
2024-11-29 14:23:59 +00:00
)
2024-11-12 20:14:17 +00:00
}
2024-11-12 20:43:48 +00:00
2024-11-12 20:14:17 +00:00
stages {
stage('Decrypt SSH Key') {
steps {
script {
2024-11-29 14:23:59 +00:00
def tempDir = "/tmp/${UUID.randomUUID().toString()}"
2024-11-12 20:14:17 +00:00
env.TEMP_DIR = tempDir
sh "mkdir -p ${tempDir}"
def decryptedKeyFile = "${tempDir}/id_ed25519"
def vaultPassFile = "${tempDir}/vault_pass"
writeFile file: vaultPassFile, text: VAULT_PASSWORD
sh """
2024-11-29 14:23:59 +00:00
ansible-vault decrypt ./ansible/id_ed25519_vault --output=${decryptedKeyFile} --vault-password-file=${vaultPassFile}
2024-11-12 20:14:17 +00:00
"""
env.DECYPTED_KEY_FILE = decryptedKeyFile
}
}
}
2024-11-12 21:10:18 +00:00
stage('Deploy Site') {
2024-11-12 20:14:17 +00:00
steps {
script {
2024-11-17 16:02:14 +00:00
def selectedHosts
2024-11-17 16:10:08 +00:00
def extraVars = [
ansible_ssh_private_key_file: env.DECYPTED_KEY_FILE
]
2024-11-17 16:02:14 +00:00
switch(params.DEPLOY_TARGET) {
case 'SiteA':
2024-11-29 14:34:32 +00:00
selectedHosts = 'SiteA'
2024-11-29 14:28:17 +00:00
extraVars.siteA_h2 = params.ADDITIONAL_CONTENT_H2
extraVars.siteA_p = params.ADDITIONAL_CONTENT_P
2024-11-17 16:02:14 +00:00
break
case 'SiteB':
2024-11-29 14:34:32 +00:00
selectedHosts = 'SiteB'
2024-11-29 14:28:17 +00:00
extraVars.siteB_h2 = params.ADDITIONAL_CONTENT_H2
extraVars.siteB_p = params.ADDITIONAL_CONTENT_P
2024-11-29 14:23:59 +00:00
break
case 'proxy':
selectedHosts = 'proxy'
break
case 'SiteA&proxy':
selectedHosts = 'SiteA,proxy'
2024-11-29 14:28:17 +00:00
extraVars.siteA_h2 = params.ADDITIONAL_CONTENT_H2
extraVars.siteA_p = params.ADDITIONAL_CONTENT_P
2024-11-29 14:23:59 +00:00
break
case 'SiteB&proxy':
2024-11-17 16:02:14 +00:00
selectedHosts = 'SiteB,proxy'
2024-11-29 14:28:17 +00:00
extraVars.siteB_h2 = params.ADDITIONAL_CONTENT_H2
extraVars.siteB_p = params.ADDITIONAL_CONTENT_P
2024-11-17 16:02:14 +00:00
break
case 'All':
selectedHosts = 'SiteA,SiteB,proxy'
2024-11-29 14:28:17 +00:00
extraVars.siteA_h2 = params.ADDITIONAL_CONTENT_H2
extraVars.siteA_p = params.ADDITIONAL_CONTENT_P
extraVars.siteB_h2 = params.ADDITIONAL_CONTENT_H2
extraVars.siteB_p = params.ADDITIONAL_CONTENT_P
2024-11-17 16:02:14 +00:00
break
default:
error "Неизвестный DEPLOY_TARGET: ${params.DEPLOY_TARGET}"
}
2024-11-12 20:14:17 +00:00
ansiblePlaybook(
playbook: 'ansible/playbook.yml',
inventory: "ansible/inventory.yml",
2024-11-12 20:43:48 +00:00
extraVars: extraVars,
2024-11-24 16:23:34 +00:00
limit: selectedHosts,
colorized: true
2024-11-12 20:14:17 +00:00
)
}
}
}
}
2024-11-12 20:43:48 +00:00
2024-11-12 20:14:17 +00:00
post {
always {
script {
if (env.TEMP_DIR) {
sh "rm -rf ${env.TEMP_DIR}"
}
}
}
success {
echo 'Deployment completed successfully.'
}
failure {
echo 'Deployment failed. Please check the logs for more details.'
}
}
2024-11-17 16:10:08 +00:00
}