Skip to content

Commit aead5fb

Browse files
committed
Add Terraform CI/CD workflow with IAM as Code integration
1 parent e143d90 commit aead5fb

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

.github/workflows/terraform.yml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: 'Terraform CI/CD with IAM as Code'
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
terraform:
11+
name: 'Terraform'
12+
runs-on: ubuntu-latest
13+
env:
14+
TF_VAR_project_id: ${{ secrets.GCP_PROJECT_ID }}
15+
TF_VAR_region: ${{ secrets.GCP_REGION }}
16+
TF_VAR_environment: ${{ secrets.GCP_ENVIRONMENT }}
17+
18+
steps:
19+
- name: 'Checkout'
20+
uses: actions/checkout@v4
21+
22+
- name: 'Authenticate to Google Cloud'
23+
uses: 'google-github-actions/auth@v2'
24+
with:
25+
credentials_json: '${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}'
26+
27+
- name: 'Set up Google Cloud SDK'
28+
uses: 'google-github-actions/setup-gcloud@v2'
29+
30+
- name: 'Set up Terraform'
31+
uses: hashicorp/setup-terraform@v3
32+
with:
33+
terraform_version: latest
34+
35+
- name: 'Terraform Init'
36+
id: init
37+
run: |
38+
terraform init \
39+
-backend-config="bucket=${{ secrets.GCP_PROJECT_ID }}-terraform-state"
40+
working-directory: ./terraform
41+
42+
- name: 'Terraform Validate'
43+
id: validate
44+
run: terraform validate -no-color
45+
working-directory: ./terraform
46+
47+
- name: 'Terraform Plan'
48+
id: plan
49+
run: terraform plan -no-color -input=false -out=tfplan
50+
working-directory: ./terraform
51+
if: github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/main')
52+
53+
- name: 'Terraform Apply'
54+
id: apply
55+
run: terraform apply -auto-approve -input=false tfplan
56+
working-directory: ./terraform
57+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
58+
59+
- name: 'Output Service Account Information'
60+
id: output_sa
61+
run: |
62+
echo "GitHub Actions Service Account: $(terraform output -raw github_actions_service_account_email)"
63+
echo "Cloud Function Service Account: $(terraform output -raw cloud_function_service_account_email)"
64+
working-directory: ./terraform
65+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
66+
67+
- name: 'Check BigQuery Dataset and Load Titanic Data'
68+
id: bigquery_check
69+
run: |
70+
chmod +x ../scripts/check_and_load_titanic_data.sh
71+
../scripts/check_and_load_titanic_data.sh ${{ secrets.GCP_PROJECT_ID }}
72+
working-directory: ./terraform
73+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'

0 commit comments

Comments
 (0)