DeployClient #303
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/[email protected] | |
with: | |
node-version: ${{ matrix.node-version }} | |
- name: Setup/ Install pnpm 9.7.0 manually | |
working-directory: ./client | |
run: npm install -g [email protected] | |
# 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 }} \ | |
--build-arg NEXT_PUBLIC_CLIENT_ID=${{ secrets.NEXT_PUBLIC_CLIENT_ID }} \ | |
--build-arg CLIENT_SECRET=${{ secrets.CLIENT_SECRET }} \ | |
-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 }} | |
# 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 |