Skip to content

DeployClient

DeployClient #224

Workflow file for this run

name: DeployClient
on:
workflow_run:
workflows: [ClientCI]
types:
- completed
branches:
- main
workflow_dispatch:
concurrency:
group: "dfe-${{ github.head_ref }}"
cancel-in-progress: true
jobs:
deploy:
strategy:
matrix:
node-version: [20.x]
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4.0.3
with:
node-version: ${{ matrix.node-version }}
- name: Setup/ Install pnpm 9.7.0 manually
working-directory: ./client
run: npm install -g pnpm@9.7.0
# 1. Autenticarse con google cloud
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
with:
project_id: "${{ secrets.GCLOUD_PROJECT_ID }}"
credentials_json: "${{ secrets.GCLOUD_SERVICE_ACCOUNT_KEY }}"
# 2. Set up Google Cloud SDK
- name: Set up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v2
# 3. Exportar la región como variable de entorno
- name: Export region
working-directory: ./client
run: echo "GCLOUD_REGION=${{ secrets.GCLOUD_REGION }}" >> $GITHUB_ENV
# 4. Autenticarse con Artifact Registry
- name: Authenticate with Artifact Registry
working-directory: ./client
run: |
gcloud auth configure-docker $GCLOUD_REGION-docker.pkg.dev
# 5. Construir la imagen Docker
- name: Build Docker image
working-directory: ./client
run: |
docker build \
--build-arg NEXT_PUBLIC_API_URL=${{ secrets.NEXT_PUBLIC_API_URL }} \
-t $GCLOUD_REGION-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhubnext-client/klowhub-client:${{ github.sha }} .
# 6.5. Limpiar imágenes antiguas
- name: Cleanup Old Images
working-directory: ./client
run: |
TAGS=$(gcloud container images list-tags \
${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhubnext-client/klowhub-client \
--format='get(tags)' \
--sort-by=~TIMESTAMP)
TOTAL_IMAGES=$(echo "$TAGS" | wc -l)
if [ $TOTAL_IMAGES -gt 1 ]; then
TO_DELETE=$((TOTAL_IMAGES - 1))
echo "Found $TOTAL_IMAGES images, keeping 1, deleting $TO_DELETE"
echo "$TAGS" | tail -n $TO_DELETE | while read TAG; do
if [ ! -z "$TAG" ]; then
echo "Deleting image with tag: $TAG"
gcloud container images delete \
"${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhubnext-client/klowhub-client:$TAG" \
--quiet
fi
done
fi
# 6. Subir la imagen a Artifact Registry
- name: Push Docker image
working-directory: ./client
run: |
docker push $GCLOUD_REGION-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhubnext-client/klowhub-client:${{ github.sha }}
# 15. Limpiar los nombres de revisiones
- name: Clean up old Cloud Run revisions
run: |
echo "Cleaning up revisions for service: klowhub"
REVISIONS=$(gcloud run revisions list \
--service=klowhub \
--region=${{ secrets.GCLOUD_REGION }} \
--sort-by=~creation_timestamp \
--format="value(name)")
REVISION_COUNT=$(echo "$REVISIONS" | wc -l)
if [ $REVISION_COUNT -gt 1 ]; then
REVISIONS_TO_DELETE=$(echo "$REVISIONS" | tail -n +2)
for REVISION in $REVISIONS_TO_DELETE
do
echo "Deleting revision: $REVISION"
gcloud run revisions delete $REVISION \
--region=${{ secrets.GCLOUD_REGION }} \
--quiet || echo "Could not delete revision $REVISION"
done
else
echo "No old revisions to delete for $SERVICE"
fi
# 7. Desplegar a Cloud Run
- name: Deploy to Cloud Run
working-directory: ./client
run: |
gcloud run deploy klowhub \
--region=$GCLOUD_REGION \
--platform=managed \
--allow-unauthenticated \
--image=$GCLOUD_REGION-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhubnext-client/klowhub-client:${{ github.sha }} \
--port='4002' \
--max-instances=2