Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9bafb84
feat: rename message to Ngrok magic!
trandreluis Sep 24, 2025
198f9e5
fix: add permission to run the .mvnw (wrapper do maven)
trandreluis Sep 24, 2025
00ec6c7
refactor: change JDK version from 25 to 21 to use alpine container ve…
trandreluis Sep 24, 2025
15f74f5
feat: add cache for maven dependencies
trandreluis Sep 24, 2025
de7ec9f
feat: add cache for docker layers
trandreluis Sep 24, 2025
62d3719
refact: optimize cache docker and clear old images
trandreluis Sep 24, 2025
48db47e
fix: load of container and optimize docker performance
trandreluis Sep 24, 2025
36eebeb
refact: add util script for cron routine to clean containers
trandreluis Sep 24, 2025
c438b6a
refact: try to get the Ngrok URL 5 times and add log message if dont …
trandreluis Sep 24, 2025
dab76be
fix: Ngrok generation link - add step to token configuration getting …
trandreluis Sep 24, 2025
418c9f8
fix: adjust output ngrok URL
trandreluis Sep 24, 2025
664d339
fix: hide container id, to fix output with only Ngrok URL and nothing…
trandreluis Sep 24, 2025
0df6db7
refact: enable multi-prs parallel with strategy different ports with …
trandreluis Sep 24, 2025
2835dbb
fix: permission to execute mvnw (maven wrapper)
trandreluis Sep 24, 2025
57bdd89
fix: add permission for marocchino/sticky-pull-request-comment to mak…
trandreluis Sep 24, 2025
1e6ef49
fix: use nohup do detach the ngrok process of step action and keep hi…
trandreluis Sep 24, 2025
8550766
test: trigger deploy ngrok
trandreluis Sep 24, 2025
b880770
refact: change grok usage to embbedded container to keep alive after …
trandreluis Sep 24, 2025
bd9cdf2
fix: check if ngrok is running correctly
trandreluis Sep 24, 2025
0e30074
fix: add log to check docker container ngrok status
trandreluis Sep 24, 2025
ea489b6
fix: pass ngrok token as env getting from github action secrets
trandreluis Sep 24, 2025
b70a239
fix: change docker host to work on wsl2
trandreluis Sep 24, 2025
2a798a5
fix: remove containiner before execution
trandreluis Sep 24, 2025
749cbeb
fix: run container ngrok in background
trandreluis Sep 24, 2025
ddd6c80
fix: remove application container if already exists before recreate t…
trandreluis Sep 24, 2025
d343b82
fix: change run container ngrok and improve logs
trandreluis Sep 24, 2025
b86b9ab
fix: incremental cache with buildx
trandreluis Sep 24, 2025
b54573f
fix: remove build with cahce layer
trandreluis Sep 24, 2025
8c2f978
fix: remove cache
trandreluis Sep 24, 2025
c4b7800
ajuste
trandreluis Sep 24, 2025
4df659e
teste
trandreluis Sep 24, 2025
b508816
teste
trandreluis Sep 24, 2025
26f4c3c
teste
trandreluis Sep 24, 2025
ba10893
fix: persmission for marocchino -sticky-comment put a comment on PR
trandreluis Sep 24, 2025
5471367
obrigado
trandreluis Sep 24, 2025
9e481bf
teste
trandreluis Sep 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/scripts/setup-docker-prune-cron.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
set -e

CRON_CMD="0 3 * * 0 docker system prune -af --volumes > /var/log/docker-prune.log 2>&1"

echo "[INFO] Configurando limpeza semanal do Docker via cron..."

# Verifica se já existe a regra no cron do root
if sudo crontab -l 2>/dev/null | grep -q "docker system prune -af --volumes"; then
echo "[INFO] Já existe uma regra de prune configurada no cron. Nada a fazer."
else
# Adiciona a regra ao cron do root
(sudo crontab -l 2>/dev/null; echo "$CRON_CMD") | sudo crontab -
echo "[INFO] Cron configurado: limpeza semanal todo domingo às 03h."
fi

echo "[INFO] Você pode verificar o log em /var/log/docker-prune.log"
56 changes: 36 additions & 20 deletions .github/scripts/start-preview.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,46 @@
set -e

PR_NUMBER=$1
PORT=$((8000 + PR_NUMBER % 1000)) # porta única para cada PR
APP_NAME="simple-project-pr${PR_NUMBER}"
NGROK_NAME="ngrok-pr${PR_NUMBER}"
HOST_PORT=$((8000 + PR_NUMBER))
API_PORT=$((4040 + PR_NUMBER))

echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${PORT}"
echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${HOST_PORT}"

# Se container já existe, remover para atualizar
docker rm -f ${APP_NAME} || true
# Garantir que não há containers anteriores
docker rm -f ${APP_NAME} 2>/dev/null || true
docker rm -f ${NGROK_NAME} 2>/dev/null || true

# Build imagem docker
# Build da imagem sem cache
echo "[INFO] Build da imagem Docker..."
docker build -t ${APP_NAME}:latest .

# Rodar container da app
docker run -d --name ${APP_NAME} -p ${PORT}:8080 ${APP_NAME}:latest

# Matar ngrok anterior se existir
pkill -f "${NGROK_NAME}" || true

# Rodar ngrok apontando para porta do container
nohup ngrok http ${PORT} --name ${NGROK_NAME} > /tmp/${NGROK_NAME}.log 2>&1 &

