Skip to content

Commit adecece

Browse files
refactor: Update GitHub Actions triggers for multi-env deployment
This commit refactors the GitHub Actions workflow to align with the specified deployment strategy: - DEV environment: Triggered manually. Includes an optional input to override the JAR file path. - QA environment: Triggered automatically on push/merge to the `main` branch. - PROD environment: Triggered automatically upon publishing a new GitHub Release. The workflow checks out the code from the release tag for deployment. The workflow has been restructured into separate jobs for DEV, QA, and PROD deployments, each with its own specific trigger conditions and environment variable configurations (GCP Project ID, environment name, Terraform state bucket name). Global environment variables for function name, region, entry point, default JAR path, runtime, memory, and timeout are defined and can be overridden at the job level if necessary. Updated user instructions have been provided to explain these new trigger mechanisms and how to use them.
1 parent b24f017 commit adecece

File tree

1 file changed

+183
-88
lines changed

1 file changed

+183
-88
lines changed

.github/workflows/deploy.yml

Lines changed: 183 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,45 @@
11
name: Deploy GBFS Validator Cloud Function
22

33
on:
4+
workflow_dispatch: # Trigger for DEV (on-demand)
5+
inputs:
6+
jar_file_path_override:
7+
description: 'Optional: Override JAR file path for DEV (e.g., target/my-dev-specific.jar)'
8+
required: false
9+
default: 'path/to/your/validator.jar' # Default, same as TF_VAR_jar_file_path
10+
# Add other inputs if needed for dev, like specific branch/commit to build from
411
push:
512
branches:
6-
- main # Or your specific branches for dev, qa, prod
7-
# Example:
8-
# - dev
9-
# - qa
10-
# - production
13+
- main # Trigger for QA
14+
release:
15+
types: [published] # Trigger for PROD
16+
17+
env: # Global env vars, can be overridden at job level
18+
# These should be configured based on your function's needs or overridden per environment job
19+
TF_VAR_function_name: "gbfs-validator-function"
20+
TF_VAR_gcp_region: "us-central1" # Change if needed
21+
TF_VAR_function_entry_point: "com.example.YourFunctionEntryPoint" # ** IMPORTANT: User needs to change this **
22+
TF_VAR_jar_file_path: "path/to/your/validator.jar" # ** IMPORTANT: User needs to change this **
23+
TF_VAR_function_runtime: "java11" # Or java17, java21
24+
TF_VAR_function_memory_mb: 256
25+
TF_VAR_function_timeout_s: 60
26+
TERRAFORM_VERSION: "1.2.0" # Specify Terraform version
1127

1228
jobs:
13-
deploy:
29+
###########################################
30+
# DEV DEPLOYMENT #
31+
###########################################
32+
deploy-dev:
33+
if: github.event_name == 'workflow_dispatch'
34+
name: Deploy to DEV
1435
runs-on: ubuntu-latest
36+
environment: dev # Optional: Link to GitHub environment for protection rules/secrets
1537
env:
16-
# Will be dynamically set based on the branch
17-
TF_VAR_gcp_project_id: ""
18-
TF_VAR_environment: ""
19-
TF_VAR_source_bucket_name: "" # e.g., gbfs-validator-src-dev
20-
# These should be configured based on your function's needs
21-
TF_VAR_function_name: "gbfs-validator-function" # Can be customized per env if needed
22-
TF_VAR_gcp_region: "us-central1" # Change if needed
23-
TF_VAR_function_entry_point: "com.example.YourFunctionEntryPoint" # ** IMPORTANT: User needs to change this **
24-
TF_VAR_jar_file_path: "path/to/your/validator.jar" # ** IMPORTANT: User needs to change this **
25-
TF_VAR_function_runtime: "java11" # Or java17, java21
26-
TF_VAR_function_memory_mb: 256
27-
TF_VAR_function_timeout_s: 60
38+
TF_VAR_gcp_project_id: ${{ secrets.GCP_PROJECT_ID_DEV }}
39+
TF_VAR_environment: "dev"
40+
TF_VAR_source_bucket_name: "gbfs-validator-src-dev"
41+
# Override JAR path if provided in workflow_dispatch input
42+
TF_VAR_jar_file_path: ${{ github.event.inputs.jar_file_path_override || env.TF_VAR_jar_file_path }}
2843

