From 77275cf1280e842e35dae5dca70d50bd3ba0d029 Mon Sep 17 00:00:00 2001 From: maxitect Date: Wed, 27 Nov 2024 23:38:16 +0000 Subject: [PATCH] build: deploy containers without kubernetes --- .github/workflows/deploy.yml | 70 ++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 99b1080..acb2ebf 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -6,9 +6,8 @@ on: env: PROJECT_ID: poke-like-api - GKE_CLUSTER: pokelike-cluster - GKE_ZONE: europe-west2-a IMAGE: pokelike-artifacts + REGION: europe-west2 jobs: build-and-deploy: @@ -33,56 +32,47 @@ jobs: - name: Create Artifact Registry Repository run: | - if ! gcloud artifacts repositories describe pokelike-artifacts --location=europe-west2; then + if ! gcloud artifacts repositories describe pokelike-artifacts --location=$REGION; then gcloud artifacts repositories create pokelike-artifacts \ --repository-format=docker \ - --location=europe-west2 \ + --location=$REGION \ --description="Docker repository for PokeLikeAPI" fi - name: Configure Docker - run: gcloud auth configure-docker europe-west2-docker.pkg.dev + run: gcloud auth configure-docker $REGION-docker.pkg.dev - name: Build and Push Image run: | - docker build -t europe-west2-docker.pkg.dev/$PROJECT_ID/$IMAGE/$IMAGE:$GITHUB_SHA . - docker push europe-west2-docker.pkg.dev/$PROJECT_ID/$IMAGE/$IMAGE:$GITHUB_SHA + docker build -t $REGION-docker.pkg.dev/$PROJECT_ID/$IMAGE/$IMAGE:$GITHUB_SHA . + docker push $REGION-docker.pkg.dev/$PROJECT_ID/$IMAGE/$IMAGE:$GITHUB_SHA - - name: Create or Get GKE Cluster + - name: Create Cloud Run Service run: | - if ! gcloud container clusters describe $GKE_CLUSTER --zone $GKE_ZONE; then - gcloud container clusters create $GKE_CLUSTER \ - --machine-type=e2-small \ - --num-nodes=1 \ - --zone=$GKE_ZONE - fi - - - name: Get GKE Credentials - uses: google-github-actions/get-gke-credentials@v2 - with: - cluster_name: ${{ env.GKE_CLUSTER }} - location: ${{ env.GKE_ZONE }} + # Create network + gcloud compute networks create pokelike-network --subnet-mode=auto || true - - name: Delete Existing Resources - run: | - kubectl delete -f kubernetes/api-deployment.yaml --ignore-not-found - kubectl delete -f kubernetes/api-service.yaml --ignore-not-found - kubectl delete -f kubernetes/postgres-deployment.yaml --ignore-not-found - kubectl delete -f kubernetes/postgres-service.yaml --ignore-not-found - kubectl delete -f kubernetes/postgres-pvc.yaml --ignore-not-found - - kubectl wait --for=delete deployment/pokelike-api --timeout=60s || true - kubectl wait --for=delete deployment/postgres --timeout=60s || true + # Deploy Postgres container + gcloud compute instances create-with-container postgres \ + --container-image=postgres:latest \ + --container-env="POSTGRES_DB=PokeLikeDb,POSTGRES_USER=postgres,POSTGRES_PASSWORD=welovepokemon" \ + --container-mount-host-path=mount-path=/var/lib/postgresql/data,host-path=/postgres-data \ + --machine-type=e2-micro \ + --network=pokelike-network \ + --zone=$REGION-a || true - - name: Deploy Infrastructure - run: | - kubectl apply -f kubernetes/postgres-pvc.yaml - kubectl apply -f kubernetes/postgres-service.yaml - kubectl apply -f kubernetes/postgres-deployment.yaml - kubectl wait --for=condition=available deployment/postgres --timeout=200s + # Deploy API container + gcloud run deploy pokelike-api \ + --image=$REGION-docker.pkg.dev/$PROJECT_ID/$IMAGE/$IMAGE:$GITHUB_SHA \ + --platform=managed \ + --region=$REGION \ + --set-env-vars="ConnectionStrings__PokeLikeDbContext=Host=postgres;Port=5432;Database=PokeLikeDb;Username=postgres;Password=welovepokemon" \ + --allow-unauthenticated \ + --vpc-connector=pokelike-connector - - name: Deploy API and Services + - name: Create VPC Connector run: | - sed "s/:IMAGE_TAG/:$GITHUB_SHA/" kubernetes/api-deployment.yaml | kubectl apply -f - - kubectl apply -f kubernetes/api-service.yaml - kubectl wait --for=condition=available deployment/pokelike-api --timeout=180s + gcloud compute networks vpc-access connectors create pokelike-connector \ + --network=pokelike-network \ + --region=$REGION \ + --range=10.8.0.0/28 || true