pipeline {
    agent any
        stages {
            stage('Clear current Branch') {
                steps {
                    echo "## Clearing - ${JOB_NAME} ##"
                    sh "ssh -o StrictHostKeyChecking=no laliga@webt.dyb-tech.com 'rm -rf /opt/srv/LaLiga-BackEnd/${BRANCH_NAME}'"
                }
            }
            stage('Create path') {
                steps {
                    echo "## Creating path in remote server ${BRANCH_NAME} ##"
                    sh "ssh -o StrictHostKeyChecking=no laliga@webt.dyb-tech.com 'mkdir -p /opt/srv/LaLiga-BackEnd/${BRANCH_NAME}'"
                }
            }
            stage('Deploy') {
                agent any
                steps {
                    echo "## Deploying into ${BRANCH_NAME} ##"
                    sh "scp  -o StrictHostKeyChecking=no -rp ./. laliga@webt.dyb-tech.com:/opt/srv/LaLiga-BackEnd/${BRANCH_NAME}"
                }
            }
            stage('Update packages'){
                agent any
                steps {
                    echo "## Updating packages - composer on ${BRANCH_NAME}"
                    sh "ssh -o StrictHostKeyChecking=no laliga@webt.dyb-tech.com '/opt/srv/LaLiga-BackEnd/scripts/after_deploy.sh ${BRANCH_NAME}'"
                }
            }
            stage('Add permissions') {
                agent any
                steps {
                    echo "## Fix permissions ${BRANCH_NAME} ##"
                    sh "ssh -o StrictHostKeyChecking=no laliga@webt.dyb-tech.com '/bin/bash /opt/srv/LaLiga-BackEnd/scripts/fix_perms.sh ${BRANCH_NAME}'"
                }
            }
       }
}