2944
steps:
3045
- name: Checkout code
@@ -33,94 +48,174 @@ jobs:
3348
- name: Set up JDK
3449
uses: actions/setup-java@v3
3550
with:
36-
distribution: 'temurin' # Or any other distribution
37-
java-version: '11' # Or 17, 21, matching TF_VAR_function_runtime
51+
distribution: 'temurin'
52+
java-version: ${{ env.TF_VAR_function_runtime == 'java11' && '11' || (env.TF_VAR_function_runtime == 'java17' && '17' || '21') }}
3853

39-
# Add a step here to build the JAR if it's not pre-built and checked into the repo
40-
# - name: Build JAR (if needed)
54+
# - name: Build JAR for DEV (if needed)
4155
# run: |
42-
# # e.g., mvn package -DskipTests
43-
# echo "JAR build step - customize this if your JAR is not pre-built"
56+
# echo "JAR build step for DEV - customize if needed"
4457
# # Ensure TF_VAR_jar_file_path points to the built JAR
4558

4659
- name: Set up Google Cloud SDK
4760
uses: google-github-actions/setup-gcloud@v1
4861
with:
49-
project_id: ${{ env.TF_VAR_gcp_project_id }} # Will be set dynamically
62+
project_id: ${{ env.TF_VAR_gcp_project_id }}
5063

