forked from GoogleCloudPlatform/microservices-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreusable_detect-terraform-changes.yaml
More file actions
74 lines (65 loc) · 2.55 KB
/
Copy pathreusable_detect-terraform-changes.yaml
File metadata and controls
74 lines (65 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
name: 'Detect Terraform Changes'
on:
workflow_call:
outputs:
environments:
description: "JSON array of changed environments"
value: ${{ jobs.detect-changes.outputs.environments }}
has_changes:
description: "Whether any changes were detected"
value: ${{ jobs.detect-changes.outputs.has_changes }}
jobs:
detect-changes:
name: 'Detect Changed Environments'
runs-on: ubuntu-latest
outputs:
environments: ${{ steps.detect.outputs.environments }}
has_changes: ${{ steps.detect.outputs.has_changes }}
steps:
- name: 'Checkout Repository'
uses: actions/checkout@v5.0.0
with:
fetch-depth: 0
- name: 'Detect Changed Environments'
id: detect
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
changed_files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }})
else
if [ "${{ github.event.before }}" == "0000000000000000000000000000000000000000" ]; then
changed_files=$(git diff --name-only origin/main...${{ github.sha }})
else
# Normalny push - porównaj z poprzednim commitem
changed_files=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }})
fi
fi
echo "Changed files:"
echo "$changed_files"
environments=()
for file in $changed_files; do
if [[ $file == terraform/environments/* ]]; then
env_name=$(echo $file | cut -d'/' -f3)
if [[ ! " ${environments[@]} " =~ " ${env_name} " ]]; then
environments+=("$env_name")
fi
fi
done
if echo "$changed_files" | grep -q "terraform/modules/"; then
echo "Modules changed, checking all environments..."
for env_dir in terraform/environments/*/; do
env_name=$(basename "$env_dir")
if [[ ! " ${environments[@]} " =~ " ${env_name} " ]]; then
environments+=("$env_name")
fi
done
fi
if [ ${#environments[@]} -eq 0 ]; then
echo "environments=[]" >> $GITHUB_OUTPUT
echo "has_changes=false" >> $GITHUB_OUTPUT
echo "No environments detected"
else
json_array=$(printf '%s\n' "${environments[@]}" | jq -R . | jq -s -c .)
echo "environments=$json_array" >> $GITHUB_OUTPUT
echo "has_changes=true" >> $GITHUB_OUTPUT
echo "Detected environments: ${environments[*]}"
fi