courseworkrep/pipeline.groovy
2024-10-20 13:35:21 +03:00

56 lines
2.5 KiB
Groovy

pipeline {
agent any
parameters {
string(name: 'TARGET_IP', defaultValue: '192.168.0.72', description: 'IP адрес машины для установки PostgreSQL')
string(name: 'DB_USER', defaultValue: 'myuser', description: 'Имя пользователя для базы данных PostgreSQL')
string(name: 'DB_NAME', defaultValue: 'mydatabase', description: 'Имя базы данных PostgreSQL')
string(name: 'BACKUP_DIR', defaultValue: '/var/backups/postgresql', description: 'Путь для сохранения резервных копий')
}
environment {
ANSIBLE_HOST_KEY_CHECKING = 'False'
}
stages {
stage('Install PostgreSQL') {
steps {
withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD')]) {
script {
writeFile file: '/ansiblesql/hosts.ini', text: """
[postgres_servers]
${params.TARGET_IP} ansible_user=${SSH_USER} ansible_ssh_pass=${POSTGRES_PASSWORD} ansible_connection=ssh
"""
sh """
ansible-playbook -i /ansiblesql/hosts.ini /ansiblesql/install_postgresql.yml \\
-e postgres_user=${params.DB_USER} \\
-e postgres_password=${POSTGRES_PASSWORD} \\
-e postgres_db=${params.DB_NAME}
"""
}
}
}
}
stage('Setup PostgreSQL Backup with Cron') {
steps {
withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD')]) {
script {
sh """
ansible-playbook -i /ansiblesql/hosts.ini /ansiblesql/backup_postgresql.yml \\
-e postgres_user=${params.DB_USER} \\
-e postgres_password=${POSTGRES_PASSWORD} \\
-e postgres_db=${params.DB_NAME} \\
-e backup_dir=${params.BACKUP_DIR}
"""
}
}
}
}
}
}