build: deploy containers without kubernetes #61
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 | |
IMAGE: pokelike-artifacts | |
REGION: europe-west2 | |
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=$REGION; then | |
gcloud artifacts repositories create pokelike-artifacts \ | |
--repository-format=docker \ | |
--location=$REGION \ | |
--description="Docker repository for PokeLikeAPI" | |
fi | |
- name: Configure Docker | |
run: gcloud auth configure-docker $REGION-docker.pkg.dev | |
- name: Build and Push Image | |
run: | | |
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 Cloud Run Service | |
run: | | |
# Create network | |
gcloud compute networks create pokelike-network --subnet-mode=auto || 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 | |
# 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: Create VPC Connector | |
run: | | |
gcloud compute networks vpc-access connectors create pokelike-connector \ | |
--network=pokelike-network \ | |
--region=$REGION \ | |
--range=10.8.0.0/28 || true |