build: attempt e2-micro deployment on 2 nodes #58
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: CI/CD Pipeline | |
on: | |
push: | |
branches: [main] | |
env: | |
PROJECT_ID: poke-like-api | |
GKE_CLUSTER: pokelike-cluster | |
GKE_ZONE: europe-west2-a | |
IMAGE: pokelike-artifacts | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Setup .NET | |
uses: actions/setup-dotnet@v4 | |
with: | |
dotnet-version: "8.0.x" | |
- name: Google Auth | |
uses: "google-github-actions/auth@v2" | |
with: | |
credentials_json: "${{ secrets.GCP_SA_KEY }}" | |
- name: Setup GCloud CLI | |
uses: google-github-actions/setup-gcloud@v2 | |
with: | |
project_id: ${{ env.PROJECT_ID }} | |
- name: Create Artifact Registry Repository | |
run: | | |
if ! gcloud artifacts repositories describe pokelike-artifacts --location=europe-west2; then | |
gcloud artifacts repositories create pokelike-artifacts \ | |
--repository-format=docker \ | |
--location=europe-west2 \ | |
--description="Docker repository for PokeLikeAPI" | |
fi | |
- name: Configure Docker | |
run: gcloud auth configure-docker europe-west2-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 | |
- name: Create or Get GKE Cluster | |
run: | | |
if ! gcloud container clusters describe $GKE_CLUSTER --zone $GKE_ZONE; then | |
gcloud container clusters create $GKE_CLUSTER \ | |
--machine-type=e2-micro \ | |
--num-nodes=2 \ | |
--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 }} | |
- 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 | |
- 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 | |
- name: Deploy API and Services | |
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 |