coursework/ansible/Jenkinsfile

84 lines
2.7 KiB
Plaintext
Raw Normal View History

2024-11-04 15:53:55 +00:00
pipeline {
agent any
environment {
VAULT_PASSWORD = credentials('vault_password')
}
2024-11-11 10:48:06 +00:00
environment {
ANSIBLE_HOST_KEY_CHECKING = 'false'
}
2024-11-04 15:53:55 +00:00
parameters {
choice(name: 'SITE_OPTION', choices: ['SiteA', 'SiteB', 'SiteA&B'], description: 'Select which site to deploy')
string(name: 'ADDITIONAL_CONTENT', defaultValue: 'Новое сообщение', description: 'Additional content to be included in the site')
}
stages {
stage('Decrypt SSH Key') {
steps {
script {
2024-11-04 16:49:57 +00:00
def tempDir = '/tmp/' + UUID.randomUUID().toString()
2024-11-04 15:53:55 +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 """
ansible-vault decrypt ./ansible/id_ed25519_vault --output=${decryptedKeyFile} --vault-password-file=${vaultPassFile}
"""
env.DECYPTED_KEY_FILE = decryptedKeyFile
2024-11-04 15:53:55 +00:00
}
}
}
stage('Deploy Site') {
steps {
script {
def sanitized_content = params.ADDITIONAL_CONTENT.replaceAll("'", "\\'").replaceAll('"', '\\"')
def siteOption = params.SITE_OPTION
def targetGroups = ''
if (siteOption == 'SiteA') {
2024-11-11 10:48:06 +00:00
targetGroups = 'SiteA,proxy'
} else if (siteOption == 'SiteB') {
2024-11-11 10:48:06 +00:00
targetGroups = 'SiteB,proxy'
} else if (siteOption == 'SiteA&B') {
targetGroups = 'SiteA,SiteB,proxy'
2024-11-04 15:53:55 +00:00
}
ansiblePlaybook(
2024-11-11 10:48:06 +00:00
playbook: 'ansible/playbooks/playbook.yml',
inventory: "ansible/inventory.yml",
extraVars: [
additional_content: sanitized_content,
ansible_ssh_private_key_file: env.DECYPTED_KEY_FILE
],
limit: targetGroups
)
2024-11-04 15:53:55 +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.'
}
}
}