diff --git a/infrastructure/jenkins/deploy-front.groovy b/infrastructure/jenkins/deploy-front.groovy new file mode 100644 index 0000000..e0a2094 --- /dev/null +++ b/infrastructure/jenkins/deploy-front.groovy @@ -0,0 +1,183 @@ +pipeline { + agent any + + parameters { + string( + name: 'BRANCH', + defaultValue: 'main', + description: 'Ветка для деплоя' + ) + } + + stages { + stage('Git pull') { + steps { + script { + checkout([ + $class: 'GitSCM', + branches: [[name: params.BRANCH]], + userRemoteConfigs: [[ + url: 'https://code.3err0.ru/frontdev/no-copy-frontend.git', + credentialsId: 'nx-jen' + ]] + ]) + } + } + } + + stage('Stop old') { + steps { + script { + withCredentials([ + usernamePassword( + credentialsId: 'server-root-password', + usernameVariable: 'SSH_USER', + passwordVariable: 'SSH_PASS' + ) + ]) { + sh """ + sshpass -p '$SSH_PASS' ssh $SSH_USER@92.242.61.23 " + docker stop no-copy-frontend 2>/dev/null || true + docker rm no-copy-frontend 2>/dev/null || true + + cd /opt/deployments/frontend/${params.BRANCH} 2>/dev/null || mkdir -p /opt/deployments/frontend/${params.BRANCH} + + if [ -f 'docker-compose.yaml' ]; then + docker-compose -f docker-compose.yaml down 2>/dev/null || true + elif [ -f 'docker-compose.yml' ]; then + docker-compose -f docker-compose.yml down 2>/dev/null || true + fi + " + """ + } + } + } + } + + stage('Copy to server') { + steps { + script { + withCredentials([ + usernamePassword( + credentialsId: 'server-root-password', + usernameVariable: 'SSH_USER', + passwordVariable: 'SSH_PASS' + ) + ]) { + sh """ + sshpass -p '$SSH_PASS' ssh $SSH_USER@92.242.61.23 " + rm -rf /opt/deployments/frontend/${params.BRANCH}/* + mkdir -p /opt/deployments/frontend/${params.BRANCH} + " + + sshpass -p '$SSH_PASS' scp -r ./* $SSH_USER@92.242.61.23:/opt/deployments/frontend/${params.BRANCH}/ + """ + } + } + } + } + + stage('Build on server') { + steps { + script { + withCredentials([ + usernamePassword( + credentialsId: 'server-root-password', + usernameVariable: 'SSH_USER', + passwordVariable: 'SSH_PASS' + ) + ]) { + sh """ + sshpass -p '$SSH_PASS' ssh $SSH_USER@92.242.61.23 " + cd /opt/deployments/frontend/${params.BRANCH} + + if [ -f 'docker-compose.yaml' ]; then + docker-compose -f docker-compose.yaml build + elif [ -f 'docker-compose.yml' ]; then + docker-compose -f docker-compose.yml build + else + echo 'Exception:don't found docker-compose' + exit 1 + fi + " + """ + } + } + } + } + + stage('Start on server') { + steps { + script { + withCredentials([ + usernamePassword( + credentialsId: 'server-root-password', + usernameVariable: 'SSH_USER', + passwordVariable: 'SSH_PASS' + ) + ]) { + sh """ + sshpass -p '$SSH_PASS' ssh $SSH_USER@92.242.61.23 " + cd /opt/deployments/frontend/${params.BRANCH} + + if [ -f 'docker-compose.yaml' ]; then + docker-compose -f docker-compose.yaml up -d + elif [ -f 'docker-compose.yml' ]; then + docker-compose -f docker-compose.yml up -d + fi + + sleep 10 + + echo 'Status container:' + docker ps --filter name=no-copy-frontend + " + """ + } + } + } + } + + stage('Check status') { + steps { + script { + withCredentials([ + usernamePassword( + credentialsId: 'server-root-password', + usernameVariable: 'SSH_USER', + passwordVariable: 'SSH_PASS' + ) + ]) { + sh """ + sshpass -p '$SSH_PASS' ssh $SSH_USER@92.242.61.23 " + if docker ps --format '{{.Names}}' | grep -q 'no-copy-frontend'; then + echo 'Container start' + + sleep 5 + + HTTP_CODE=\$(curl -s -o /dev/null -w '%{http_code}' http://localhost:2998 || echo '000') + echo 'HTTP код: ' \$HTTP_CODE + + echo 'Frong start on http://92.242.61.23:2998' + else + echo 'Exception: container don't start' + docker ps -a | grep frontend + exit 1 + fi + " + """ + } + } + } + } + } + + post { + success { + echo "Front branch ${params.BRANCH} completed" + echo "Use on http://92.242.61.23:2998" + } + failure { + echo "Failed" + } + } +} \ No newline at end of file