Skip to content

Commit

Permalink
(iac) retrive gcp project id on my cicd
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Alexandre35 committed Nov 2, 2024
1 parent db71474 commit e99372d
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 5 deletions.
31 changes: 26 additions & 5 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Deploy to GCS
on:
push:
branches:
- main # Trigger on push to main branch
- main

jobs:
build:
Expand All @@ -13,17 +13,38 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.5.3 # Specify the version you are using

- name: Initialize Terraform
working-directory: iac
run: terraform init

- name: Apply Terraform
working-directory: iac
id: terraform
run: terraform apply -auto-approve
env:
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GCP_SA_KEY }}

- name: Get project ID from Terraform output
working-directory: iac
id: get_gcp_project_id
run: echo "PROJECT_ID=$(terraform output -raw gcp_project_id)" >> $GITHUB_ENV

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: "18" # Ensure the correct Node.js version
node-version: "18"

- name: Install dependencies
working-directory: travian/frontend # Change to frontend directory
working-directory: travian/frontend
run: npm install

- name: Build project
working-directory: travian/frontend # Change to frontend directory
working-directory: travian/frontend
run: npm run build

- name: Authenticate to GCP
Expand All @@ -34,7 +55,7 @@ jobs:
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2
with:
project_id: "voltaic-sensor-438416-h9"
project_id: ${{ env.PROJECT_ID }}
service_account_key: ${{ secrets.GCP_SA_KEY }}

- name: Upload to GCS
Expand Down
12 changes: 12 additions & 0 deletions iac/apis.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ resource "google_project_service" "cloud_build_api" {
project = google_project.gcp_prod_project.project_id
service = "cloudbuild.googleapis.com"
}

# Enable Artifact Registry API
resource "google_project_service" "artifact_registry" {
project = google_project.gcp_prod_project.project_id
service = "artifactregistry.googleapis.com"
}

# Enable Cloud Run API
resource "google_project_service" "cloud_run" {
project = google_project.gcp_prod_project.project_id
service = "run.googleapis.com"
}
61 changes: 61 additions & 0 deletions iac/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,64 @@ resource "google_project_iam_member" "cloud_build_compute_role" {
member = "serviceAccount:${google_project.gcp_prod_project.number}[email protected]"
role = "roles/cloudbuild.builds.builder"
}

# Create Artifact Registry repository for Docker images
resource "google_artifact_registry_repository" "docker_repo" {
project = google_project.gcp_prod_project.project_id
location = var.region
repository_id = "python-backend-repo"
description = "Docker repository for Cloud Run"
format = "DOCKER"
}


# Assign Artifact Registry permissions to Cloud Build
resource "google_project_iam_member" "cloud_build_artifact_registry_pusher" {
project = google_project.gcp_prod_project.project_id
member = "serviceAccount:${google_project.gcp_prod_project.number}@cloudbuild.gserviceaccount.com"
role = "roles/artifactregistry.writer"
}

# Define Cloud Run service
resource "google_cloud_run_service" "python_backend" {
name = "python-backend"
project = google_project.gcp_prod_project.project_id
location = var.region

template {
spec {
containers {
image = "gcr.io/cloudrun/hello" # Public Cloud Run Hello World image
resources {
limits = {
memory = "512Mi"
cpu = "1"
}
}
}
}
}

autogenerate_revision_name = true

# Optional: Allow unauthenticated access
traffic {
percent = 100
latest_revision = true
}
}

# Allow public access to Cloud Run service
resource "google_cloud_run_service_iam_member" "invoker" {
project = google_project.gcp_prod_project.project_id
location = var.region
service = google_cloud_run_service.python_backend.name
role = "roles/run.invoker"
member = "allUsers"
}

# Output Cloud Run URL
output "cloud_run_url" {
value = google_cloud_run_service.python_backend.status[0].url
description = "URL of the deployed Python backend on Cloud Run."
}
4 changes: 4 additions & 0 deletions iac/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "gcp_project_id" {
value = google_project.gcp_prod_project.project_id
description = "The dynamically generated project ID"
}

0 comments on commit e99372d

Please sign in to comment.