# Esperar ngrok subir e pegar URL
sleep 5
URL=$(curl --silent http://127.0.0.1:4040/api/tunnels | jq -r ".tunnels[] | select(.config.addr==\"http://localhost:${PORT}\") | .public_url")

echo "[INFO] Preview URL: ${URL}"
echo "${URL}"
# Rodar o container da aplicação
echo "[INFO] Subindo container da aplicação ${APP_NAME}..."
docker run -d --name ${APP_NAME} -p ${HOST_PORT}:8080 ${APP_NAME}:latest

# Rodar o container do ngrok expondo API na porta 4040+PR_NUMBER
echo "[INFO] Subindo container do ngrok ${NGROK_NAME}..."
docker run -d --name ${NGROK_NAME} \
-e NGROK_AUTHTOKEN="${NGROK_AUTHTOKEN}" \
-p ${API_PORT}:4040 \
ngrok/ngrok:latest http host.docker.internal:${HOST_PORT} > /tmp/${NGROK_NAME}.log 2>&1

# Aguardar o ngrok iniciar e capturar a URL pela API
echo "[INFO] Aguardando ngrok inicializar na porta ${API_PORT}..."
for i in {1..10}; do
sleep 2
URL=$(curl -s http://127.0.0.1:${API_PORT}/api/tunnels | grep -o 'https://[0-9a-z]*\.ngrok-free\.app' | head -n 1 || true)
if [ -n "$URL" ]; then
break
fi
done

if [ -z "$URL" ]; then
echo "[ERRO] Não foi possível capturar a URL do Ngrok. Veja os logs em /tmp/${NGROK_NAME}.log"
exit 1
fi

echo "[INFO] URL pública gerada: $URL"
echo "$URL"
28 changes: 24 additions & 4 deletions .github/scripts/stop-preview.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,28 @@ NGROK_NAME="ngrok-pr${PR_NUMBER}"

echo "[INFO] Removendo preview para PR #${PR_NUMBER}"

# Parar container e remover
docker rm -f ${APP_NAME} || true
# Se o container da aplicação existir mas estiver em estado de falha, mostrar logs antes de remover
if docker ps -a --format '{{.Names}} {{.Status}}' | grep -q "${APP_NAME}"; then
STATUS=$(docker inspect -f '{{.State.Status}}' ${APP_NAME} || echo "desconhecido")
if [ "$STATUS" != "running" ]; then
echo "[WARN] Container ${APP_NAME} não está rodando (status: $STATUS). Exibindo últimos logs:"
docker logs --tail=100 ${APP_NAME} || true
fi
fi

# Matar ngrok correspondente
pkill -f "${NGROK_NAME}" || true
# Parar e remover container da aplicação
docker rm -f ${APP_NAME} >/dev/null 2>&1 || true

# Remover imagem associada ao PR (se existir)
docker rmi -f ${APP_NAME}:latest >/dev/null 2>&1 || true

# Parar e remover container do ngrok
docker rm -f ${NGROK_NAME} >/dev/null 2>&1 || true

# Extra: limpar eventuais containers órfãos do mesmo PR
docker ps -a --format '{{.Names}}' | grep -E "${APP_NAME}|${NGROK_NAME}" | xargs -r docker rm -f || true

# Extra: limpar eventuais imagens órfãs
docker image prune -f >/dev/null 2>&1 || true

echo "[INFO] Preview do PR #${PR_NUMBER} removido com sucesso."
22 changes: 17 additions & 5 deletions .github/workflows/pr-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
pull_request:
types: [opened, synchronize, reopened, closed]

permissions:
pull-requests: write
contents: read

jobs:
preview:
runs-on: self-hosted
Expand All @@ -12,31 +16,39 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Set up JDK 25
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '25'
java-version: '21'
distribution: 'temurin'

- name: Build simple-project app
if: github.event.action != 'closed'
run: ./mvnw clean package -DskipTests
run: |
chmod +x ./mvnw
./mvnw clean package -DskipTests

- name: Start Preview (container + ngrok)
if: github.event.action != 'closed'
id: preview
env:
NGROK_AUTHTOKEN: ${{ secrets.NGROK_AUTHTOKEN }}
run: |
chmod +x .github/scripts/start-preview.sh
URL=$(.github/scripts/start-preview.sh ${{ github.event.number }})
RAW_OUTPUT=$(.github/scripts/start-preview.sh ${{ github.event.number }})
URL=$(echo "$RAW_OUTPUT" | tail -n 1)
HOST_PORT=$((8000 + ${{ github.event.number }}))
echo "preview_url=$URL" >> $GITHUB_OUTPUT
echo "host_port=$HOST_PORT" >> $GITHUB_OUTPUT

- name: Comment PR with Preview URL
if: github.event.action != 'closed'
uses: marocchino/sticky-pull-request-comment@v2
with:
message: |
🚀 Preview da aplicação *simple-project* para este PR:
${{ steps.preview.outputs.preview_url }}
🌐 URL pública (Ngrok): ${{ steps.preview.outputs.preview_url }}
🖥️ Porta local usada no host: `${{ steps.preview.outputs.host_port }}`
_(Permanece ativo até o PR ser fechado ou manualmente removido)_

- name: Stop Preview on PR close
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Imagem base com JDK 25 (Temurin)
FROM eclipse-temurin:25-jdk-alpine
# Imagem base com JDK 21 (LTS estável)
FROM eclipse-temurin:21-jdk-alpine

WORKDIR /app

Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<url/>
</scm>
<properties>
<java.version>25</java.version>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -62,3 +62,4 @@
</build>

</project>

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class HomeController {

@GetMapping({"/", "/home"})
public String home(Model model) {
model.addAttribute("message", "Hello, World!");
model.addAttribute("message", "Hello, World! Now you see the Ngrok magic!");
return "home";
}
}
2 changes: 1 addition & 1 deletion src/main/resources/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
<title>Home</title>
</head>
<body>
<h1 th:text="${message}">Hello, World!</h1>
<h1 th:text="${message}"></h1>
</body>
</html>
Loading