This repository was archived by the owner on Feb 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-cloud-build.sh
More file actions
144 lines (116 loc) · 4.34 KB
/
deploy-cloud-build.sh
File metadata and controls
144 lines (116 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
# 🚀 Script de Deploy usando Google Cloud Build (sem Docker local)
# Deploy da API Go para produção no Google Cloud Run
set -e # Parar em caso de erro
echo "🚀 Iniciando deploy via Google Cloud Build..."
echo "================================================"
# Cores para output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Função para log colorido
log_info() {
echo -e "${BLUE}ℹ️ $1${NC}"
}
log_success() {
echo -e "${GREEN}✅ $1${NC}"
}
log_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
log_error() {
echo -e "${RED}❌ $1${NC}"
}
# Configurações do projeto
PROJECT_ID="gestao-de-pessoas-v2"
SERVICE_NAME="prime-people-api"
REGION="us-central1"
# 1. Verificar pré-requisitos
log_info "Verificando pré-requisitos..."
# Verificar se gcloud está instalado
if ! command -v gcloud &> /dev/null; then
log_error "Google Cloud CLI não está instalado!"
log_info "Instale com: curl https://sdk.cloud.google.com | bash"
exit 1
fi
log_success "Pré-requisitos verificados"
# 2. Verificar autenticação do gcloud
log_info "Verificando autenticação do Google Cloud..."
if ! gcloud auth list --filter=status:ACTIVE --format="value(account)" | grep -q .; then
log_warning "Não há contas autenticadas no gcloud"
log_info "Execute: gcloud auth login"
gcloud auth login
fi
# Configurar projeto
gcloud config set project $PROJECT_ID
log_success "Projeto configurado: $PROJECT_ID"
# 3. Habilitar APIs necessárias
log_info "Habilitando APIs necessárias..."
gcloud services enable cloudbuild.googleapis.com
gcloud services enable run.googleapis.com
gcloud services enable containerregistry.googleapis.com
log_success "APIs habilitadas"
# 4. Verificar se cloudbuild.yaml existe
if [ ! -f "cloudbuild.yaml" ]; then
log_error "Arquivo cloudbuild.yaml não encontrado!"
exit 1
fi
# 5. Executar build e deploy via Cloud Build
log_info "Iniciando build e deploy via Google Cloud Build..."
log_info "Isso pode levar alguns minutos..."
BUILD_ID=$(gcloud builds submit --config cloudbuild.yaml . --format="value(id)")
if [ $? -eq 0 ]; then
log_success "Build e deploy concluídos! Build ID: $BUILD_ID"
else
log_error "Erro no build e deploy"
exit 1
fi
# 6. Obter URL do serviço
log_info "Obtendo URL do serviço..."
SERVICE_URL=$(gcloud run services describe $SERVICE_NAME --region=$REGION --format='value(status.url)' 2>/dev/null || echo "")
if [ -z "$SERVICE_URL" ]; then
log_warning "Não foi possível obter a URL automaticamente"
log_info "Verifique no console: https://console.cloud.google.com/run"
else
echo "================================================"
echo "🎉 Deploy concluído com sucesso!"
echo "🌐 URL do serviço: $SERVICE_URL"
echo "🔗 Health check: $SERVICE_URL/health"
echo "📊 API Base: $SERVICE_URL/api"
echo "================================================"
# 7. Teste básico da API
log_info "Testando a API..."
sleep 10 # Aguardar alguns segundos para o serviço inicializar
if curl -f "$SERVICE_URL/health" > /dev/null 2>&1; then
log_success "API está respondendo corretamente!"
echo
echo "📋 Teste manual:"
echo "curl $SERVICE_URL/health"
echo
else
log_warning "API pode estar iniciando ainda. Teste manualmente:"
echo "curl $SERVICE_URL/health"
fi
fi
# 8. Configurar domínio customizado (opcional)
echo
read -p "Deseja configurar um domínio customizado? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
read -p "Digite o domínio (ex: api.sigep.app.br): " CUSTOM_DOMAIN
log_info "Configurando domínio customizado: $CUSTOM_DOMAIN"
gcloud run domain-mappings create \
--service $SERVICE_NAME \
--domain $CUSTOM_DOMAIN \
--region $REGION
log_warning "Configure o DNS do seu domínio para apontar para:"
gcloud run domain-mappings describe $CUSTOM_DOMAIN --region=$REGION --format='value(status.resourceRecords[0].rrdata)' 2>/dev/null || echo "Verifique no console do Google Cloud"
fi
log_success "Deploy via Cloud Build finalizado!"
echo
echo "🔍 Para monitorar:"
echo "- Logs: gcloud run services logs tail $SERVICE_NAME --region=$REGION"
echo "- Console: https://console.cloud.google.com/run"
echo "- Build History: https://console.cloud.google.com/cloud-build/builds"