To set up the Terraform module to deploy your static website to GCS using GitHub Actions, follow these steps:
Step 1: Prepare Your Environment
-
Install Terraform: Make sure you have Terraform installed. You can install it using Terraform's official guide.
-
Create a GCP Service Account:
- Go to the Google Cloud Console.
- Create a service account with "Storage Admin" permissions.
- Download the service account key as a JSON file. You'll need this file for Terraform.
-
Add GCP Service Account Key to GitHub Secrets:
- In your GitHub repository, go to "Settings" > "Secrets and variables" > "Actions".
- Add a new secret with the name
GCP_SA_KEY
and paste the contents of your GCP service account key JSON file.
Step 2: Create the Terraform Configuration Files
-
Create a Directory for Terraform: Inside your project, create a directory for the Terraform configuration (e.g.,
terraform/
). -
Add the Provided Terraform Module: Save the content of the Terraform module you created into a file named
main.tf
inside theterraform/
directory. -
Create a Variables File:
-
Create a file named
variables.tfvars
and populate it with your specific values:credentials_file = "path/to/your/gcp-key.json" # Replace with the local path to your GCP key JSON project_id = "your-gcp-project-id" # Replace with your GCP project ID bucket_name = "your-gcs-bucket-name" # Replace with your bucket name (must be globally unique) public_directory = "path/to/your/public" # Replace with the path to the directory containing your static website files
-
Step 3: Setup GitHub Action to Deploy Using Terraform
Add the following GitHub Action workflow file (.github/workflows/deploy.yml
) to automate deployment:
name: Deploy Static Site to GCS
on:
push:
branches:
- main
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_wrapper: false
- name: Authenticate to GCP
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}
- name: Initialize Terraform
working-directory: terraform
run: terraform init
- name: Validate Terraform
working-directory: terraform
run: terraform validate
- name: Plan Terraform
working-directory: terraform
run: terraform plan -var-file="variables.tfvars"
- name: Apply Terraform
working-directory: terraform
run: terraform apply -auto-approve -var-file="variables.tfvars"
Step 4: Run the Workflow
-
Commit and Push Your Changes: Add the Terraform configuration, GitHub Action workflow, and any other files to Git, and push them to your
main
branch. -
Trigger the Workflow: The GitHub Action workflow will automatically trigger when you push changes to the
main
branch. You can also manually run the workflow from the GitHub Actions tab.
Step 5: Verify Deployment
- Access Your Site:
- Your static site should now be available through your GCS bucket's website URL.
- Make sure your bucket's URL is configured properly for public access (
https://<YOUR_BUCKET_NAME>.storage.googleapis.com
or a custom domain).
Summary
- Service Account Key: Set up and secure credentials.
- Terraform Configuration: Use the provided module for setting up GCS resources.
- GitHub Action: Automate the deployment process with Terraform.
Let me know if you need help with any of these steps or additional guidance for customizing the workflow!