release.yml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. name: release
  2. # Release estable por tag v*: goreleaser construye netpulse (con el frontend
  3. # embebido) y netpulse-collector para linux/amd64|arm64|armv7 + checksums.txt.
  4. # install.sh / install-collector.sh consumen estos assets.
  5. # Distinto de go.yml: aquello publica binarios rolling del commit en la
  6. # prerelease go-latest (para el updater del CT 226); esto publica versiones.
  7. on:
  8. push:
  9. tags: ['v*']
  10. workflow_dispatch: # sin tag = snapshot de prueba (no publica release)
  11. permissions:
  12. contents: write
  13. jobs:
  14. release:
  15. runs-on: ubuntu-latest
  16. steps:
  17. - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
  18. with:
  19. fetch-depth: 0 # tags e historia: el gate de agent/VERSION difiere contra el tag anterior
  20. - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
  21. with:
  22. node-version: 24
  23. cache: npm
  24. cache-dependency-path: app/package-lock.json
  25. - uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0
  26. with:
  27. go-version: '1.26'
  28. cache-dependency-path: |
  29. server-go/go.sum
  30. collector/go.sum
  31. # Versión del agente: fuente única de verdad en agent/VERSION (#790),
  32. # desacoplada del tag del server. Se inyecta igual en el binario del
  33. # agente (main.Version) y en el servidor (agentbin.EmbeddedAgentVersion
  34. # vía .goreleaser.yaml): alimenta el campo updateAvailable de
  35. # GET /api/agents, así que solo debe subir cuando el código de agent/
  36. # cambia de verdad. Gate de consistencia: si agent/ cambió desde el tag
  37. # anterior y agent/VERSION no subió, la release falla (obliga al bump).
  38. - name: Resuelve AGENT_VERSION desde agent/VERSION
  39. id: version
  40. run: |
  41. set -e
  42. AGENT_VERSION=$(tr -d '[:space:]' < agent/VERSION)
  43. if [ -z "$AGENT_VERSION" ]; then
  44. echo "::error::agent/VERSION está vacío"
  45. exit 1
  46. fi
  47. echo "agent=$AGENT_VERSION" >> "$GITHUB_OUTPUT"
  48. PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || true)
  49. if [ -n "$PREV_TAG" ]; then
  50. PREV_AGENT=$(git show "$PREV_TAG:agent/VERSION" 2>/dev/null | tr -d '[:space:]' || true)
  51. if ! git diff --quiet "$PREV_TAG"..HEAD -- agent/; then
  52. if [ -n "$PREV_AGENT" ] && [ "$PREV_AGENT" = "$AGENT_VERSION" ]; then
  53. echo "::error::agent/ cambió desde $PREV_TAG pero agent/VERSION sigue en $AGENT_VERSION. Sube la versión del agente antes de taguear."
  54. exit 1
  55. fi
  56. fi
  57. # #1840: un release no puede rebajar la versión del agente. Si lo
  58. # hace, el servidor embebe un agente más viejo que el que ya corre
  59. # en los routers y un reinstall degradaría la flota.
  60. if [ -n "$PREV_AGENT" ] && [ "$PREV_AGENT" != "$AGENT_VERSION" ]; then
  61. NEWEST=$(printf '%s\n%s\n' "$PREV_AGENT" "$AGENT_VERSION" | sort -V | tail -n 1)
  62. if [ "$NEWEST" = "$PREV_AGENT" ]; then
  63. echo "::error::agent/VERSION bajó de $PREV_AGENT ($PREV_TAG) a $AGENT_VERSION. Publicar un servidor con un agente embebido más viejo que el de la flota no está permitido (#1840)."
  64. exit 1
  65. fi
  66. fi
  67. fi
  68. - name: Build frontend + copia dist al embed (necesario para go:embed y tests)
  69. run: |
  70. npm ci --no-audit --no-fund --prefix app
  71. npm run build --prefix app
  72. rm -rf server-go/internal/staticspa/dist && cp -r app/dist server-go/internal/staticspa/dist
  73. # Antes de los tests: TestOpenArmVariants abre los binarios embebidos
  74. # (mismo orden que go.yml desde #459; aquí faltó aplicarlo y las
  75. # releases v2.25.0+ se quedaron sin assets). OJO los nombres: Open()
  76. # resuelve "agents/netpulse-agent-" + normalizeArch(arch) y esta mapea
  77. # armv7/armv7l/armhf -> arm, asi que el fichero se llama
  78. # netpulse-agent-arm (con GOARM=7), NO netpulse-agent-armv7 (#458).
  79. # #1469: cuando agent/VERSION no cambió respecto al tag anterior, los
  80. # binarios del agente son los mismos que la release previa: se
  81. # descargan sus assets y se extraen al embed en vez de recompilar 5
  82. # arquitecturas. Garantiza además embed == assets bit a bit. Cualquier
  83. # fallo de la descarga o la verificación deja reused=false y el paso
  84. # de compilación (fallback) corre encima, sobreescribiendo lo extraído.
  85. - name: Reutiliza agentes del release anterior (si agent/VERSION no cambió)
  86. id: agent-reuse
  87. env:
  88. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  89. run: |
  90. set -e
  91. PREV_TAG=$(git describe --tags --abbrev=0 --match 'v[0-9]*' HEAD^ 2>/dev/null || true)
  92. AGENT_VERSION=${{ steps.version.outputs.agent }}
  93. if [ -z "$PREV_TAG" ]; then
  94. echo "sin tag anterior: se compila"
  95. echo "reused=false" >> "$GITHUB_OUTPUT"
  96. exit 0
  97. fi
  98. PREV_AGENT=$(git show "$PREV_TAG:agent/VERSION" 2>/dev/null | tr -d '[:space:]' || true)
  99. if [ "$PREV_AGENT" != "$AGENT_VERSION" ]; then
  100. echo "agente cambió ($PREV_AGENT -> $AGENT_VERSION): se compila"
  101. echo "reused=false" >> "$GITHUB_OUTPUT"
  102. exit 0
  103. fi
  104. reuse() {
  105. # set -e NO aplica dentro de una función llamada en condición:
  106. # cada paso crítico aborta explícitamente con || return 1.
  107. TMP=$(mktemp -d) || return 1
  108. cd "$TMP" || return 1
  109. # -R explícito: dentro de $TMP no hay repositorio git y gh resuelve
  110. # el remoto con `git remote get-url`, que falla con "not a git
  111. # repository" (#1916). Con el repo en la variable del entorno no
  112. # depende del directorio de trabajo.
  113. for arch in amd64 arm64 armv7 mipsle mips; do
  114. gh release download "$PREV_TAG" -R "$GITHUB_REPOSITORY" --pattern "netpulse-agent_*_linux_${arch}.tar.gz" --clobber || return 1
  115. done
  116. # Los tarballs deben casar con el checksums.txt del release
  117. # anterior (filtrado a los assets del agente).
  118. gh release download "$PREV_TAG" -R "$GITHUB_REPOSITORY" --pattern checksums.txt --clobber || return 1
  119. grep 'netpulse-agent_.*_linux_.*\.tar\.gz' checksums.txt > agent-checksums.txt || return 1
  120. sha256sum -c agent-checksums.txt || return 1
  121. NAMES="amd64:amd64 arm64:arm64 armv7:arm mipsle:mipsle mips:mips"
  122. for pair in $NAMES; do
  123. arch="${pair%%:*}"; name="${pair##*:}"
  124. tarball=$(ls netpulse-agent_*_linux_${arch}.tar.gz) || return 1
  125. tar xzf "$tarball" netpulse-agent || return 1
  126. mv netpulse-agent "$GITHUB_WORKSPACE/server-go/internal/agentbin/agents/netpulse-agent-${name}" || return 1
  127. chmod +x "$GITHUB_WORKSPACE/server-go/internal/agentbin/agents/netpulse-agent-${name}" || return 1
  128. done
  129. # Sello de versión (#1914): build-agents.sh lo escribe al compilar y
  130. # verifyFSVersion lo lee porque los agentes van comprimidos con UPX
  131. # y el literal no está en claro. Al reutilizar no se compila, así que
  132. # el sello hay que dejarlo aquí o el guard de #1246 vuelve a
  133. # desactivar los upgrades de agente.
  134. printf '%s\n' "$AGENT_VERSION" > "$GITHUB_WORKSPACE/server-go/internal/agentbin/agents/agent.version" || return 1
  135. }
  136. if reuse; then
  137. echo "agente $AGENT_VERSION reutilizado de $PREV_TAG"
  138. echo "reused=true" >> "$GITHUB_OUTPUT"
  139. else
  140. echo "::warning::reutilización del agente falló; se compila"
  141. echo "reused=false" >> "$GITHUB_OUTPUT"
  142. fi
  143. - name: Instala upx
  144. run: sudo apt-get update && sudo apt-get install -y upx-ucl
  145. - name: Build agent binaries para embed (todas las arquitecturas)
  146. if: steps.agent-reuse.outputs.reused != 'true'
  147. run: scripts/build-agents.sh "${{ steps.version.outputs.agent }}"
  148. - name: Tests Go (ambos módulos)
  149. run: |
  150. (cd server-go && go test ./...)
  151. (cd collector && go test ./...)
  152. - name: Valida config goreleaser
  153. uses: goreleaser/goreleaser-action@e435ccd777264be153ace6237001ef4d979d3a7a # v6.4.0
  154. with:
  155. version: '~> v2'
  156. args: check
  157. - name: Release (tag v* — publica)
  158. if: github.ref_type == 'tag'
  159. uses: goreleaser/goreleaser-action@e435ccd777264be153ace6237001ef4d979d3a7a # v6.4.0
  160. with:
  161. version: '~> v2'
  162. args: release --clean
  163. env:
  164. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  165. AGENT_VERSION: ${{ steps.version.outputs.agent }}
  166. - name: Release (workflow_dispatch — snapshot sin publicar)
  167. if: github.ref_type != 'tag'
  168. uses: goreleaser/goreleaser-action@e435ccd777264be153ace6237001ef4d979d3a7a # v6.4.0
  169. with:
  170. version: '~> v2'
  171. args: release --clean --snapshot
  172. env:
  173. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  174. AGENT_VERSION: ${{ steps.version.outputs.agent }}
  175. # #1890: goreleaser SALTA upx en silencio si no está en PATH, y el
  176. # resultado sería un release con agentes de 7-9 MB. Verifica que los
  177. # binarios que van en los tarballs están empaquetados y son válidos.
  178. - name: Verifica que los agentes del release van con upx
  179. if: github.ref_type == 'tag'
  180. run: |
  181. set -e
  182. find dist -name netpulse-agent -type f | while read -r bin; do
  183. tail -c 256 "$bin" | grep -qa "UPX!" || { echo "::error::$bin no está empaquetado con upx"; exit 1; }
  184. upx -t "$bin" >/dev/null
  185. echo "$bin $(du -h "$bin" | cut -f1)"
  186. done
  187. # #1656: el demo online es una copia estatica de app/dist, y hasta ahora
  188. # se publicaba a mano, asi que se quedaba atras sin que nada fallara.
  189. # Se publica aqui el MISMO dist que va embebido en el binario; el host del
  190. # demo lo recoge solo con deploy/publish-demo.sh (modelo pull, sin
  191. # credenciales de ese host en CI) y compara el bundle servido.
  192. - name: Publica el artefacto del demo (app/dist)
  193. if: github.ref_type == 'tag'
  194. env:
  195. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  196. run: |
  197. set -e
  198. tar czf "app-dist-${GITHUB_REF_NAME}.tar.gz" -C app/dist .
  199. sha256sum "app-dist-${GITHUB_REF_NAME}.tar.gz" > app-dist.sha256
  200. gh release upload "${GITHUB_REF_NAME}" "app-dist-${GITHUB_REF_NAME}.tar.gz" app-dist.sha256 --clobber