From 9bafb840bbf320b51a11c80040500fef86b1fe55 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Tue, 23 Sep 2025 22:30:09 -0300 Subject: [PATCH 01/36] feat: rename message to Ngrok magic! --- .../java/com/trandreluis/simple_project/HomeController.java | 2 +- src/main/resources/templates/home.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/trandreluis/simple_project/HomeController.java b/src/main/java/com/trandreluis/simple_project/HomeController.java index c49c4e5..f1d87c6 100644 --- a/src/main/java/com/trandreluis/simple_project/HomeController.java +++ b/src/main/java/com/trandreluis/simple_project/HomeController.java @@ -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"; } } \ No newline at end of file diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html index d16f6e3..8d81624 100644 --- a/src/main/resources/templates/home.html +++ b/src/main/resources/templates/home.html @@ -5,6 +5,6 @@ Home -

Hello, World!

+

\ No newline at end of file From 198f9e53f422f44a434730789b43d91d72ae9a18 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Tue, 23 Sep 2025 22:42:47 -0300 Subject: [PATCH 02/36] fix: add permission to run the .mvnw (wrapper do maven) --- .github/workflows/pr-preview.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 0673b88..d96e888 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -20,7 +20,9 @@ jobs: - 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' From 00ec6c741b891c6b6f290c05a0793c6333de5774 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Tue, 23 Sep 2025 22:55:34 -0300 Subject: [PATCH 03/36] refactor: change JDK version from 25 to 21 to use alpine container version --- .github/workflows/pr-preview.yml | 4 ++-- Dockerfile | 4 ++-- pom.xml | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index d96e888..409d965 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -12,10 +12,10 @@ 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 diff --git a/Dockerfile b/Dockerfile index 695afa8..19a4621 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/pom.xml b/pom.xml index 300b995..53dd86f 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ - 25 + 21 From 15f74f51ffc2c0308fb9ece5361d60c4e1852a08 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Tue, 23 Sep 2025 23:00:15 -0300 Subject: [PATCH 04/36] feat: add cache for maven dependencies --- .github/workflows/pr-preview.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 409d965..9dedd2f 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -18,6 +18,14 @@ jobs: java-version: '21' distribution: 'temurin' + - name: Cache Maven dependencies + uses: actions/cache@v4 + with: + path: ~/.m2/repository + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: | + ${{ runner.os }}-maven- + - name: Build simple-project app if: github.event.action != 'closed' run: | From de7ec9f8d7781606ce0514d8907be82120fdcbc6 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Tue, 23 Sep 2025 23:01:47 -0300 Subject: [PATCH 05/36] feat: add cache for docker layers --- .github/workflows/pr-preview.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 9dedd2f..6292cde 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -26,6 +26,14 @@ jobs: restore-keys: | ${{ runner.os }}-maven- + - name: Cache Docker layers + uses: actions/cache@v4 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-docker-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-docker- + - name: Build simple-project app if: github.event.action != 'closed' run: | @@ -37,6 +45,12 @@ jobs: id: preview run: | chmod +x .github/scripts/start-preview.sh + # habilitar cache de buildx + docker buildx create --use --name buildx-simple || docker buildx use buildx-simple + docker buildx build --cache-from=type=local,src=/tmp/.buildx-cache \ + --cache-to=type=local,dest=/tmp/.buildx-cache-new,mode=max \ + -t simple-project-pr${{ github.event.number }}:latest . + mv /tmp/.buildx-cache-new /tmp/.buildx-cache URL=$(.github/scripts/start-preview.sh ${{ github.event.number }}) echo "preview_url=$URL" >> $GITHUB_OUTPUT From 62d3719957ce9685fa6c5c53df69878d61cb9695 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Tue, 23 Sep 2025 23:03:56 -0300 Subject: [PATCH 06/36] refact: optimize cache docker and clear old images --- .github/scripts/start-preview.sh | 15 +++++++++++++-- .github/scripts/stop-preview.sh | 7 +++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index e3a577f..13433ee 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -5,14 +5,25 @@ 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}" +CACHE_DIR="/tmp/.buildx-cache" echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${PORT}" # Se container já existe, remover para atualizar docker rm -f ${APP_NAME} || true -# Build imagem docker -docker build -t ${APP_NAME}:latest . +# Garantir que o builder buildx existe +docker buildx create --use --name buildx-simple || docker buildx use buildx-simple + +# Build imagem docker com cache +docker buildx build \ + --cache-from=type=local,src=${CACHE_DIR} \ + --cache-to=type=local,dest=${CACHE_DIR}-new,mode=max \ + -t ${APP_NAME}:latest . + +# Atualizar cache +rm -rf ${CACHE_DIR} +mv ${CACHE_DIR}-new ${CACHE_DIR} # Rodar container da app docker run -d --name ${APP_NAME} -p ${PORT}:8080 ${APP_NAME}:latest diff --git a/.github/scripts/stop-preview.sh b/.github/scripts/stop-preview.sh index 66e2e2b..1c14a26 100644 --- a/.github/scripts/stop-preview.sh +++ b/.github/scripts/stop-preview.sh @@ -4,6 +4,7 @@ set -e PR_NUMBER=$1 APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" +CACHE_DIR="/tmp/.buildx-cache" echo "[INFO] Removendo preview para PR #${PR_NUMBER}" @@ -12,3 +13,9 @@ docker rm -f ${APP_NAME} || true # Matar ngrok correspondente pkill -f "${NGROK_NAME}" || true + +# Limpar camadas órfãs do buildx +if [ -d "${CACHE_DIR}" ]; then + echo "[INFO] Limpando cache antigo do buildx..." + docker builder prune -f || true +fi From 48db47e657a952a45bc5714f6fd8494110b33b80 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Tue, 23 Sep 2025 23:12:50 -0300 Subject: [PATCH 07/36] fix: load of container and optimize docker performance --- .github/scripts/start-preview.sh | 11 ++++++++--- .github/scripts/stop-preview.sh | 5 ++++- .github/workflows/pr-preview.yml | 26 ++++++++++++++++++++------ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index 13433ee..360884e 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -13,13 +13,18 @@ echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${PORT}" docker rm -f ${APP_NAME} || true # Garantir que o builder buildx existe -docker buildx create --use --name buildx-simple || docker buildx use buildx-simple +if ! docker buildx inspect buildx-simple >/dev/null 2>&1; then + docker buildx create --use --name buildx-simple +else + docker buildx use buildx-simple +fi -# Build imagem docker com cache +# Build imagem docker com cache e carregando no Docker local docker buildx build \ --cache-from=type=local,src=${CACHE_DIR} \ --cache-to=type=local,dest=${CACHE_DIR}-new,mode=max \ - -t ${APP_NAME}:latest . + -t ${APP_NAME}:latest . \ + --load # Atualizar cache rm -rf ${CACHE_DIR} diff --git a/.github/scripts/stop-preview.sh b/.github/scripts/stop-preview.sh index 1c14a26..41d8dc6 100644 --- a/.github/scripts/stop-preview.sh +++ b/.github/scripts/stop-preview.sh @@ -11,11 +11,14 @@ echo "[INFO] Removendo preview para PR #${PR_NUMBER}" # Parar container e remover docker rm -f ${APP_NAME} || true +# Remover imagem associada ao PR +docker rmi -f ${APP_NAME}:latest || true + # Matar ngrok correspondente pkill -f "${NGROK_NAME}" || true # Limpar camadas órfãs do buildx if [ -d "${CACHE_DIR}" ]; then - echo "[INFO] Limpando cache antigo do buildx..." + echo "[INFO] Limpando camadas órfãs do buildx..." docker builder prune -f || true fi diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 6292cde..71a38b9 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -45,12 +45,26 @@ jobs: id: preview run: | chmod +x .github/scripts/start-preview.sh - # habilitar cache de buildx - docker buildx create --use --name buildx-simple || docker buildx use buildx-simple - docker buildx build --cache-from=type=local,src=/tmp/.buildx-cache \ - --cache-to=type=local,dest=/tmp/.buildx-cache-new,mode=max \ - -t simple-project-pr${{ github.event.number }}:latest . - mv /tmp/.buildx-cache-new /tmp/.buildx-cache + CACHE_DIR="/tmp/.buildx-cache" + BUILDER_NAME="buildx-simple" + + # garantir builder buildx + if ! docker buildx inspect $BUILDER_NAME >/dev/null 2>&1; then + docker buildx create --use --name $BUILDER_NAME + else + docker buildx use $BUILDER_NAME + fi + + # build imagem com cache e --load para Docker local + docker buildx build \ + --cache-from=type=local,src=${CACHE_DIR} \ + --cache-to=type=local,dest=${CACHE_DIR}-new,mode=max \ + -t simple-project-pr${{ github.event.number }}:latest . \ + --load + + rm -rf ${CACHE_DIR} + mv ${CACHE_DIR}-new ${CACHE_DIR} + URL=$(.github/scripts/start-preview.sh ${{ github.event.number }}) echo "preview_url=$URL" >> $GITHUB_OUTPUT From 36eebeba77447dceee3d154ae64bcdf6e709b146 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Tue, 23 Sep 2025 23:15:06 -0300 Subject: [PATCH 08/36] refact: add util script for cron routine to clean containers --- .github/scripts/setup-docker-prune-cron.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .github/scripts/setup-docker-prune-cron.sh diff --git a/.github/scripts/setup-docker-prune-cron.sh b/.github/scripts/setup-docker-prune-cron.sh new file mode 100644 index 0000000..24f5e46 --- /dev/null +++ b/.github/scripts/setup-docker-prune-cron.sh @@ -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" From c438b6a2c103b50d956217c490ad488a5a8538df Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Tue, 23 Sep 2025 23:21:47 -0300 Subject: [PATCH 09/36] refact: try to get the Ngrok URL 5 times and add log message if dont get that --- .github/scripts/start-preview.sh | 20 +++++++++++++++++--- .github/workflows/pr-preview.yml | 20 -------------------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index 360884e..6f99d73 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -39,9 +39,23 @@ 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") +# Tentar capturar a URL do Ngrok com retries +URL="" +for i in {1..5}; do + sleep 2 + URL=$(curl --silent http://127.0.0.1:4040/api/tunnels \ + | jq -r ".tunnels[] | select(.config.addr==\"http://localhost:${PORT}\") | .public_url") + if [ -n "$URL" ] && [ "$URL" != "null" ]; then + break + fi + echo "[WARN] Tentativa $i: ainda não capturou a URL do Ngrok." +done + +if [ -z "$URL" ] || [ "$URL" = "null" ]; then + echo "[ERRO] Não foi possível capturar a URL do Ngrok após várias tentativas." + exit 1 +fi echo "[INFO] Preview URL: ${URL}" +# Imprime apenas a URL (para o workflow capturar) echo "${URL}" diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 71a38b9..cc4eb90 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -45,26 +45,6 @@ jobs: id: preview run: | chmod +x .github/scripts/start-preview.sh - CACHE_DIR="/tmp/.buildx-cache" - BUILDER_NAME="buildx-simple" - - # garantir builder buildx - if ! docker buildx inspect $BUILDER_NAME >/dev/null 2>&1; then - docker buildx create --use --name $BUILDER_NAME - else - docker buildx use $BUILDER_NAME - fi - - # build imagem com cache e --load para Docker local - docker buildx build \ - --cache-from=type=local,src=${CACHE_DIR} \ - --cache-to=type=local,dest=${CACHE_DIR}-new,mode=max \ - -t simple-project-pr${{ github.event.number }}:latest . \ - --load - - rm -rf ${CACHE_DIR} - mv ${CACHE_DIR}-new ${CACHE_DIR} - URL=$(.github/scripts/start-preview.sh ${{ github.event.number }}) echo "preview_url=$URL" >> $GITHUB_OUTPUT From dab76be3bad48d1b79e4d0e8d474e452d0a8feab Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Tue, 23 Sep 2025 23:33:20 -0300 Subject: [PATCH 10/36] fix: Ngrok generation link - add step to token configuration getting from github secrets repository --- .github/workflows/pr-preview.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index cc4eb90..c1bbd92 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -34,6 +34,11 @@ jobs: restore-keys: | ${{ runner.os }}-docker- + - name: Configure Ngrok + if: github.event.action != 'closed' + run: | + ngrok config add-authtoken ${{ secrets.NGROK_AUTHTOKEN }} + - name: Build simple-project app if: github.event.action != 'closed' run: | From 418c9f8d32777f4fd24a87d1d0515d5e541d6d60 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Tue, 23 Sep 2025 23:37:57 -0300 Subject: [PATCH 11/36] fix: adjust output ngrok URL --- .github/scripts/start-preview.sh | 14 ++++++-------- .github/scripts/stop-preview.sh | 11 ++++++++++- .github/workflows/pr-preview.yml | 4 ++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index 6f99d73..dc152a3 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -7,8 +7,6 @@ APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" CACHE_DIR="/tmp/.buildx-cache" -echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${PORT}" - # Se container já existe, remover para atualizar docker rm -f ${APP_NAME} || true @@ -41,21 +39,21 @@ nohup ngrok http ${PORT} --name ${NGROK_NAME} > /tmp/${NGROK_NAME}.log 2>&1 & # Tentar capturar a URL do Ngrok com retries URL="" -for i in {1..5}; do - sleep 2 +for i in {1..10}; do + sleep 3 URL=$(curl --silent http://127.0.0.1:4040/api/tunnels \ | jq -r ".tunnels[] | select(.config.addr==\"http://localhost:${PORT}\") | .public_url") if [ -n "$URL" ] && [ "$URL" != "null" ]; then break fi - echo "[WARN] Tentativa $i: ainda não capturou a URL do Ngrok." done if [ -z "$URL" ] || [ "$URL" = "null" ]; then - echo "[ERRO] Não foi possível capturar a URL do Ngrok após várias tentativas." + echo "[ERRO] Não foi possível capturar a URL do Ngrok após várias tentativas." >&2 + docker logs ${APP_NAME} || true + tail -n 50 /tmp/${NGROK_NAME}.log || true exit 1 fi -echo "[INFO] Preview URL: ${URL}" -# Imprime apenas a URL (para o workflow capturar) +# ⚠️ Importante: imprimir só a URL, sem logs extras echo "${URL}" diff --git a/.github/scripts/stop-preview.sh b/.github/scripts/stop-preview.sh index 41d8dc6..eb33d1e 100644 --- a/.github/scripts/stop-preview.sh +++ b/.github/scripts/stop-preview.sh @@ -8,7 +8,16 @@ CACHE_DIR="/tmp/.buildx-cache" echo "[INFO] Removendo preview para PR #${PR_NUMBER}" -# Parar container e remover +# Se o container 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 + +# Parar e remover container docker rm -f ${APP_NAME} || true # Remover imagem associada ao PR diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index c1bbd92..3d153b5 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -51,6 +51,10 @@ jobs: run: | chmod +x .github/scripts/start-preview.sh URL=$(.github/scripts/start-preview.sh ${{ github.event.number }}) + if [[ "$URL" != https* ]]; then + echo "Erro: saída inválida do start-preview.sh: $URL" + exit 1 + fi echo "preview_url=$URL" >> $GITHUB_OUTPUT - name: Comment PR with Preview URL From 664d339b2f4e7ac8609b9bad87318f6f924dbd51 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Tue, 23 Sep 2025 23:44:32 -0300 Subject: [PATCH 12/36] fix: hide container id, to fix output with only Ngrok URL and nothing more --- .github/scripts/start-preview.sh | 4 ++-- .github/workflows/pr-preview.yml | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index dc152a3..a9b0c5d 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -28,8 +28,8 @@ docker buildx build \ rm -rf ${CACHE_DIR} mv ${CACHE_DIR}-new ${CACHE_DIR} -# Rodar container da app -docker run -d --name ${APP_NAME} -p ${PORT}:8080 ${APP_NAME}:latest +# Rodar container da app (captura o ID mas não imprime) +CONTAINER_ID=$(docker run -d --name ${APP_NAME} -p ${PORT}:8080 ${APP_NAME}:latest) # Matar ngrok anterior se existir pkill -f "${NGROK_NAME}" || true diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 3d153b5..69e81e0 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -55,6 +55,7 @@ jobs: echo "Erro: saída inválida do start-preview.sh: $URL" exit 1 fi + echo "Preview URL gerada: $URL" echo "preview_url=$URL" >> $GITHUB_OUTPUT - name: Comment PR with Preview URL From 0df6db7abe71d8ecf728bb94e1a22661c412e3d8 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Tue, 23 Sep 2025 23:57:02 -0300 Subject: [PATCH 13/36] refact: enable multi-prs parallel with strategy different ports with + PR number --- .github/scripts/start-preview.sh | 77 +++++++++++--------------------- .github/scripts/stop-preview.sh | 33 +++++++------- .github/workflows/pr-preview.yml | 30 +------------ 3 files changed, 46 insertions(+), 94 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index a9b0c5d..ce1efbe 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -2,58 +2,35 @@ 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}" -CACHE_DIR="/tmp/.buildx-cache" - -# Se container já existe, remover para atualizar -docker rm -f ${APP_NAME} || true - -# Garantir que o builder buildx existe -if ! docker buildx inspect buildx-simple >/dev/null 2>&1; then - docker buildx create --use --name buildx-simple -else - docker buildx use buildx-simple +APP_NAME="simple-project" +CONTAINER_NAME="${APP_NAME}-pr${PR_NUMBER}" +HOST_PORT=$((8000 + PR_NUMBER)) + +echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${HOST_PORT}..." + +# Build da imagem +docker build -t "${CONTAINER_NAME}:latest" . + +# Remove container antigo se existir +if [ "$(docker ps -aq -f name=${CONTAINER_NAME})" ]; then + docker rm -f "${CONTAINER_NAME}" || true fi -# Build imagem docker com cache e carregando no Docker local -docker buildx build \ - --cache-from=type=local,src=${CACHE_DIR} \ - --cache-to=type=local,dest=${CACHE_DIR}-new,mode=max \ - -t ${APP_NAME}:latest . \ - --load - -# Atualizar cache -rm -rf ${CACHE_DIR} -mv ${CACHE_DIR}-new ${CACHE_DIR} - -# Rodar container da app (captura o ID mas não imprime) -CONTAINER_ID=$(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 & - -# Tentar capturar a URL do Ngrok com retries -URL="" -for i in {1..10}; do - sleep 3 - URL=$(curl --silent http://127.0.0.1:4040/api/tunnels \ - | jq -r ".tunnels[] | select(.config.addr==\"http://localhost:${PORT}\") | .public_url") - if [ -n "$URL" ] && [ "$URL" != "null" ]; then - break - fi -done - -if [ -z "$URL" ] || [ "$URL" = "null" ]; then - echo "[ERRO] Não foi possível capturar a URL do Ngrok após várias tentativas." >&2 - docker logs ${APP_NAME} || true - tail -n 50 /tmp/${NGROK_NAME}.log || true +# Sobe o container com porta exclusiva +docker run -d --name "${CONTAINER_NAME}" -p ${HOST_PORT}:8080 "${CONTAINER_NAME}:latest" + +# Inicia Ngrok com log dedicado para o PR +ngrok http ${HOST_PORT} > "ngrok-${PR_NUMBER}.log" 2>&1 & +sleep 5 + +# Captura URL do Ngrok +NGROK_URL=$(curl --silent --max-time 10 http://127.0.0.1:4040/api/tunnels | jq -r '.tunnels[0].public_url') + +if [[ -z "$NGROK_URL" || "$NGROK_URL" == "null" ]]; then + echo "Erro: não foi possível obter a URL do Ngrok" >&2 exit 1 fi -# ⚠️ Importante: imprimir só a URL, sem logs extras -echo "${URL}" +echo "[INFO] Preview disponível em: $NGROK_URL" +# Só imprime a URL para o workflow capturar +echo "$NGROK_URL" diff --git a/.github/scripts/stop-preview.sh b/.github/scripts/stop-preview.sh index eb33d1e..c56bb6b 100644 --- a/.github/scripts/stop-preview.sh +++ b/.github/scripts/stop-preview.sh @@ -2,32 +2,35 @@ set -e PR_NUMBER=$1 -APP_NAME="simple-project-pr${PR_NUMBER}" -NGROK_NAME="ngrok-pr${PR_NUMBER}" +APP_NAME="simple-project" +CONTAINER_NAME="${APP_NAME}-pr${PR_NUMBER}" +HOST_PORT=$((8000 + PR_NUMBER)) CACHE_DIR="/tmp/.buildx-cache" -echo "[INFO] Removendo preview para PR #${PR_NUMBER}" +echo "[INFO] Removendo preview para PR #${PR_NUMBER} (porta ${HOST_PORT})..." -# Se o container 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") +# Se container existir, mostrar logs se falhou antes de remover +if docker ps -a --format '{{.Names}} {{.Status}}' | grep -q "${CONTAINER_NAME}"; then + STATUS=$(docker inspect -f '{{.State.Status}}' ${CONTAINER_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 + echo "[WARN] Container ${CONTAINER_NAME} não está rodando (status: $STATUS). Últimos logs:" + docker logs --tail=100 ${CONTAINER_NAME} || true fi fi -# Parar e remover container -docker rm -f ${APP_NAME} || true +# Remover container +docker rm -f ${CONTAINER_NAME} || true -# Remover imagem associada ao PR -docker rmi -f ${APP_NAME}:latest || true +# Remover imagem associada +docker rmi -f ${CONTAINER_NAME}:latest || true -# Matar ngrok correspondente -pkill -f "${NGROK_NAME}" || true +# Encerrar ngrok do PR específico +pkill -f "ngrok http ${HOST_PORT}" || true -# Limpar camadas órfãs do buildx +# Limpar cache do buildx if [ -d "${CACHE_DIR}" ]; then echo "[INFO] Limpando camadas órfãs do buildx..." docker builder prune -f || true fi + +echo "[INFO] Preview do PR #${PR_NUMBER} encerrado com sucesso." diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 69e81e0..d1c33b4 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -18,32 +18,9 @@ jobs: java-version: '21' distribution: 'temurin' - - name: Cache Maven dependencies - uses: actions/cache@v4 - with: - path: ~/.m2/repository - key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} - restore-keys: | - ${{ runner.os }}-maven- - - - name: Cache Docker layers - uses: actions/cache@v4 - with: - path: /tmp/.buildx-cache - key: ${{ runner.os }}-docker-${{ github.sha }} - restore-keys: | - ${{ runner.os }}-docker- - - - name: Configure Ngrok - if: github.event.action != 'closed' - run: | - ngrok config add-authtoken ${{ secrets.NGROK_AUTHTOKEN }} - - name: Build simple-project app if: github.event.action != 'closed' - run: | - chmod +x mvnw - ./mvnw clean package -DskipTests + run: ./mvnw clean package -DskipTests - name: Start Preview (container + ngrok) if: github.event.action != 'closed' @@ -51,11 +28,6 @@ jobs: run: | chmod +x .github/scripts/start-preview.sh URL=$(.github/scripts/start-preview.sh ${{ github.event.number }}) - if [[ "$URL" != https* ]]; then - echo "Erro: saída inválida do start-preview.sh: $URL" - exit 1 - fi - echo "Preview URL gerada: $URL" echo "preview_url=$URL" >> $GITHUB_OUTPUT - name: Comment PR with Preview URL From 2835dbb591da148a9e115a4bb8fe03cdf14420be Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 00:02:32 -0300 Subject: [PATCH 14/36] fix: permission to execute mvnw (maven wrapper) --- .github/workflows/pr-preview.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index d1c33b4..89bda3b 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -20,15 +20,20 @@ jobs: - 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 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' @@ -36,7 +41,8 @@ jobs: 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 From 57bdd89f51063da38062868f61e31e0173b94250 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 00:08:28 -0300 Subject: [PATCH 15/36] fix: add permission for marocchino/sticky-pull-request-comment to make a comment with the Ngrok link on PR --- .github/workflows/pr-preview.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 89bda3b..4bcdf07 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -4,6 +4,10 @@ on: pull_request: types: [opened, synchronize, reopened, closed] +permissions: + pull-requests: write + contents: read + jobs: preview: runs-on: self-hosted From 1e6ef49bd408c488650353780c36dd88cf24748d Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 00:29:57 -0300 Subject: [PATCH 16/36] fix: use nohup do detach the ngrok process of step action and keep him alive after her finish --- .github/scripts/start-preview.sh | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index ce1efbe..2b3052f 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -5,6 +5,7 @@ PR_NUMBER=$1 APP_NAME="simple-project" CONTAINER_NAME="${APP_NAME}-pr${PR_NUMBER}" HOST_PORT=$((8000 + PR_NUMBER)) +LOG_FILE="ngrok-${PR_NUMBER}.log" echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${HOST_PORT}..." @@ -19,11 +20,13 @@ fi # Sobe o container com porta exclusiva docker run -d --name "${CONTAINER_NAME}" -p ${HOST_PORT}:8080 "${CONTAINER_NAME}:latest" -# Inicia Ngrok com log dedicado para o PR -ngrok http ${HOST_PORT} > "ngrok-${PR_NUMBER}.log" 2>&1 & +# Inicia Ngrok desacoplado do shell e salva logs +nohup ngrok http ${HOST_PORT} > "${LOG_FILE}" 2>&1 & + +# Aguarda o Ngrok inicializar sleep 5 -# Captura URL do Ngrok +# Captura URL do Ngrok pela API local NGROK_URL=$(curl --silent --max-time 10 http://127.0.0.1:4040/api/tunnels | jq -r '.tunnels[0].public_url') if [[ -z "$NGROK_URL" || "$NGROK_URL" == "null" ]]; then @@ -31,6 +34,8 @@ if [[ -z "$NGROK_URL" || "$NGROK_URL" == "null" ]]; then exit 1 fi -echo "[INFO] Preview disponível em: $NGROK_URL" +# Grava a URL no log para debug futuro +echo "[INFO] URL gerada: $NGROK_URL" | tee -a "${LOG_FILE}" + # Só imprime a URL para o workflow capturar echo "$NGROK_URL" From 8550766b7f00247aac716d389113f4ac639c5e78 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 00:37:43 -0300 Subject: [PATCH 17/36] test: trigger deploy ngrok --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 53dd86f..c130963 100644 --- a/pom.xml +++ b/pom.xml @@ -62,3 +62,4 @@ + From b8807702a69b3e7a644f1b274d052cf57031a9cc Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 00:45:44 -0300 Subject: [PATCH 18/36] refact: change grok usage to embbedded container to keep alive after actions be ended --- .github/scripts/start-preview.sh | 47 +++++++++++++++----------------- .github/scripts/stop-preview.sh | 31 +++++++++------------ 2 files changed, 35 insertions(+), 43 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index 2b3052f..e12c307 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -2,40 +2,37 @@ set -e PR_NUMBER=$1 -APP_NAME="simple-project" -CONTAINER_NAME="${APP_NAME}-pr${PR_NUMBER}" +APP_NAME="simple-project-pr${PR_NUMBER}" +NGROK_NAME="ngrok-pr${PR_NUMBER}" HOST_PORT=$((8000 + PR_NUMBER)) -LOG_FILE="ngrok-${PR_NUMBER}.log" +CACHE_DIR="/tmp/.buildx-cache" +LOG_FILE="/tmp/${APP_NAME}.log" -echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${HOST_PORT}..." +echo "[INFO] Iniciando preview para PR #${PR_NUMBER}" -# Build da imagem -docker build -t "${CONTAINER_NAME}:latest" . +# Construir imagem +docker buildx build \ + --cache-to=type=local,dest=${CACHE_DIR},mode=max \ + --cache-from=type=local,src=${CACHE_DIR} \ + -t ${APP_NAME}:latest \ + --load . -# Remove container antigo se existir -if [ "$(docker ps -aq -f name=${CONTAINER_NAME})" ]; then - docker rm -f "${CONTAINER_NAME}" || true -fi - -# Sobe o container com porta exclusiva -docker run -d --name "${CONTAINER_NAME}" -p ${HOST_PORT}:8080 "${CONTAINER_NAME}:latest" +# Rodar container da aplicação +docker run -d --name ${APP_NAME} -p ${HOST_PORT}:8080 ${APP_NAME}:latest -# Inicia Ngrok desacoplado do shell e salva logs -nohup ngrok http ${HOST_PORT} > "${LOG_FILE}" 2>&1 & +# Subir ngrok em container separado +docker run -d --name ${NGROK_NAME} --network host \ + -e NGROK_AUTHTOKEN=${NGROK_AUTHTOKEN} \ + ngrok/ngrok:latest http ${HOST_PORT} > "${LOG_FILE}" 2>&1 || true -# Aguarda o Ngrok inicializar +# Aguardar ngrok subir e pegar URL sleep 5 +NGROK_URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -oE "https://[0-9a-z]+\.ngrok-free\.app" | head -n 1) -# Captura URL do Ngrok pela API local -NGROK_URL=$(curl --silent --max-time 10 http://127.0.0.1:4040/api/tunnels | jq -r '.tunnels[0].public_url') - -if [[ -z "$NGROK_URL" || "$NGROK_URL" == "null" ]]; then - echo "Erro: não foi possível obter a URL do Ngrok" >&2 +if [ -z "$NGROK_URL" ]; then + echo "[ERRO] Não foi possível capturar a URL do Ngrok" exit 1 fi -# Grava a URL no log para debug futuro -echo "[INFO] URL gerada: $NGROK_URL" | tee -a "${LOG_FILE}" - -# Só imprime a URL para o workflow capturar +echo "[INFO] URL pública: $NGROK_URL" echo "$NGROK_URL" diff --git a/.github/scripts/stop-preview.sh b/.github/scripts/stop-preview.sh index c56bb6b..856bebe 100644 --- a/.github/scripts/stop-preview.sh +++ b/.github/scripts/stop-preview.sh @@ -2,35 +2,30 @@ set -e PR_NUMBER=$1 -APP_NAME="simple-project" -CONTAINER_NAME="${APP_NAME}-pr${PR_NUMBER}" -HOST_PORT=$((8000 + PR_NUMBER)) +APP_NAME="simple-project-pr${PR_NUMBER}" +NGROK_NAME="ngrok-pr${PR_NUMBER}" CACHE_DIR="/tmp/.buildx-cache" -echo "[INFO] Removendo preview para PR #${PR_NUMBER} (porta ${HOST_PORT})..." +echo "[INFO] Removendo preview para PR #${PR_NUMBER}" -# Se container existir, mostrar logs se falhou antes de remover -if docker ps -a --format '{{.Names}} {{.Status}}' | grep -q "${CONTAINER_NAME}"; then - STATUS=$(docker inspect -f '{{.State.Status}}' ${CONTAINER_NAME} || echo "desconhecido") +# Se o container da aplicação existir mas estiver parado, mostrar logs +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 ${CONTAINER_NAME} não está rodando (status: $STATUS). Últimos logs:" - docker logs --tail=100 ${CONTAINER_NAME} || true + echo "[WARN] Container ${APP_NAME} não está rodando (status: $STATUS). Exibindo últimos logs:" + docker logs --tail=100 ${APP_NAME} || true fi fi -# Remover container -docker rm -f ${CONTAINER_NAME} || true +# Parar e remover containers +docker rm -f ${APP_NAME} || true +docker rm -f ${NGROK_NAME} || true # Remover imagem associada -docker rmi -f ${CONTAINER_NAME}:latest || true +docker rmi -f ${APP_NAME}:latest || true -# Encerrar ngrok do PR específico -pkill -f "ngrok http ${HOST_PORT}" || true - -# Limpar cache do buildx +# Limpar camadas órfãs do buildx if [ -d "${CACHE_DIR}" ]; then echo "[INFO] Limpando camadas órfãs do buildx..." docker builder prune -f || true fi - -echo "[INFO] Preview do PR #${PR_NUMBER} encerrado com sucesso." From bd9cdf26ae3a5ded477a31bfed00cd6b83dde382 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 00:53:42 -0300 Subject: [PATCH 19/36] fix: check if ngrok is running correctly --- .github/scripts/start-preview.sh | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index e12c307..d393439 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -20,19 +20,35 @@ docker buildx build \ # Rodar container da aplicação docker run -d --name ${APP_NAME} -p ${HOST_PORT}:8080 ${APP_NAME}:latest +# Verificar se token do Ngrok está configurado +if [ -z "$NGROK_AUTHTOKEN" ]; then + echo "[ERRO] NGROK_AUTHTOKEN não definido. Configure como secret no GitHub." + exit 1 +fi + # Subir ngrok em container separado docker run -d --name ${NGROK_NAME} --network host \ -e NGROK_AUTHTOKEN=${NGROK_AUTHTOKEN} \ ngrok/ngrok:latest http ${HOST_PORT} > "${LOG_FILE}" 2>&1 || true -# Aguardar ngrok subir e pegar URL -sleep 5 -NGROK_URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -oE "https://[0-9a-z]+\.ngrok-free\.app" | head -n 1) +# Aguardar até 30s pelo ngrok +NGROK_URL="" +for i in {1..30}; do + NGROK_URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -oE "https://[0-9a-z]+\.ngrok-free\.app" | head -n 1 || true) + if [ -n "$NGROK_URL" ]; then + break + fi + echo "[INFO] Aguardando ngrok subir... (${i}s)" + sleep 1 +done if [ -z "$NGROK_URL" ]; then - echo "[ERRO] Não foi possível capturar a URL do Ngrok" + echo "[ERRO] Não foi possível capturar a URL do Ngrok após 30s." + echo "[DEBUG] Logs do container do ngrok:" + docker logs ${NGROK_NAME} || true exit 1 fi echo "[INFO] URL pública: $NGROK_URL" +# A URL do ngrok será a última linha da saída (mantendo compatibilidade com o workflow) echo "$NGROK_URL" From 0e30074778bf48fdbfbe9ec803864fda04f2e6c7 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 01:07:17 -0300 Subject: [PATCH 20/36] fix: add log to check docker container ngrok status --- .github/scripts/start-preview.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index d393439..9a8071c 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -27,9 +27,14 @@ if [ -z "$NGROK_AUTHTOKEN" ]; then fi # Subir ngrok em container separado +echo "[INFO] Subindo container do ngrok..." docker run -d --name ${NGROK_NAME} --network host \ -e NGROK_AUTHTOKEN=${NGROK_AUTHTOKEN} \ - ngrok/ngrok:latest http ${HOST_PORT} > "${LOG_FILE}" 2>&1 || true + ngrok/ngrok:latest http ${HOST_PORT} > "${LOG_FILE}" 2>&1 + +# Debug inicial +echo "[DEBUG] Verificando se o container do ngrok subiu..." +docker ps -a | grep ${NGROK_NAME} || echo "[ERRO] Container ngrok não encontrado" # Aguardar até 30s pelo ngrok NGROK_URL="" From ea489b6e43f18762ad9c42c75f5a73a52e1448ed Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 01:17:27 -0300 Subject: [PATCH 21/36] fix: pass ngrok token as env getting from github action secrets --- .github/workflows/pr-preview.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 4bcdf07..9fb4510 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -4,10 +4,6 @@ on: pull_request: types: [opened, synchronize, reopened, closed] -permissions: - pull-requests: write - contents: read - jobs: preview: runs-on: self-hosted @@ -31,6 +27,8 @@ jobs: - 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 RAW_OUTPUT=$(.github/scripts/start-preview.sh ${{ github.event.number }}) From b70a2398de4d2c54ce690d5746e8f9fade9fb0fb Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 01:22:14 -0300 Subject: [PATCH 22/36] fix: change docker host to work on wsl2 --- .github/scripts/start-preview.sh | 64 +++++++++++++------------------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index 9a8071c..9f9b491 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -6,54 +6,42 @@ APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" HOST_PORT=$((8000 + PR_NUMBER)) CACHE_DIR="/tmp/.buildx-cache" -LOG_FILE="/tmp/${APP_NAME}.log" +LOG_FILE="/tmp/${NGROK_NAME}.log" -echo "[INFO] Iniciando preview para PR #${PR_NUMBER}" +echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${HOST_PORT}" -# Construir imagem -docker buildx build \ - --cache-to=type=local,dest=${CACHE_DIR},mode=max \ +# Build da imagem +docker buildx build --load \ + --cache-to=type=local,dest=${CACHE_DIR} \ --cache-from=type=local,src=${CACHE_DIR} \ - -t ${APP_NAME}:latest \ - --load . + -t ${APP_NAME}:latest . -# Rodar container da aplicação +# Subir container da aplicação docker run -d --name ${APP_NAME} -p ${HOST_PORT}:8080 ${APP_NAME}:latest -# Verificar se token do Ngrok está configurado -if [ -z "$NGROK_AUTHTOKEN" ]; then - echo "[ERRO] NGROK_AUTHTOKEN não definido. Configure como secret no GitHub." +# Garantir que o container subiu +sleep 5 +if ! docker ps --format '{{.Names}}' | grep -q "^${APP_NAME}$"; then + echo "[ERRO] Falha ao iniciar container ${APP_NAME}" >&2 exit 1 fi -# Subir ngrok em container separado -echo "[INFO] Subindo container do ngrok..." -docker run -d --name ${NGROK_NAME} --network host \ +# Subir container do Ngrok +echo "[INFO] Iniciando ngrok..." +docker run -d --name ${NGROK_NAME} \ -e NGROK_AUTHTOKEN=${NGROK_AUTHTOKEN} \ - ngrok/ngrok:latest http ${HOST_PORT} > "${LOG_FILE}" 2>&1 - -# Debug inicial -echo "[DEBUG] Verificando se o container do ngrok subiu..." -docker ps -a | grep ${NGROK_NAME} || echo "[ERRO] Container ngrok não encontrado" - -# Aguardar até 30s pelo ngrok -NGROK_URL="" -for i in {1..30}; do - NGROK_URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -oE "https://[0-9a-z]+\.ngrok-free\.app" | head -n 1 || true) - if [ -n "$NGROK_URL" ]; then - break - fi - echo "[INFO] Aguardando ngrok subir... (${i}s)" - sleep 1 -done - -if [ -z "$NGROK_URL" ]; then - echo "[ERRO] Não foi possível capturar a URL do Ngrok após 30s." - echo "[DEBUG] Logs do container do ngrok:" - docker logs ${NGROK_NAME} || true + -v /tmp/ngrok-${PR_NUMBER}:/tmp \ + ngrok/ngrok:latest http host.docker.internal:${HOST_PORT} > "${LOG_FILE}" 2>&1 + +sleep 5 + +# Pegar URL do Ngrok +URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -o "https://[0-9a-z]*\.ngrok-free\.app" | head -n 1) + +if [ -z "$URL" ]; then + echo "[ERRO] Não foi possível capturar a URL do Ngrok. Veja os logs em ${LOG_FILE}" >&2 exit 1 fi -echo "[INFO] URL pública: $NGROK_URL" -# A URL do ngrok será a última linha da saída (mantendo compatibilidade com o workflow) -echo "$NGROK_URL" +echo "[INFO] Preview disponível em: ${URL}" +echo "${URL}" From 2a798a57ca236ef60c1762a413c89e42714ae32c Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 01:31:28 -0300 Subject: [PATCH 23/36] fix: remove containiner before execution --- .github/scripts/start-preview.sh | 15 +++++++++------ .github/scripts/stop-preview.sh | 8 +++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index 9f9b491..e5b1548 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -4,11 +4,14 @@ set -e PR_NUMBER=$1 APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" -HOST_PORT=$((8000 + PR_NUMBER)) CACHE_DIR="/tmp/.buildx-cache" LOG_FILE="/tmp/${NGROK_NAME}.log" -echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${HOST_PORT}" +echo "[INFO] Iniciando preview para PR #${PR_NUMBER}" + +# Garantir que containers antigos não atrapalhem +docker rm -f ${APP_NAME} 2>/dev/null || true +docker rm -f ${NGROK_NAME} 2>/dev/null || true # Build da imagem docker buildx build --load \ @@ -17,7 +20,7 @@ docker buildx build --load \ -t ${APP_NAME}:latest . # Subir container da aplicação -docker run -d --name ${APP_NAME} -p ${HOST_PORT}:8080 ${APP_NAME}:latest +docker run -d --name ${APP_NAME} ${APP_NAME}:latest # Garantir que o container subiu sleep 5 @@ -26,12 +29,12 @@ if ! docker ps --format '{{.Names}}' | grep -q "^${APP_NAME}$"; then exit 1 fi -# Subir container do Ngrok +# Subir container do Ngrok conectado diretamente à rede do app echo "[INFO] Iniciando ngrok..." docker run -d --name ${NGROK_NAME} \ + --network container:${APP_NAME} \ -e NGROK_AUTHTOKEN=${NGROK_AUTHTOKEN} \ - -v /tmp/ngrok-${PR_NUMBER}:/tmp \ - ngrok/ngrok:latest http host.docker.internal:${HOST_PORT} > "${LOG_FILE}" 2>&1 + ngrok/ngrok:latest http 8080 > "${LOG_FILE}" 2>&1 sleep 5 diff --git a/.github/scripts/stop-preview.sh b/.github/scripts/stop-preview.sh index 856bebe..7f0cc8d 100644 --- a/.github/scripts/stop-preview.sh +++ b/.github/scripts/stop-preview.sh @@ -8,7 +8,7 @@ CACHE_DIR="/tmp/.buildx-cache" echo "[INFO] Removendo preview para PR #${PR_NUMBER}" -# Se o container da aplicação existir mas estiver parado, mostrar logs +# Se o container do app existir mas não estiver rodando, 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 @@ -17,11 +17,13 @@ if docker ps -a --format '{{.Names}} {{.Status}}' | grep -q "${APP_NAME}"; then fi fi -# Parar e remover containers +# Parar e remover container da aplicação docker rm -f ${APP_NAME} || true + +# Parar e remover container do Ngrok docker rm -f ${NGROK_NAME} || true -# Remover imagem associada +# Remover imagem associada ao PR docker rmi -f ${APP_NAME}:latest || true # Limpar camadas órfãs do buildx From 749cbeb02338ed6244f275a42cdedebfd62c6be9 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 01:46:20 -0300 Subject: [PATCH 24/36] fix: run container ngrok in background --- .github/scripts/start-preview.sh | 68 ++++++++++++++++---------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index e5b1548..8dde0e8 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -5,46 +5,46 @@ PR_NUMBER=$1 APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" CACHE_DIR="/tmp/.buildx-cache" -LOG_FILE="/tmp/${NGROK_NAME}.log" echo "[INFO] Iniciando preview para PR #${PR_NUMBER}" -# Garantir que containers antigos não atrapalhem -docker rm -f ${APP_NAME} 2>/dev/null || true -docker rm -f ${NGROK_NAME} 2>/dev/null || true - -# Build da imagem -docker buildx build --load \ +# Construir a imagem +docker buildx build \ --cache-to=type=local,dest=${CACHE_DIR} \ --cache-from=type=local,src=${CACHE_DIR} \ - -t ${APP_NAME}:latest . - -# Subir container da aplicação -docker run -d --name ${APP_NAME} ${APP_NAME}:latest - -# Garantir que o container subiu -sleep 5 -if ! docker ps --format '{{.Names}}' | grep -q "^${APP_NAME}$"; then - echo "[ERRO] Falha ao iniciar container ${APP_NAME}" >&2 - exit 1 -fi - -# Subir container do Ngrok conectado diretamente à rede do app -echo "[INFO] Iniciando ngrok..." -docker run -d --name ${NGROK_NAME} \ - --network container:${APP_NAME} \ - -e NGROK_AUTHTOKEN=${NGROK_AUTHTOKEN} \ - ngrok/ngrok:latest http 8080 > "${LOG_FILE}" 2>&1 + -t ${APP_NAME}:latest \ + . --load -sleep 5 +# Calcular porta baseada no número do PR +HOST_PORT=$((8000 + PR_NUMBER)) -# Pegar URL do Ngrok -URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -o "https://[0-9a-z]*\.ngrok-free\.app" | head -n 1) +# Subir container da aplicação +docker run -d --rm \ + --name ${APP_NAME} \ + -p ${HOST_PORT}:8080 \ + ${APP_NAME}:latest -if [ -z "$URL" ]; then - echo "[ERRO] Não foi possível capturar a URL do Ngrok. Veja os logs em ${LOG_FILE}" >&2 - exit 1 -fi +echo "[INFO] Aplicação rodando em porta local ${HOST_PORT}" -echo "[INFO] Preview disponível em: ${URL}" -echo "${URL}" +# Subir túnel ngrok no mesmo namespace de rede do container +docker run -d \ + --name ${NGROK_NAME} \ + --network=container:${APP_NAME} \ + -e NGROK_AUTHTOKEN=${NGROK_AUTHTOKEN} \ + ngrok/ngrok:latest http 8080 > /tmp/${NGROK_NAME}.cid + +# Tentar capturar a URL do túnel +for i in {1..10}; do + URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -o 'https://[0-9a-zA-Z.-]*\.ngrok-free\.app' | head -n1 || true) + if [ -n "$URL" ]; then + echo "[INFO] Ngrok URL capturada: $URL" + echo "$URL" + exit 0 + fi + echo "[INFO] Aguardando URL do Ngrok... Tentativa $i/10" + sleep 2 +done + +echo "[ERRO] Não foi possível capturar a URL do Ngrok. Veja os logs em /tmp/${NGROK_NAME}.log" +docker logs ${NGROK_NAME} &> /tmp/${NGROK_NAME}.log +exit 1 From ddd6c80dba50f4cd0e429513e7564526796f8b43 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 01:58:11 -0300 Subject: [PATCH 25/36] fix: remove application container if already exists before recreate to avoid conflicts --- .github/scripts/start-preview.sh | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index 8dde0e8..d8103f8 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -4,19 +4,24 @@ set -e PR_NUMBER=$1 APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" -CACHE_DIR="/tmp/.buildx-cache" +HOST_PORT=$((8000 + PR_NUMBER)) + +echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${HOST_PORT}" -echo "[INFO] Iniciando preview para PR #${PR_NUMBER}" +# Garantir que não exista container antigo com mesmo nome +docker rm -f ${APP_NAME} 2>/dev/null || true +docker rm -f ${NGROK_NAME} 2>/dev/null || true -# Construir a imagem +# Build da imagem (com cache para performance) docker buildx build \ - --cache-to=type=local,dest=${CACHE_DIR} \ - --cache-from=type=local,src=${CACHE_DIR} \ - -t ${APP_NAME}:latest \ - . --load + --load \ + --cache-from=type=local,src=/tmp/.buildx-cache \ + --cache-to=type=local,dest=/tmp/.buildx-cache-new,mode=max \ + -t ${APP_NAME}:latest . -# Calcular porta baseada no número do PR -HOST_PORT=$((8000 + PR_NUMBER)) +# Trocar cache de forma atômica +rm -rf /tmp/.buildx-cache +mv /tmp/.buildx-cache-new /tmp/.buildx-cache # Subir container da aplicação docker run -d --rm \ @@ -24,9 +29,7 @@ docker run -d --rm \ -p ${HOST_PORT}:8080 \ ${APP_NAME}:latest -echo "[INFO] Aplicação rodando em porta local ${HOST_PORT}" - -# Subir túnel ngrok no mesmo namespace de rede do container +# Subir túnel ngrok no mesmo namespace de rede do container da aplicação docker run -d \ --name ${NGROK_NAME} \ --network=container:${APP_NAME} \ From d343b82302c86f78e66a78d697336d6a2c21bf31 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 02:03:14 -0300 Subject: [PATCH 26/36] fix: change run container ngrok and improve logs --- .github/scripts/start-preview.sh | 48 +++++++++++++++----------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index d8103f8..0717ad4 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -4,50 +4,46 @@ set -e PR_NUMBER=$1 APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" -HOST_PORT=$((8000 + PR_NUMBER)) +CACHE_DIR="/tmp/.buildx-cache" +NGROK_LOG="/tmp/${NGROK_NAME}.log" -echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${HOST_PORT}" +echo "[INFO] Iniciando preview para PR #${PR_NUMBER}" -# Garantir que não exista container antigo com mesmo nome -docker rm -f ${APP_NAME} 2>/dev/null || true -docker rm -f ${NGROK_NAME} 2>/dev/null || true +# Garantir que containers antigos não causem conflito +docker rm -f ${APP_NAME} || true +docker rm -f ${NGROK_NAME} || true -# Build da imagem (com cache para performance) -docker buildx build \ - --load \ - --cache-from=type=local,src=/tmp/.buildx-cache \ - --cache-to=type=local,dest=/tmp/.buildx-cache-new,mode=max \ +# Build da imagem +docker build \ + --cache-from=type=local,src=${CACHE_DIR} \ + --cache-to=type=local,dest=${CACHE_DIR},mode=max \ -t ${APP_NAME}:latest . -# Trocar cache de forma atômica -rm -rf /tmp/.buildx-cache -mv /tmp/.buildx-cache-new /tmp/.buildx-cache +# Subir app container +docker run -d --name ${APP_NAME} -p 0:8080 ${APP_NAME}:latest -# Subir container da aplicação -docker run -d --rm \ - --name ${APP_NAME} \ - -p ${HOST_PORT}:8080 \ - ${APP_NAME}:latest +# Pegar porta real mapeada +HOST_PORT=$(docker inspect --format='{{(index (index .NetworkSettings.Ports "8080/tcp") 0).HostPort}}' ${APP_NAME}) +echo "[INFO] Aplicação rodando em porta local ${HOST_PORT}" -# Subir túnel ngrok no mesmo namespace de rede do container da aplicação +# Iniciar Ngrok em container separado e logar no arquivo docker run -d \ --name ${NGROK_NAME} \ --network=container:${APP_NAME} \ -e NGROK_AUTHTOKEN=${NGROK_AUTHTOKEN} \ - ngrok/ngrok:latest http 8080 > /tmp/${NGROK_NAME}.cid + ngrok/ngrok:latest http 8080 --log=stdout > "${NGROK_LOG}" 2>&1 -# Tentar capturar a URL do túnel -for i in {1..10}; do - URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -o 'https://[0-9a-zA-Z.-]*\.ngrok-free\.app' | head -n1 || true) +# Esperar até capturar a URL +for i in {1..15}; do + URL=$(grep -o 'https://[0-9a-zA-Z.-]*\.ngrok-free\.app' "${NGROK_LOG}" | head -n1 || true) if [ -n "$URL" ]; then echo "[INFO] Ngrok URL capturada: $URL" echo "$URL" exit 0 fi - echo "[INFO] Aguardando URL do Ngrok... Tentativa $i/10" + echo "[INFO] Aguardando URL do Ngrok... Tentativa $i/15" sleep 2 done -echo "[ERRO] Não foi possível capturar a URL do Ngrok. Veja os logs em /tmp/${NGROK_NAME}.log" -docker logs ${NGROK_NAME} &> /tmp/${NGROK_NAME}.log +echo "[ERRO] Não foi possível capturar a URL do Ngrok. Veja os logs em ${NGROK_LOG}" exit 1 From b86b9abc6324c2a922308eab6f1b0b302c738b89 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 02:14:39 -0300 Subject: [PATCH 27/36] fix: incremental cache with buildx --- .github/scripts/start-preview.sh | 74 +++++++++++++++++++------------- 1 file changed, 45 insertions(+), 29 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index 0717ad4..cc840a6 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -4,46 +4,62 @@ set -e PR_NUMBER=$1 APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" +HOST_PORT=$((8000 + PR_NUMBER)) CACHE_DIR="/tmp/.buildx-cache" NGROK_LOG="/tmp/${NGROK_NAME}.log" -echo "[INFO] Iniciando preview para PR #${PR_NUMBER}" +echo "[INFO] Subindo preview para PR #${PR_NUMBER} na porta ${HOST_PORT}" -# Garantir que containers antigos não causem conflito -docker rm -f ${APP_NAME} || true -docker rm -f ${NGROK_NAME} || true +# Garantir que não existam containers antigos +docker rm -f ${APP_NAME} 2>/dev/null || true +docker rm -f ${NGROK_NAME} 2>/dev/null || true -# Build da imagem -docker build \ +# Garantir que existe um builder buildx com driver containerd +if ! docker buildx inspect buildx-simple >/dev/null 2>&1; then + echo "[INFO] Criando builder buildx 'buildx-simple' com driver containerd..." + docker buildx create --name buildx-simple --driver docker-container --use + docker buildx inspect --bootstrap +else + echo "[INFO] Reutilizando builder existente 'buildx-simple'" + docker buildx use buildx-simple +fi + +# Fazer build com cache +docker buildx build \ --cache-from=type=local,src=${CACHE_DIR} \ --cache-to=type=local,dest=${CACHE_DIR},mode=max \ - -t ${APP_NAME}:latest . - -# Subir app container -docker run -d --name ${APP_NAME} -p 0:8080 ${APP_NAME}:latest + -t ${APP_NAME}:latest \ + --load . -# Pegar porta real mapeada -HOST_PORT=$(docker inspect --format='{{(index (index .NetworkSettings.Ports "8080/tcp") 0).HostPort}}' ${APP_NAME}) -echo "[INFO] Aplicação rodando em porta local ${HOST_PORT}" +# Subir container da aplicação +docker run -d --name ${APP_NAME} -p ${HOST_PORT}:8080 ${APP_NAME}:latest -# Iniciar Ngrok em container separado e logar no arquivo -docker run -d \ - --name ${NGROK_NAME} \ - --network=container:${APP_NAME} \ - -e NGROK_AUTHTOKEN=${NGROK_AUTHTOKEN} \ - ngrok/ngrok:latest http 8080 --log=stdout > "${NGROK_LOG}" 2>&1 +# Subir ngrok em segundo plano +docker run -d --name ${NGROK_NAME} \ + -e NGROK_AUTHTOKEN="${NGROK_AUTHTOKEN}" \ + --net=host \ + ngrok/ngrok:latest http ${HOST_PORT} > "${NGROK_LOG}" 2>&1 -# Esperar até capturar a URL -for i in {1..15}; do - URL=$(grep -o 'https://[0-9a-zA-Z.-]*\.ngrok-free\.app' "${NGROK_LOG}" | head -n1 || true) +# Aguardar até que o túnel esteja pronto +echo "[INFO] Aguardando URL pública do Ngrok..." +URL="" +for i in {1..10}; do + sleep 3 + URL=$(docker logs ${NGROK_NAME} 2>/dev/null | grep -o 'https://[a-zA-Z0-9.-]*\.ngrok-free\.app' | head -n1 || true) if [ -n "$URL" ]; then - echo "[INFO] Ngrok URL capturada: $URL" - echo "$URL" - exit 0 + break fi - echo "[INFO] Aguardando URL do Ngrok... Tentativa $i/15" - sleep 2 done -echo "[ERRO] Não foi possível capturar a URL do Ngrok. Veja os logs em ${NGROK_LOG}" -exit 1 +if [ -z "$URL" ]; then + echo "[ERRO] Não foi possível capturar a URL do Ngrok. Veja os logs em ${NGROK_LOG}" + exit 1 +fi + +echo "[INFO] Preview rodando:" +echo " App: ${APP_NAME}" +echo " Porta: ${HOST_PORT}" +echo " URL: ${URL}" + +# Retornar apenas a URL no stdout (para o workflow capturar) +echo "$URL" From b54573f636e29477006ab09537b1ab165031a9bd Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 02:18:56 -0300 Subject: [PATCH 28/36] fix: remove build with cahce layer --- .github/scripts/start-preview.sh | 63 +++++++++++--------------------- .github/scripts/stop-preview.sh | 36 ++++++++++-------- 2 files changed, 42 insertions(+), 57 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index cc840a6..07b7365 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -5,61 +5,42 @@ PR_NUMBER=$1 APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" HOST_PORT=$((8000 + PR_NUMBER)) -CACHE_DIR="/tmp/.buildx-cache" -NGROK_LOG="/tmp/${NGROK_NAME}.log" -echo "[INFO] Subindo preview para PR #${PR_NUMBER} na porta ${HOST_PORT}" +echo "[INFO] Iniciando preview para PR #${PR_NUMBER}..." -# Garantir que não existam containers antigos +# Remover containers antigos se existirem docker rm -f ${APP_NAME} 2>/dev/null || true docker rm -f ${NGROK_NAME} 2>/dev/null || true -# Garantir que existe um builder buildx com driver containerd -if ! docker buildx inspect buildx-simple >/dev/null 2>&1; then - echo "[INFO] Criando builder buildx 'buildx-simple' com driver containerd..." - docker buildx create --name buildx-simple --driver docker-container --use - docker buildx inspect --bootstrap -else - echo "[INFO] Reutilizando builder existente 'buildx-simple'" - docker buildx use buildx-simple -fi - -# Fazer build com cache -docker buildx build \ - --cache-from=type=local,src=${CACHE_DIR} \ - --cache-to=type=local,dest=${CACHE_DIR},mode=max \ - -t ${APP_NAME}:latest \ - --load . +# Construir imagem da aplicação (sem cache buildx) +echo "[INFO] Construindo imagem Docker para ${APP_NAME}..." +docker build -t ${APP_NAME}:latest . -# Subir container da aplicação +# Rodar container da aplicação +echo "[INFO] Subindo container da aplicação na porta ${HOST_PORT}..." docker run -d --name ${APP_NAME} -p ${HOST_PORT}:8080 ${APP_NAME}:latest -# Subir ngrok em segundo plano +# Subir ngrok em container separado +echo "[INFO] Subindo túnel Ngrok..." docker run -d --name ${NGROK_NAME} \ - -e NGROK_AUTHTOKEN="${NGROK_AUTHTOKEN}" \ --net=host \ - ngrok/ngrok:latest http ${HOST_PORT} > "${NGROK_LOG}" 2>&1 + -e NGROK_AUTHTOKEN=${NGROK_AUTHTOKEN} \ + ngrok/ngrok:latest http ${HOST_PORT} \ + > /tmp/${NGROK_NAME}.log 2>&1 -# Aguardar até que o túnel esteja pronto -echo "[INFO] Aguardando URL pública do Ngrok..." -URL="" -for i in {1..10}; do - sleep 3 - URL=$(docker logs ${NGROK_NAME} 2>/dev/null | grep -o 'https://[a-zA-Z0-9.-]*\.ngrok-free\.app' | head -n1 || true) - if [ -n "$URL" ]; then - break - fi -done +# Aguardar ngrok subir +sleep 5 + +# Capturar URL do ngrok +URL=$(docker exec ${NGROK_NAME} curl -s http://127.0.0.1:4040/api/tunnels \ + | grep -o '"public_url":"[^"]*"' \ + | head -n 1 \ + | cut -d'"' -f4) if [ -z "$URL" ]; then - echo "[ERRO] Não foi possível capturar a URL do Ngrok. Veja os logs em ${NGROK_LOG}" + echo "[ERRO] Não foi possível capturar a URL do Ngrok. Veja os logs em /tmp/${NGROK_NAME}.log" >&2 exit 1 fi -echo "[INFO] Preview rodando:" -echo " App: ${APP_NAME}" -echo " Porta: ${HOST_PORT}" -echo " URL: ${URL}" - -# Retornar apenas a URL no stdout (para o workflow capturar) +echo "[INFO] Preview disponível em: $URL" echo "$URL" diff --git a/.github/scripts/stop-preview.sh b/.github/scripts/stop-preview.sh index 7f0cc8d..79fd7ce 100644 --- a/.github/scripts/stop-preview.sh +++ b/.github/scripts/stop-preview.sh @@ -4,30 +4,34 @@ set -e PR_NUMBER=$1 APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" -CACHE_DIR="/tmp/.buildx-cache" -echo "[INFO] Removendo preview para PR #${PR_NUMBER}" +echo "[INFO] Removendo preview para PR #${PR_NUMBER}..." -# Se o container do app existir mas não estiver rodando, mostrar logs antes de remover -if docker ps -a --format '{{.Names}} {{.Status}}' | grep -q "${APP_NAME}"; then +# Se o container da aplicação existir mas não estiver rodando, mostrar últimos logs antes de remover +if docker ps -a --format '{{.Names}}' | 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 + echo "[INFO] Removendo container da aplicação ${APP_NAME}..." + docker rm -f ${APP_NAME} >/dev/null 2>&1 || true +else + echo "[INFO] Nenhum container da aplicação encontrado para ${APP_NAME}." fi -# Parar e remover container da aplicação -docker rm -f ${APP_NAME} || true - -# Parar e remover container do Ngrok -docker rm -f ${NGROK_NAME} || true - -# Remover imagem associada ao PR -docker rmi -f ${APP_NAME}:latest || true +# Remover imagem associada ao PR (se existir) +if docker images -q ${APP_NAME}:latest >/dev/null 2>&1; then + echo "[INFO] Removendo imagem Docker ${APP_NAME}:latest..." + docker rmi -f ${APP_NAME}:latest >/dev/null 2>&1 || true +fi -# Limpar camadas órfãs do buildx -if [ -d "${CACHE_DIR}" ]; then - echo "[INFO] Limpando camadas órfãs do buildx..." - docker builder prune -f || true +# Remover container do Ngrok (se existir) +if docker ps -a --format '{{.Names}}' | grep -q "^${NGROK_NAME}$"; then + echo "[INFO] Removendo container do Ngrok ${NGROK_NAME}..." + docker rm -f ${NGROK_NAME} >/dev/null 2>&1 || true +else + echo "[INFO] Nenhum container do Ngrok encontrado para ${NGROK_NAME}." fi + +echo "[INFO] Preview para PR #${PR_NUMBER} removido com sucesso." From 8c2f9783fd807771a4d516dec07fe653b537419c Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 02:25:41 -0300 Subject: [PATCH 29/36] fix: remove cache --- .github/scripts/start-preview.sh | 33 ++++++++++++-------------------- .github/scripts/stop-preview.sh | 29 +++++++++------------------- 2 files changed, 21 insertions(+), 41 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index 07b7365..8a8a9b1 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -6,41 +6,32 @@ APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" HOST_PORT=$((8000 + PR_NUMBER)) -echo "[INFO] Iniciando preview para PR #${PR_NUMBER}..." +echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${HOST_PORT}" -# Remover containers antigos se existirem +# 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 -# Construir imagem da aplicação (sem cache buildx) -echo "[INFO] Construindo imagem Docker para ${APP_NAME}..." +# Build da imagem sem cache docker build -t ${APP_NAME}:latest . -# Rodar container da aplicação -echo "[INFO] Subindo container da aplicação na porta ${HOST_PORT}..." +# Rodar o container da aplicação docker run -d --name ${APP_NAME} -p ${HOST_PORT}:8080 ${APP_NAME}:latest -# Subir ngrok em container separado -echo "[INFO] Subindo túnel Ngrok..." +# Rodar o container do ngrok docker run -d --name ${NGROK_NAME} \ - --net=host \ - -e NGROK_AUTHTOKEN=${NGROK_AUTHTOKEN} \ - ngrok/ngrok:latest http ${HOST_PORT} \ - > /tmp/${NGROK_NAME}.log 2>&1 + -e NGROK_AUTHTOKEN="${NGROK_AUTHTOKEN}" \ + --network host \ + ngrok/ngrok:latest http ${HOST_PORT} > /tmp/${NGROK_NAME}.log 2>&1 & -# Aguardar ngrok subir +# Aguardar o ngrok iniciar e capturar a URL sleep 5 - -# Capturar URL do ngrok -URL=$(docker exec ${NGROK_NAME} curl -s http://127.0.0.1:4040/api/tunnels \ - | grep -o '"public_url":"[^"]*"' \ - | head -n 1 \ - | cut -d'"' -f4) +URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -o 'https://[0-9a-z]*\.ngrok-free\.app' | head -n 1) if [ -z "$URL" ]; then - echo "[ERRO] Não foi possível capturar a URL do Ngrok. Veja os logs em /tmp/${NGROK_NAME}.log" >&2 + 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] Preview disponível em: $URL" +echo "[INFO] URL pública gerada: $URL" echo "$URL" diff --git a/.github/scripts/stop-preview.sh b/.github/scripts/stop-preview.sh index 79fd7ce..7fa3aa0 100644 --- a/.github/scripts/stop-preview.sh +++ b/.github/scripts/stop-preview.sh @@ -5,33 +5,22 @@ PR_NUMBER=$1 APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" -echo "[INFO] Removendo preview para PR #${PR_NUMBER}..." +echo "[INFO] Removendo preview para PR #${PR_NUMBER}" -# Se o container da aplicação existir mas não estiver rodando, mostrar últimos logs antes de remover -if docker ps -a --format '{{.Names}}' | grep -q "^${APP_NAME}$"; then +# Se o container 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 - echo "[INFO] Removendo container da aplicação ${APP_NAME}..." - docker rm -f ${APP_NAME} >/dev/null 2>&1 || true -else - echo "[INFO] Nenhum container da aplicação encontrado para ${APP_NAME}." fi -# Remover imagem associada ao PR (se existir) -if docker images -q ${APP_NAME}:latest >/dev/null 2>&1; then - echo "[INFO] Removendo imagem Docker ${APP_NAME}:latest..." - docker rmi -f ${APP_NAME}:latest >/dev/null 2>&1 || true -fi +# Parar e remover container da aplicação +docker rm -f ${APP_NAME} || true -# Remover container do Ngrok (se existir) -if docker ps -a --format '{{.Names}}' | grep -q "^${NGROK_NAME}$"; then - echo "[INFO] Removendo container do Ngrok ${NGROK_NAME}..." - docker rm -f ${NGROK_NAME} >/dev/null 2>&1 || true -else - echo "[INFO] Nenhum container do Ngrok encontrado para ${NGROK_NAME}." -fi +# Remover imagem associada ao PR +docker rmi -f ${APP_NAME}:latest || true -echo "[INFO] Preview para PR #${PR_NUMBER} removido com sucesso." +# Parar e remover container do ngrok +docker rm -f ${NGROK_NAME} || true From c4b780066f4944af23e2704e9309fa9771c5524a Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 02:34:15 -0300 Subject: [PATCH 30/36] ajuste --- .github/scripts/start-preview.sh | 22 ++++++++++++++++++---- .github/scripts/stop-preview.sh | 18 +++++++++++++----- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index 8a8a9b1..bc9e94e 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -22,14 +22,28 @@ docker run -d --name ${APP_NAME} -p ${HOST_PORT}:8080 ${APP_NAME}:latest docker run -d --name ${NGROK_NAME} \ -e NGROK_AUTHTOKEN="${NGROK_AUTHTOKEN}" \ --network host \ - ngrok/ngrok:latest http ${HOST_PORT} > /tmp/${NGROK_NAME}.log 2>&1 & + ngrok/ngrok:latest http ${HOST_PORT} > /tmp/${NGROK_NAME}.log 2>&1 -# Aguardar o ngrok iniciar e capturar a URL -sleep 5 -URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -o 'https://[0-9a-z]*\.ngrok-free\.app' | head -n 1) +# Confirmar que o container subiu +if ! docker ps --format '{{.Names}}' | grep -q "${NGROK_NAME}"; then + echo "[ERRO] Container do ngrok não iniciou corretamente." + docker logs ${NGROK_NAME} || true + exit 1 +fi + +# Aguardar e tentar capturar a URL até 5 vezes +URL="" +for i in {1..5}; do + sleep 3 + URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -o 'https://[0-9a-z]*\.ngrok-free\.app' | head -n 1) + 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" + docker logs ${NGROK_NAME} || true exit 1 fi diff --git a/.github/scripts/stop-preview.sh b/.github/scripts/stop-preview.sh index 7fa3aa0..861567a 100644 --- a/.github/scripts/stop-preview.sh +++ b/.github/scripts/stop-preview.sh @@ -7,7 +7,7 @@ NGROK_NAME="ngrok-pr${PR_NUMBER}" echo "[INFO] Removendo preview para PR #${PR_NUMBER}" -# Se o container existir mas estiver em estado de falha, mostrar logs antes de remover +# 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 @@ -17,10 +17,18 @@ if docker ps -a --format '{{.Names}} {{.Status}}' | grep -q "${APP_NAME}"; then fi # Parar e remover container da aplicação -docker rm -f ${APP_NAME} || true +docker rm -f ${APP_NAME} >/dev/null 2>&1 || true -# Remover imagem associada ao PR -docker rmi -f ${APP_NAME}:latest || 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} || true +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." From 4df659ee6f4db97faa5dce52d135c3b9400cbc47 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 02:42:09 -0300 Subject: [PATCH 31/36] teste --- .github/scripts/start-preview.sh | 51 ++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index bc9e94e..cd93ec7 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -6,7 +6,7 @@ APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" HOST_PORT=$((8000 + PR_NUMBER)) -echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${HOST_PORT}" +echo "[INFO] Iniciando preview para PR #${PR_NUMBER} (porta base ${HOST_PORT})" # Garantir que não há containers anteriores docker rm -f ${APP_NAME} 2>/dev/null || true @@ -15,37 +15,64 @@ docker rm -f ${NGROK_NAME} 2>/dev/null || true # Build da imagem sem cache docker build -t ${APP_NAME}:latest . -# Rodar o container da aplicação -docker run -d --name ${APP_NAME} -p ${HOST_PORT}:8080 ${APP_NAME}:latest +# Tentar iniciar o app na porta base; se ocupada, tentar próximas +SELECTED_PORT=${HOST_PORT} +STARTED=0 +for P in $(seq ${HOST_PORT} $((HOST_PORT+20))); do + # Verifica se a porta está livre (ss geralmente existe em Ubuntu) + if ss -ltn 2>/dev/null | awk '{print $4}' | grep -q ":${P}$"; then + echo "[WARN] Porta ${P} em uso, tentando a próxima..." + continue + fi + + # Tenta subir o container mapeando a porta + if docker run -d --name ${APP_NAME} -p ${P}:8080 ${APP_NAME}:latest >/dev/null 2>&1; then + SELECTED_PORT=${P} + STARTED=1 + break + else + echo "[WARN] Falha ao iniciar app na porta ${P}, tentando a próxima..." + docker rm -f ${APP_NAME} >/dev/null 2>&1 || true + fi +done + +if [ ${STARTED} -ne 1 ]; then + echo "[ERRO] Não foi possível iniciar o container da aplicação em nenhuma porta entre ${HOST_PORT} e $((HOST_PORT+20))." + exit 1 +fi + +echo "[INFO] Aplicação rodando na porta ${SELECTED_PORT}" -# Rodar o container do ngrok +# Subir o container do ngrok expondo a porta efetiva no host docker run -d --name ${NGROK_NAME} \ -e NGROK_AUTHTOKEN="${NGROK_AUTHTOKEN}" \ --network host \ - ngrok/ngrok:latest http ${HOST_PORT} > /tmp/${NGROK_NAME}.log 2>&1 + ngrok/ngrok:latest http ${SELECTED_PORT} -# Confirmar que o container subiu -if ! docker ps --format '{{.Names}}' | grep -q "${NGROK_NAME}"; then +# Confirmar que o container do ngrok subiu +if ! docker ps --format '{{.Names}}' | grep -q "^${NGROK_NAME}$"; then echo "[ERRO] Container do ngrok não iniciou corretamente." docker logs ${NGROK_NAME} || true exit 1 fi -# Aguardar e tentar capturar a URL até 5 vezes +# Aguardar e tentar capturar a URL até 10 vezes URL="" -for i in {1..5}; do - sleep 3 - URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -o 'https://[0-9a-z]*\.ngrok-free\.app' | head -n 1) +for i in {1..10}; do + sleep 2 + URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -o 'https://[0-9a-zA-Z.-]*\.ngrok-free\.app' | head -n 1 || true) if [ -n "$URL" ]; then break fi + echo "[INFO] Aguardando URL do Ngrok... Tentativa $i/10" 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" + echo "[ERRO] Não foi possível capturar a URL do Ngrok." docker logs ${NGROK_NAME} || true exit 1 fi echo "[INFO] URL pública gerada: $URL" +# A ÚLTIMA linha precisa ser só a URL (o workflow captura com tail -n 1) echo "$URL" From b50881666f466deb5a11e05fb3fa3029c61cf762 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 02:52:30 -0300 Subject: [PATCH 32/36] teste --- .github/scripts/start-preview.sh | 69 ++++++++------------------------ 1 file changed, 17 insertions(+), 52 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index cd93ec7..0cdf7b1 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -6,73 +6,38 @@ APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" HOST_PORT=$((8000 + PR_NUMBER)) -echo "[INFO] Iniciando preview para PR #${PR_NUMBER} (porta base ${HOST_PORT})" +# Logs sempre no stderr +>&2 echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${HOST_PORT}" # 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 da imagem sem cache -docker build -t ${APP_NAME}:latest . +>&2 echo "[INFO] Build da imagem Docker..." +docker build -t ${APP_NAME}:latest . >&2 -# Tentar iniciar o app na porta base; se ocupada, tentar próximas -SELECTED_PORT=${HOST_PORT} -STARTED=0 -for P in $(seq ${HOST_PORT} $((HOST_PORT+20))); do - # Verifica se a porta está livre (ss geralmente existe em Ubuntu) - if ss -ltn 2>/dev/null | awk '{print $4}' | grep -q ":${P}$"; then - echo "[WARN] Porta ${P} em uso, tentando a próxima..." - continue - fi +# Rodar o container da aplicação +>&2 echo "[INFO] Subindo container da aplicação ${APP_NAME}..." +docker run -d --name ${APP_NAME} -p ${HOST_PORT}:8080 ${APP_NAME}:latest >&2 - # Tenta subir o container mapeando a porta - if docker run -d --name ${APP_NAME} -p ${P}:8080 ${APP_NAME}:latest >/dev/null 2>&1; then - SELECTED_PORT=${P} - STARTED=1 - break - else - echo "[WARN] Falha ao iniciar app na porta ${P}, tentando a próxima..." - docker rm -f ${APP_NAME} >/dev/null 2>&1 || true - fi -done - -if [ ${STARTED} -ne 1 ]; then - echo "[ERRO] Não foi possível iniciar o container da aplicação em nenhuma porta entre ${HOST_PORT} e $((HOST_PORT+20))." - exit 1 -fi - -echo "[INFO] Aplicação rodando na porta ${SELECTED_PORT}" - -# Subir o container do ngrok expondo a porta efetiva no host +# Rodar o container do ngrok +>&2 echo "[INFO] Subindo container do ngrok ${NGROK_NAME}..." docker run -d --name ${NGROK_NAME} \ -e NGROK_AUTHTOKEN="${NGROK_AUTHTOKEN}" \ --network host \ - ngrok/ngrok:latest http ${SELECTED_PORT} + ngrok/ngrok:latest http ${HOST_PORT} > /tmp/${NGROK_NAME}.log 2>&1 & -# Confirmar que o container do ngrok subiu -if ! docker ps --format '{{.Names}}' | grep -q "^${NGROK_NAME}$"; then - echo "[ERRO] Container do ngrok não iniciou corretamente." - docker logs ${NGROK_NAME} || true - exit 1 -fi +# Aguardar o ngrok iniciar +sleep 5 -# Aguardar e tentar capturar a URL até 10 vezes -URL="" -for i in {1..10}; do - sleep 2 - URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -o 'https://[0-9a-zA-Z.-]*\.ngrok-free\.app' | head -n 1 || true) - if [ -n "$URL" ]; then - break - fi - echo "[INFO] Aguardando URL do Ngrok... Tentativa $i/10" -done +# Capturar a URL do ngrok +URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -o 'https://[0-9a-z]*\.ngrok-free\.app' | head -n 1) if [ -z "$URL" ]; then - echo "[ERRO] Não foi possível capturar a URL do Ngrok." - docker logs ${NGROK_NAME} || true + >&2 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" -# A ÚLTIMA linha precisa ser só a URL (o workflow captura com tail -n 1) -echo "$URL" +>&2 echo "[INFO] URL pública gerada: $URL" +echo "$URL" # <-- ÚNICA saída em stdout From 26f4c3c98ee13a86eb8e13c5080b1f15880520d1 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 02:59:36 -0300 Subject: [PATCH 33/36] teste --- .github/scripts/start-preview.sh | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index 0cdf7b1..f405eba 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -6,38 +6,37 @@ APP_NAME="simple-project-pr${PR_NUMBER}" NGROK_NAME="ngrok-pr${PR_NUMBER}" HOST_PORT=$((8000 + PR_NUMBER)) -# Logs sempre no stderr ->&2 echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${HOST_PORT}" +echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${HOST_PORT}" # 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 +echo "[INFO] Build da imagem Docker..." # Build da imagem sem cache ->&2 echo "[INFO] Build da imagem Docker..." -docker build -t ${APP_NAME}:latest . >&2 +docker build -t ${APP_NAME}:latest . +echo "[INFO] Subindo container da aplicação ${APP_NAME}..." # Rodar o container da aplicação ->&2 echo "[INFO] Subindo container da aplicação ${APP_NAME}..." -docker run -d --name ${APP_NAME} -p ${HOST_PORT}:8080 ${APP_NAME}:latest >&2 +docker run -d --name ${APP_NAME} -p ${HOST_PORT}:8080 ${APP_NAME}:latest -# Rodar o container do ngrok ->&2 echo "[INFO] Subindo container do ngrok ${NGROK_NAME}..." +echo "[INFO] Subindo container do ngrok ${NGROK_NAME}..." +# Rodar o container do ngrok com a API exposta docker run -d --name ${NGROK_NAME} \ -e NGROK_AUTHTOKEN="${NGROK_AUTHTOKEN}" \ - --network host \ - ngrok/ngrok:latest http ${HOST_PORT} > /tmp/${NGROK_NAME}.log 2>&1 & + -p 4040:4040 \ + ngrok/ngrok:latest http ${HOST_PORT} > /tmp/${NGROK_NAME}.log 2>&1 # Aguardar o ngrok iniciar sleep 5 -# Capturar a URL do ngrok -URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -o 'https://[0-9a-z]*\.ngrok-free\.app' | head -n 1) +# Capturar a URL pública via API local +URL=$(curl --silent http://127.0.0.1:4040/api/tunnels | jq -r '.tunnels[0].public_url') -if [ -z "$URL" ]; then - >&2 echo "[ERRO] Não foi possível capturar a URL do Ngrok. Veja os logs em /tmp/${NGROK_NAME}.log" +if [ -z "$URL" ] || [ "$URL" == "null" ]; then + echo "[ERRO] Não foi possível capturar a URL do Ngrok. Veja os logs em /tmp/${NGROK_NAME}.log" exit 1 fi ->&2 echo "[INFO] URL pública gerada: $URL" -echo "$URL" # <-- ÚNICA saída em stdout +echo "[INFO] URL pública gerada: $URL" +echo "$URL" From ba108939dffd132a75e51eac74447049e6026f72 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 03:07:50 -0300 Subject: [PATCH 34/36] fix: persmission for marocchino -sticky-comment put a comment on PR --- .github/workflows/pr-preview.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 9fb4510..105465a 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -4,6 +4,10 @@ on: pull_request: types: [opened, synchronize, reopened, closed] +permissions: + pull-requests: write + contents: read + jobs: preview: runs-on: self-hosted From 54713679349d18e4d808895dadf85b1503b87462 Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 03:11:47 -0300 Subject: [PATCH 35/36] obrigado --- .github/scripts/start-preview.sh | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index f405eba..e519baa 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -21,19 +21,17 @@ echo "[INFO] Subindo container da aplicação ${APP_NAME}..." docker run -d --name ${APP_NAME} -p ${HOST_PORT}:8080 ${APP_NAME}:latest echo "[INFO] Subindo container do ngrok ${NGROK_NAME}..." -# Rodar o container do ngrok com a API exposta +# Rodar o container do ngrok (usando rede do host) docker run -d --name ${NGROK_NAME} \ -e NGROK_AUTHTOKEN="${NGROK_AUTHTOKEN}" \ - -p 4040:4040 \ + --network host \ ngrok/ngrok:latest http ${HOST_PORT} > /tmp/${NGROK_NAME}.log 2>&1 -# Aguardar o ngrok iniciar +# Aguardar o ngrok iniciar e capturar a URL sleep 5 +URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -o 'https://[0-9a-z]*\.ngrok-free\.app' | head -n 1) -# Capturar a URL pública via API local -URL=$(curl --silent http://127.0.0.1:4040/api/tunnels | jq -r '.tunnels[0].public_url') - -if [ -z "$URL" ] || [ "$URL" == "null" ]; then +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 From 9e481bf9aa9943c47aeebc91041d61336054ce7b Mon Sep 17 00:00:00 2001 From: Andre Luis Gomes da Silva Date: Wed, 24 Sep 2025 03:15:43 -0300 Subject: [PATCH 36/36] teste --- .github/scripts/start-preview.sh | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/.github/scripts/start-preview.sh b/.github/scripts/start-preview.sh index e519baa..fc35d7d 100644 --- a/.github/scripts/start-preview.sh +++ b/.github/scripts/start-preview.sh @@ -5,6 +5,7 @@ PR_NUMBER=$1 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 ${HOST_PORT}" @@ -12,24 +13,30 @@ echo "[INFO] Iniciando preview para PR #${PR_NUMBER} na porta ${HOST_PORT}" docker rm -f ${APP_NAME} 2>/dev/null || true docker rm -f ${NGROK_NAME} 2>/dev/null || true -echo "[INFO] Build da imagem Docker..." # Build da imagem sem cache +echo "[INFO] Build da imagem Docker..." docker build -t ${APP_NAME}:latest . -echo "[INFO] Subindo container da aplicação ${APP_NAME}..." # 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}..." -# Rodar o container do ngrok (usando rede do host) docker run -d --name ${NGROK_NAME} \ -e NGROK_AUTHTOKEN="${NGROK_AUTHTOKEN}" \ - --network host \ - ngrok/ngrok:latest http ${HOST_PORT} > /tmp/${NGROK_NAME}.log 2>&1 - -# Aguardar o ngrok iniciar e capturar a URL -sleep 5 -URL=$(docker logs ${NGROK_NAME} 2>&1 | grep -o 'https://[0-9a-z]*\.ngrok-free\.app' | head -n 1) + -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"