71 lines
2.7 KiB
YAML
71 lines
2.7 KiB
YAML
name: CD
|
|
|
|
on:
|
|
push:
|
|
branches: [master, main]
|
|
pull_request:
|
|
|
|
env:
|
|
IMAGE_BASE: ${{ secrets.AR_LOCATION }}-docker.pkg.dev/${{ secrets.GCP_PROJECT }}/${{ secrets.AR_REPO }}
|
|
|
|
jobs:
|
|
build:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
# Imagem default do runner (node:18-bullseye) não traz docker CLI nem kubectl —
|
|
# instalamos sob demanda. Eventualmente isso vai pra imagem custom do runner.
|
|
- name: Install docker CLI + kubectl
|
|
run: |
|
|
apt-get update -qq
|
|
apt-get install -y -qq docker.io
|
|
KVER=$(curl -sL https://dl.k8s.io/release/stable.txt)
|
|
curl -sL "https://dl.k8s.io/release/${KVER}/bin/linux/amd64/kubectl" -o /usr/local/bin/kubectl
|
|
chmod +x /usr/local/bin/kubectl
|
|
docker --version && kubectl version --client=true
|
|
|
|
- name: Auth GCP
|
|
uses: google-github-actions/auth@v2
|
|
with:
|
|
credentials_json: ${{ secrets.GCP_SA_KEY }}
|
|
|
|
- name: Setup gcloud
|
|
uses: google-github-actions/setup-gcloud@v2
|
|
with:
|
|
project_id: ${{ secrets.GCP_PROJECT }}
|
|
|
|
- name: Configure Docker auth
|
|
run: gcloud auth configure-docker ${{ secrets.AR_LOCATION }}-docker.pkg.dev --quiet
|
|
|
|
- name: Build image
|
|
run: |
|
|
IMG="${IMAGE_BASE}/${{ gitea.event.repository.name }}:lab-${{ gitea.run_number }}"
|
|
docker build --platform=linux/amd64 -t "$IMG" .
|
|
echo "IMG=$IMG" >> $GITHUB_ENV
|
|
|
|
- name: Push image (apenas em push pra master/main)
|
|
if: github.event_name == 'push'
|
|
run: docker push "$IMG"
|
|
|
|
- name: Deploy hml2 (apenas em push pra master/main)
|
|
if: github.event_name == 'push'
|
|
run: |
|
|
gcloud container clusters get-credentials ${{ secrets.GKE_CLUSTER }} --region ${{ secrets.GKE_REGION }} --project ${{ secrets.GCP_PROJECT }}
|
|
NS=${{ secrets.K8S_NAMESPACE }}
|
|
|
|
# 1) Aplica manifests (idempotente — cria PVC/Service/Ingress/Deployment se faltarem)
|
|
if [ -d k8s ]; then
|
|
kubectl apply -n "$NS" -f k8s/
|
|
fi
|
|
|
|
# 2) Atualiza image
|
|
DEPLOYMENT="${{ gitea.event.repository.name }}-deployment"
|
|
if kubectl get deployment "$DEPLOYMENT" -n "$NS" >/dev/null 2>&1; then
|
|
CONTAINER=$(kubectl get deployment "$DEPLOYMENT" -n "$NS" -o jsonpath='{.spec.template.spec.containers[0].name}')
|
|
kubectl set image deployment/"$DEPLOYMENT" -n "$NS" "$CONTAINER=$IMG"
|
|
kubectl rollout status deployment/"$DEPLOYMENT" -n "$NS" --timeout=300s
|
|
else
|
|
echo "Deployment $DEPLOYMENT não existe no ns $NS — pulei set image (provavelmente é o 1º deploy e o kubectl apply acabou de criar)"
|
|
fi
|