172 lines
6.8 KiB
YAML
172 lines
6.8 KiB
YAML
name: Docker - Build e Publicacao
|
|
|
|
# Publicacao no registry do Gitea (git.jf.eng.br/jfeng/evogo):
|
|
# - push de tag v<versao> (ex: v0.7.2-evogo.1) = release aprovado -> publica
|
|
# git.jf.eng.br/jfeng/evogo:<versao> (tag semantica)
|
|
# git.jf.eng.br/jfeng/evogo:<versao>-<sha7> (tag imutavel do commit)
|
|
# - workflow_dispatch com publish_image=true -> idem, versao = target_tag ou VERSION
|
|
# - push em maintained-stable -> apenas build + teste + scan (nao publica)
|
|
# Tags v* aprovadas em maintained-stable tambem promovem :latest apos o push
|
|
# das tags imutavel e semantica, evitando a corrida com promote-latest.yml.
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- maintained-stable
|
|
tags:
|
|
- 'v*'
|
|
workflow_dispatch:
|
|
inputs:
|
|
publish_image:
|
|
description: 'Publicar imagem no registry apos build e testes (requer aprovacao)'
|
|
type: boolean
|
|
default: false
|
|
required: true
|
|
target_tag:
|
|
description: 'Tag semantica para publicacao (ex: 0.7.2-evogo.1). Vazio = arquivo VERSION'
|
|
type: string
|
|
default: ''
|
|
required: false
|
|
promote_latest:
|
|
description: 'Tambem atualizar :latest (somente versoes aprovadas em maintained-stable)'
|
|
type: boolean
|
|
default: false
|
|
required: false
|
|
|
|
concurrency:
|
|
group: promote-latest
|
|
cancel-in-progress: false
|
|
|
|
env:
|
|
REGISTRY: git.jf.eng.br
|
|
IMAGE: git.jf.eng.br/jfeng/evogo
|
|
|
|
jobs:
|
|
build_docker:
|
|
name: Build, Scan e Publicacao
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout do Repositorio
|
|
uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- name: Validar origem da promocao latest
|
|
id: promotion
|
|
run: |
|
|
PROMOTE=false
|
|
if { [ "${{ github.event_name }}" = "push" ] && [ "${{ github.ref_type }}" = "tag" ]; } || [ "${{ github.event.inputs.promote_latest }}" = "true" ]; then
|
|
git fetch origin maintained-stable
|
|
if git merge-base --is-ancestor HEAD FETCH_HEAD; then
|
|
PROMOTE=true
|
|
elif [ "${{ github.event.inputs.promote_latest }}" = "true" ]; then
|
|
echo "Promocao recusada: commit nao aprovado em maintained-stable"
|
|
exit 1
|
|
fi
|
|
fi
|
|
echo "promote=${PROMOTE}" >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Configurar Go
|
|
uses: actions/setup-go@v5
|
|
with:
|
|
go-version-file: go.mod
|
|
|
|
- name: Validar codigo antes da publicacao
|
|
run: |
|
|
sudo apt-get update
|
|
sudo apt-get install -y --no-install-recommends libjpeg-dev libwebp-dev poppler-utils ffmpeg
|
|
go mod verify
|
|
go vet ./...
|
|
go test -v ./...
|
|
|
|
- name: Obter Versao e Commit SHA
|
|
id: meta
|
|
run: |
|
|
COMMIT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)
|
|
if [ "${{ github.ref_type }}" = "tag" ]; then
|
|
VERSION="${{ github.ref_name }}"
|
|
VERSION="${VERSION#v}"
|
|
PUBLISH=true
|
|
else
|
|
VERSION="${{ github.event.inputs.target_tag }}"
|
|
[ -n "$VERSION" ] || VERSION=$(cat VERSION 2>/dev/null || echo "0.0.0")
|
|
PUBLISH="${{ github.event.inputs.publish_image == 'true' }}"
|
|
fi
|
|
echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
|
|
echo "commit_sha=${COMMIT_SHA}" >> "$GITHUB_OUTPUT"
|
|
echo "image_tag=${VERSION}-${COMMIT_SHA}" >> "$GITHUB_OUTPUT"
|
|
echo "publish=${PUBLISH}" >> "$GITHUB_OUTPUT"
|
|
echo "Versao: ${VERSION} | Commit: ${COMMIT_SHA} | Publicar: ${PUBLISH}"
|
|
|
|
- name: Construir Imagem Docker Local
|
|
run: |
|
|
docker build \
|
|
--build-arg VERSION=${{ steps.meta.outputs.image_tag }} \
|
|
-t evogo:ci-${{ steps.meta.outputs.commit_sha }} .
|
|
|
|
- name: Testar Inicializacao do Container
|
|
run: |
|
|
# O binario precisa subir e falhar apenas por falta de banco/config,
|
|
# nunca por erro de link (CGO/libwebp/libjpeg) ou arquivo ausente.
|
|
set +e
|
|
timeout 20 docker run --rm evogo:ci-${{ steps.meta.outputs.commit_sha }} > /tmp/run.log 2>&1
|
|
set -e
|
|
tail -20 /tmp/run.log
|
|
if grep -qiE "error while loading shared libraries|exec format error|exec /app/server" /tmp/run.log; then
|
|
echo "Falha ao iniciar o binario dentro da imagem"; exit 1
|
|
fi
|
|
docker run --rm --entrypoint sh evogo:ci-${{ steps.meta.outputs.commit_sha }} -c 'test -x /app/server && test -d /app/manager/dist && cat /app/VERSION'
|
|
|
|
- name: Verificar Vulnerabilidades (Trivy, nao bloqueante)
|
|
continue-on-error: true
|
|
run: |
|
|
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy:latest \
|
|
image --severity HIGH,CRITICAL --ignore-unfixed --no-progress \
|
|
evogo:ci-${{ steps.meta.outputs.commit_sha }}
|
|
|
|
- name: Login no Gitea Package Registry
|
|
if: steps.meta.outputs.publish == 'true'
|
|
run: |
|
|
USER="${{ secrets.REGISTRY_USER }}"
|
|
[ -n "$USER" ] || USER="${{ github.actor }}"
|
|
TOKEN="${{ secrets.REGISTRY_TOKEN }}"
|
|
[ -n "$TOKEN" ] || TOKEN="${{ secrets.GITEA_TOKEN }}"
|
|
# O registry ja estourou timeout pontualmente (run 482); tenta de novo antes de falhar.
|
|
for n in 1 2 3 4 5; do
|
|
echo "$TOKEN" | docker login "$REGISTRY" -u "$USER" --password-stdin && exit 0
|
|
echo "Login falhou (tentativa $n/5); nova tentativa em $((n*15))s"
|
|
sleep $((n*15))
|
|
done
|
|
exit 1
|
|
|
|
- name: Publicar Imagens no Registry
|
|
if: steps.meta.outputs.publish == 'true'
|
|
run: |
|
|
push() {
|
|
for n in 1 2 3 4 5; do
|
|
docker push "$1" && return 0
|
|
echo "Push de $1 falhou (tentativa $n/5); nova tentativa em $((n*15))s"
|
|
sleep $((n*15))
|
|
done
|
|
return 1
|
|
}
|
|
SRC=evogo:ci-${{ steps.meta.outputs.commit_sha }}
|
|
for TAG in "${{ steps.meta.outputs.image_tag }}" "${{ steps.meta.outputs.version }}"; do
|
|
docker tag "$SRC" "$IMAGE:$TAG"
|
|
push "$IMAGE:$TAG"
|
|
echo "Publicado: $IMAGE:$TAG"
|
|
done
|
|
if [ "${{ steps.promotion.outputs.promote }}" = "true" ]; then
|
|
docker tag "$SRC" "$IMAGE:latest"
|
|
push "$IMAGE:latest"
|
|
IMMUTABLE_DIGEST=$(docker buildx imagetools inspect "$IMAGE:${{ steps.meta.outputs.image_tag }}" --format '{{json .Manifest.Digest}}')
|
|
LATEST_DIGEST=$(docker buildx imagetools inspect "$IMAGE:latest" --format '{{json .Manifest.Digest}}')
|
|
[ -n "$IMMUTABLE_DIGEST" ] && [ "$IMMUTABLE_DIGEST" = "$LATEST_DIGEST" ]
|
|
echo "Publicado e verificado: $IMAGE:latest = $IMMUTABLE_DIGEST"
|
|
fi
|
|
|
|
- name: Limpeza
|
|
if: always()
|
|
run: docker image rm -f evogo:ci-${{ steps.meta.outputs.commit_sha }} >/dev/null 2>&1 || true
|