courseworkrep/Jenkinsfile
2024-10-20 13:42:05 +03:00

98 lines
3.6 KiB
Groovy

pipeline {
agent any
environment {
SSH_ROOT_PASSWORD = credentials('ssh_root_password')
PSQL_PASSWORD = credentials('PSQL_pass')
ANSIBLE_HOST_KEY_CHECKING = 'False'
}
parameters {
string(name: 'DB_SERVER_IP', defaultValue: '192.168.0.71', description: 'IP-адрес сервера')
string(name: 'SSH_USER', defaultValue: 'root', description: 'Имя пользователя для SSH подключения')
string(name: 'DB_USER', defaultValue: 'postgres', description: 'Имя пользователя базы данных')
string(name: 'DB_NAME', defaultValue: 'mydb', description: 'Имя базы данных')
string(name: 'BACKUP_DIR', defaultValue: '/var/backups/postgresql', description: 'Директория для бэкапа')
}
stages {
stage('Clone repository') {
steps {
git branch: 'main', url: 'http://192.168.0.70:3000/coursework/courseworkrep.git'
}
}
stage('Install PostgreSQL') {
steps {
script {
def tempDir = sh(script: 'mktemp -d', returnStdout: true).trim()
def hostsFile = "${tempDir}/hosts.ini"
echo "Temporary directory created: ${tempDir}"
writeFile file: hostsFile, text: """
[postgres_servers]
${params.DB_SERVER_IP} ansible_user=${params.SSH_USER} ansible_password=${SSH_ROOT_PASSWORD} ansible_ssh_extra_args='-o StrictHostKeyChecking=no'
"""
sh "cat ${hostsFile}"
sh """
ansible-playbook -i ${hostsFile} install_postgresql.yml \
-e postgres_user=${params.DB_USER} \
-e postgres_password=${PSQL_PASSWORD} \
-e postgres_db=${params.DB_NAME}
"""
sh "rm -rf ${tempDir}"
echo "Temporary directory ${tempDir} has been removed"
}
}
}
stage('Setup PostgreSQL Backup with Cron') {
steps {
script {
def tempDir = sh(script: 'mktemp -d', returnStdout: true).trim()
def hostsFile = "${tempDir}/hosts.ini"
echo "Temporary directory created: ${tempDir}"
writeFile file: hostsFile, text: """
[postgres_servers]
${params.DB_SERVER_IP} ansible_user=${params.SSH_USER} ansible_password=${SSH_ROOT_PASSWORD} ansible_ssh_extra_args='-o StrictHostKeyChecking=no'
"""
sh "cat ${hostsFile}"
sh """
ansible-playbook -i ${hostsFile} backup_postgresql.yml \
-e postgres_user=${params.DB_USER} \
-e postgres_password=${PSQL_PASSWORD} \
-e postgres_db=${params.DB_NAME} \
-e backup_dir=${params.BACKUP_DIR}
"""
sh "rm -rf ${tempDir}"
echo "Temporary directory ${tempDir} has been removed"
}
}
}
}
post {
always {
echo "Pipeline completed"
}
failure {
echo "Pipeline failed"
}
success {
echo "Pipeline succeeded"
}
}
}