4.9 KiB
4.9 KiB
Setup do Ambiente CI/CD - Avanzato
Arquitetura
+------------------------------------------------------------------+
| SEU SERVIDOR |
| |
| +-------------------+ +-------------------+ +-----------+ |
| | Git Bare Repo | | Pipeline | | Site | |
| | /var/git/site |--->| /var/ci/ |--->| Nginx | |
| | | | | | | |
| | - hooks/post-receive | - build.sh | | /var/www | |
| | - hooks/pre-receive | - deploy.sh | | | |
| +-------------------+ +-------------------+ +-----------+ |
| ^ | |
| | v |
| +-------------------+ +-------------------+ |
| | Seu Computador | | Mautic | |
| | | | mkt.avanzato | |
| | git push | | | |
| +-------------------+ +-------------------+ |
+------------------------------------------------------------------+
1. Instalar dependencias no servidor
# Atualizar sistema
apt update && apt upgrade -y
# Instalar Git
apt install -y git curl
# Instalar Node.js 20
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs
# Verificar
node -v # v20.x.x
npm -v # 10.x.x
# Instalar Nginx
apt install -y nginx
# Instalar PM2 (opcional, para gerenciar processos)
npm install -g pm2
2. Criar usuario de deploy
# Criar usuario dedicado
useradd -m -s /bin/bash deploy
# Adicionar ao grupo www-data
usermod -aG www-data deploy
# Criar chave SSH (como usuario deploy)
su - deploy
ssh-keygen -t ed25519 -C "deploy" -f ~/.ssh/id_ed25519 -N ""
# Ver chave publica (para autorizar pushes)
cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys
3. Criar repositorio Git bare
# Criar pasta
mkdir -p /var/git/site.git
cd /var/git/site.git
git init --bare
# Permissoes
chown -R deploy:deploy /var/git/site.git
chmod -R 755 /var/git/site.git
4. Configurar hooks Git
# Copiar hooks
sudo su - deploy
cp /var/ci/hooks/post-receive /var/git/site.git/hooks/post-receive
chmod +x /var/git/site.git/hooks/post-receive
5. Configurar Nginx
# Criar config
nano /etc/nginx/sites-available/avanzato
# Ver link: CI/CD > nginx-config.conf
# Ativar
ln -s /etc/nginx/sites-available/avanzato /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx
# SSL com Let's Encrypt
certbot --nginx -d avanzato.com.br -d www.avanzato.com.br
6. Criar estrutura de pastas
mkdir -p /var/www/avanzato # Site em producao
mkdir -p /var/www/avanzato-staging # Site em staging (opcional)
mkdir -p /var/ci # Pipeline scripts
mkdir -p /var/ci/logs # Logs de build/deploy
mkdir -p /var/ci/backups # Backups automaticos
mkdir -p /var/ci/assets # Assets preservados
chown -R deploy:www-data /var/www
chmod -R 755 /var/www
7. Configurar pipeline
# Copiar scripts do pipeline
cp /var/ci/scripts/* /var/ci/
chmod +x /var/ci/*.sh
# Configurar variaveis
nano /var/ci/config.env
# Conteudo:
DEPLOY_PATH=/var/www/avanzato
STAGING_PATH=/var/www/avanzato-staging
BACKUP_PATH=/var/ci/backups
LOG_PATH=/var/ci/logs
MAUTIC_URL=https://mkt.avanzato.com.br
8. Configurar seu computador local
# Adicionar remote no seu projeto local
cd /caminho/do/projeto/avanzato-site
git remote add servidor ssh://deploy@SEU_SERVIDOR/var/git/site.git
# Testar conexao
ssh deploy@SEU_SERVIDOR "echo 'Conectado!'"
# Push inicial
git push servidor main
Comandos de uso diario
| Comando | Resultado |
|---|---|
git push servidor main |
Deploy automatico para producao |
git push servidor staging |
Deploy para staging |
ssh deploy@SERVIDOR "cat /var/ci/logs/last-deploy.log" |
Ver ultimo log |
ssh deploy@SERVIDOR "ls /var/ci/backups" |
Listar backups |
Estrutura final no servidor
/var/
git/
site.git/ # Repo bare (recebe pushes)
hooks/
post-receive # Hook de deploy automatico
ci/
scripts/
build.sh # Compila o site
deploy.sh # Envia para producao
preserve-assets.sh # Preserva logo/imagens
backup.sh # Cria backup
hooks/
post-receive # Template do hook
logs/ # Logs de execucao
backups/ # Backups automaticos
assets/ # Assets preservados
config.env # Variaveis de ambiente
www/
avanzato/ # Site em producao
index.html
assets/
backups/ # Backups antigos