5164
- name: Authenticate to GCP
65+
id: auth_dev
5266
uses: google-github-actions/auth@v1
5367
with:
54-
credentials_json: ${{ secrets.GCP_SA_KEY }} # User needs to set this secret
55-
56-
- name: Set environment-specific variables
57-
run: |
58-
BRANCH_NAME=${GITHUB_REF#refs/heads/}
59-
if [[ "$BRANCH_NAME" == "main" ]]; then # Assuming 'main' is for 'prod'
60-
echo "Setting environment for PROD"
61-
echo "TF_VAR_gcp_project_id=${{ secrets.GCP_PROJECT_ID_PROD }}" >> $GITHUB_ENV
62-
echo "TF_VAR_environment=prod" >> $GITHUB_ENV
63-
echo "TF_VAR_source_bucket_name=gbfs-validator-src-prod" >> $GITHUB_ENV
64-
# Add other prod-specific TF_VARs if needed
65-
elif [[ "$BRANCH_NAME" == "qa" ]]; then
66-
echo "Setting environment for QA"
67-
echo "TF_VAR_gcp_project_id=${{ secrets.GCP_PROJECT_ID_QA }}" >> $GITHUB_ENV
68-
echo "TF_VAR_environment=qa" >> $GITHUB_ENV
69-
echo "TF_VAR_source_bucket_name=gbfs-validator-src-qa" >> $GITHUB_ENV
70-
# Add other qa-specific TF_VARs if needed
71-
elif [[ "$BRANCH_NAME" == "dev" ]]; then
72-
echo "Setting environment for DEV"
73-
echo "TF_VAR_gcp_project_id=${{ secrets.GCP_PROJECT_ID_DEV }}" >> $GITHUB_ENV
74-
echo "TF_VAR_environment=dev" >> $GITHUB_ENV
75-
echo "TF_VAR_source_bucket_name=gbfs-validator-src-dev" >> $GITHUB_ENV
76-
# Add other dev-specific TF_VARs if needed
77-
else
78-
echo "Branch $BRANCH_NAME is not configured for deployment."
79-
exit 1
80-
fi
81-
echo "VERIFYING ENV VARS:"
82-
echo "Project ID: ${{ env.TF_VAR_gcp_project_id }}"
83-
echo "Environment: ${{ env.TF_VAR_environment }}"
84-
echo "Source Bucket: ${{ env.TF_VAR_source_bucket_name }}"
85-
echo "Entry Point: ${{ env.TF_VAR_function_entry_point }}"
86-
echo "JAR Path: ${{ env.TF_VAR_jar_file_path }}"
68+
credentials_json: ${{ secrets.GCP_SA_KEY }}
8769

70+
- name: Set up Terraform
71+
uses: hashicorp/setup-terraform@v2
72+
with:
73+
terraform_version: ${{ env.TERRAFORM_VERSION }}
74+
75+
- name: Terraform Init (DEV)
76+
run: terraform init -backend-config=bucket=${{ env.TF_VAR_environment }}-gbfs-tf-state -backend-config=prefix=gbfs-validator
77+
78+
- name: Terraform Validate (DEV)
79+
run: terraform validate
80+
81+
- name: Terraform Plan (DEV)
82+
run: terraform plan -input=false -no-color -out=tfplan_dev
83+
84+
- name: Terraform Apply (DEV)
85+
run: terraform apply -auto-approve -input=false tfplan_dev
86+
87+
- name: Show Function URL (DEV)
88+
run: echo "DEV Cloud Function URL: $(terraform output -raw function_url)"
89+
90+
###########################################
91+
# QA DEPLOYMENT #
92+
###########################################
93+
deploy-qa:
94+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
95+
name: Deploy to QA
96+
runs-on: ubuntu-latest
97+
needs: [deploy-dev] # Optional: make QA depend on a successful DEV manual run if desired, though typically QA is from main
98+
environment: qa
99+
env:
100+
TF_VAR_gcp_project_id: ${{ secrets.GCP_PROJECT_ID_QA }}
101+
TF_VAR_environment: "qa"
102+
TF_VAR_source_bucket_name: "gbfs-validator-src-qa"
103+
# TF_VAR_jar_file_path: "path/to/qa/validator.jar" # Override if QA uses a different JAR path
104+
105+
steps:
106+
- name: Checkout code
107+
uses: actions/checkout@v3
108+
109+
- name: Set up JDK
110+
uses: actions/setup-java@v3
111+
with:
112+
distribution: 'temurin'
113+
java-version: ${{ env.TF_VAR_function_runtime == 'java11' && '11' || (env.TF_VAR_function_runtime == 'java17' && '17' || '21') }}
114+
115+
# - name: Build JAR for QA (if needed)
116+
# run: |
117+
# echo "JAR build step for QA - customize if needed"
118+
# # Ensure TF_VAR_jar_file_path points to the built JAR
119+
120+
- name: Set up Google Cloud SDK
121+
uses: google-github-actions/setup-gcloud@v1
122+
with:
123+
project_id: ${{ env.TF_VAR_gcp_project_id }}
124+
125+
- name: Authenticate to GCP
126+
id: auth_qa
127+
uses: google-github-actions/auth@v1
128+
with:
129+
credentials_json: ${{ secrets.GCP_SA_KEY }}
88130

89131
- name: Set up Terraform
90132
uses: hashicorp/setup-terraform@v2
91133
with:
92-
terraform_version: 1.2.0 # Or your desired version
134+
terraform_version: ${{ env.TERRAFORM_VERSION }}
93135

94-
- name: Terraform Init
95-
run: terraform init
96-
env:
97-
# Pass GCS backend config if you decide to use it
98-
# GOOGLE_APPLICATION_CREDENTIALS: ${{ steps.auth.outputs.credentials_path }} # Not needed if using default auth
99-
TF_CLI_ARGS_init: "-backend-config=bucket=${{ env.TF_VAR_environment }}-gbfs-tf-state -backend-config=prefix=gbfs-validator"
136+
- name: Terraform Init (QA)
137+
run: terraform init -backend-config=bucket=${{ env.TF_VAR_environment }}-gbfs-tf-state -backend-config=prefix=gbfs-validator
138+
139+
- name: Terraform Validate (QA)
140+
run: terraform validate
100141

142+
- name: Terraform Plan (QA)
143+
run: terraform plan -input=false -no-color -out=tfplan_qa
101144

102-
- name: Terraform Validate
145+
- name: Terraform Apply (QA)
146+
run: terraform apply -auto-approve -input=false tfplan_qa
147+
148+
- name: Show Function URL (QA)
149+
run: echo "QA Cloud Function URL: $(terraform output -raw function_url)"
150+
151+
###########################################
152+
# PROD DEPLOYMENT #
153+
###########################################
154+
deploy-prod:
155+
if: github.event_name == 'release' && github.event.action == 'published'
156+
name: Deploy to PROD
157+
runs-on: ubuntu-latest
158+
needs: [deploy-qa] # Optional: make PROD depend on a successful QA deployment
159+
environment: prod
160+
env:
161+
TF_VAR_gcp_project_id: ${{ secrets.GCP_PROJECT_ID_PROD }}
162+
TF_VAR_environment: "prod"
163+
TF_VAR_source_bucket_name: "gbfs-validator-src-prod"
164+
# For releases, you might want to use a JAR attached to the release or built from the release tag
165+
# TF_VAR_jar_file_path: "path/to/release/validator.jar" # Override for PROD
166+
167+
steps:
168+
- name: Checkout code
169+
uses: actions/checkout@v3
170+
with:
171+
ref: ${{ github.event.release.tag_name }} # Checkout the code from the release tag
172+
173+
# Add step here to download JAR from release assets if that's your strategy
174+
# - name: Download Release JAR
175+
# uses: actions/download-artifact@v3
176+
# with:
177+
# name: validator-jar # Assuming JAR was uploaded as an artifact with this name
178+
# path: path/to/download # Download to a specific path
179+
# Then update TF_VAR_jar_file_path accordingly for this job
180+
181+
- name: Set up JDK
182+
uses: actions/setup-java@v3
183+
with:
184+
distribution: 'temurin'
185+
java-version: ${{ env.TF_VAR_function_runtime == 'java11' && '11' || (env.TF_VAR_function_runtime == 'java17' && '17' || '21') }}
186+
187+
# - name: Build JAR for PROD (if needed, typically use release artifact)
188+
# run: |
189+
# echo "JAR build step for PROD - customize if needed"
190+
# # Ensure TF_VAR_jar_file_path points to the built JAR or release artifact
191+
192+
- name: Set up Google Cloud SDK
193+
uses: google-github-actions/setup-gcloud@v1
194+
with:
195+
project_id: ${{ env.TF_VAR_gcp_project_id }}
196+
197+
- name: Authenticate to GCP
198+
id: auth_prod
199+
uses: google-github-actions/auth@v1
200+
with:
201+
credentials_json: ${{ secrets.GCP_SA_KEY }}
202+
203+
- name: Set up Terraform
204+
uses: hashicorp/setup-terraform@v2
205+
with:
206+
terraform_version: ${{ env.TERRAFORM_VERSION }}
207+
208+
- name: Terraform Init (PROD)
209+
run: terraform init -backend-config=bucket=${{ env.TF_VAR_environment }}-gbfs-tf-state -backend-config=prefix=gbfs-validator
210+
211+
- name: Terraform Validate (PROD)
103212
run: terraform validate
104213

105-
- name: Terraform Plan
106-
id: plan
107-
run: terraform plan -input=false -no-color -out=tfplan
108-
continue-on-error: true # To allow viewing the plan even if there are errors for PRs
109-
110-
- name: Terraform Plan Status
111-
if: steps.plan.outcome == 'failure'
112-
run: |
113-
echo "Terraform Plan failed!"
114-
exit 1
115-
116-
# On pull requests, you might only want to run init, validate, and plan.
117-
# The apply step should only run on merges to specific branches.
118-
- name: Terraform Apply
119-
if: github.event_name == 'push' # Only apply on direct pushes to configured branches
120-
run: terraform apply -auto-approve -input=false tfplan
121-
122-
# Optional: Add a step to output the function URL
123-
- name: Show Function URL
124-
if: github.event_name == 'push'
125-
run: |
126-
echo "Cloud Function URL: $(terraform output -raw function_url)"
214+
- name: Terraform Plan (PROD)
215+
run: terraform plan -input=false -no-color -out=tfplan_prod
216+
217+
- name: Terraform Apply (PROD)
218+
run: terraform apply -auto-approve -input=false tfplan_prod
219+
220+
- name: Show Function URL (PROD)
221+
run: echo "PROD Cloud Function URL: $(terraform output -raw function_url)"

0 commit comments

Comments
 (0)