|
| 1 | +name: 'Terraform CI/CD' |
| 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 }} # Make sure to set this secret in your repository |
| 15 | + |
| 16 | + steps: |
| 17 | + - name: 'Checkout' |
| 18 | + uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - name: 'Authenticate to Google Cloud' |
| 21 | + uses: 'google-github-actions/auth@v2' |
| 22 | + with: |
| 23 | + credentials_json: '${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}' # Make sure to set this secret in your repository |
| 24 | + |
| 25 | + - name: 'Set up Terraform' |
| 26 | + uses: hashicorp/setup-terraform@v3 |
| 27 | + with: |
| 28 | + terraform_version: latest # Or specify a version e.g., 1.0.0 |
| 29 | + |
| 30 | + - name: 'Terraform Init' |
| 31 | + id: init |
| 32 | + run: terraform init |
| 33 | + working-directory: ./terraform # Assuming your Terraform files are in a 'terraform' subdirectory |
| 34 | + |
| 35 | + - name: 'Terraform Validate' |
| 36 | + id: validate |
| 37 | + run: terraform validate -no-color |
| 38 | + working-directory: ./terraform |
| 39 | + |
| 40 | + - name: 'Terraform Plan' |
| 41 | + id: plan |
| 42 | + run: terraform plan -no-color -input=false -out=tfplan |
| 43 | + working-directory: ./terraform |
| 44 | + # Only run on pull requests or direct pushes to main (not on merges) |
| 45 | + if: github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/main') |
| 46 | + |
| 47 | + - name: 'Terraform Apply' |
| 48 | + id: apply |
| 49 | + run: terraform apply -auto-approve -input=false tfplan |
| 50 | + working-directory: ./terraform |
| 51 | + # Only run on pushes to the main branch (e.g., after a PR is merged) |
| 52 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 53 | + |
| 54 | + - name: 'Check BigQuery Dataset and Load Titanic Data' |
| 55 | + id: bigquery_check |
| 56 | + run: | |
| 57 | + chmod +x ../scripts/check_and_load_titanic_data.sh |
| 58 | + ../scripts/check_and_load_titanic_data.sh ${{ secrets.GCP_PROJECT_ID }} |
| 59 | + working-directory: ./terraform |
| 60 | + # Only run on pushes to the main branch (after terraform apply) |
| 61 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
0 commit comments