Deploy automático

 avatar
unknown
sh
2 years ago
3.1 kB
13
Indexable
#!/bin/bash

#
# repo - folder com clone.
# source - folder com o clone (ui).
# target - folder intermédio para verificações de testes e compilação.
# production - folder de produção, onde o sistema está a correr.
#
repo_path="/root/RobDroneGo/SEM5_PI"
repo_ui_path="/root/RobDroneGo/SEM5_PI/ui2/"
source_path="/root/RobDroneGo/SEM5_PI/ui2"
production_path="/root/production/SEM5_PI/"
production_ui_path="/root/production/SEM5_PI/ui2/"

# Token de autenticação do GitHub.
access_token="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"

# Caminho para ficheiro de logs.
audit_log="/root/audit_log.txt"

# Função para formatar e escrever logs no ficheiro.
log_message() {
    local timestamp=$(date +"[%d-%m-%Y %T] - ")
    echo "$timestamp $1" >> "$audit_log"
}

# Redireciona todo o output para o audit_log.
exec >> "$audit_log" 2>&1

cd "$repo_path" || exit

# Verifica se o repo local está para trás na branch.
if [ "$(git fetch && git status -uno | grep 'Your branch is behind')" > /dev/null ]; then

    git pull origin main > /dev/null

    diff --exclude=node_modules -r  --exclude=.angular "$repo_ui_path" "$production_ui_path" > /dev/null

    # Compara o source com production (excluindo os node_modules).
    if  [ "$?" -gt 0 ]; then
        log_message "There are no updates on frontend."
        exit -1
    fi

    # Executa os testes de unidade.
    npm run test > /dev/null

    if [ "$?" -gt 0 ]; then
        log_message "Unit tests failed."
        exit -1
    fi

    # Executa teste de compilação.
    #/root/compile_test.sh > /dev/null

    #if [ $? -eq -1 ]; then
#       log_message "Compilation error."
#    fi

    cd "$production_path" || exit

    # Faz o pull na produção.
    git pull origin main > /dev/null

    # Procura o PID que está a executar o frontend.
    pid=$(ps aux | grep "ng serve --host 0.0.0.0" | grep -v grep | awk '{print $2}')

    if [ "$pid" -ne 1 ]; then
        # Termina o processo.
        kill -SIGKILL $pid
    fi

    cd "$production_ui_path" || exit

    /usr/local/bin/ng serve --host 0.0.0.0 & > /dev/null

    log_message "New version deployed successfully."
    exit 0

    # Corre script de teste de compilação.
#    if /root/compile_test.sh > /dev/null; then

        # Se existirem diferenças entre o target e a produção, é feita a atualização.
        #if diff -r "$repo_path" "$production_path" > /dev/null; then
        #    log_message "No updates on front-end module"
        #    exit -1
        #else
        #    /usr/bin/npm run test > /dev/null
        #    if [ $? -eq 0 ]; then
        #        ps aux | grep "ng serve"
#               kill -SIGKILL $?
        #       log_message "Update and copy completed successfully."
        #       cp -r "$source_path"/* "$production_path"
        #       ng serve
#               exit 0
#           else
#               log_message "Unit tests faled."
#           fi#
#       fi
#    else
#        log_message "Compile errors!"
#       exit -1
#    fi
else
    log_message "Local branch is up to date. No need to update or copy."
fi
Editor is loading...
Leave a Comment