-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitbucket-pipelines.yml
133 lines (111 loc) · 5.15 KB
/
bitbucket-pipelines.yml
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
image: python:3.8
definitions:
steps:
- step: &validate-yaml
name: Validate YAML files
script:
# Install yq
- apt-get update && apt-get install -y wget && apt-get install -y git
- wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/local/bin/yq
- chmod +x /usr/local/bin/yq
# Get changed files
- |
# Function to validate yaml name matches filename
validate_yaml_name() {
local file=$1
local filename=$(basename "$file" .yaml)
# Check if file exists (not deleted)
if [ -f "$file" ]; then
# First check if the file is valid YAML
if ! yq eval '.' "$file" > /dev/null 2>&1; then
echo "Error: Invalid YAML syntax in file $file"
exit 1
fi
local yaml_name=$(yq eval '.name' "$file")
if [[ "$yaml_name" != "$filename" ]]; then
echo "Error: YAML name field '$yaml_name' does not match filename '$filename'"
echo "File: $file"
exit 1
fi
fi
}
validate_folder_changes() {
local folder_name=$1
changed_files=$(git diff --name-status $BITBUCKET_PR_DESTINATION_COMMIT $BITBUCKET_COMMIT | grep "^[AMD].*\.yaml$" | grep "^[AMD].*${folder_name}/" | awk '{print $2}')
# Validate each changed file
for file in $changed_files; do
echo "Validating $file"
validate_yaml_name "$file"
done
}
# Call function for all folders
validate_folder_changes "teams"
validate_folder_changes "integrations"
validate_folder_changes "clusters"
validate_folder_changes "virtualaccounts"
pipelines:
pull-requests:
'**':
- step: *validate-yaml
- step:
name: Dry Run TFY Changes
script:
- apt-get update && apt-get install -y git
- pip install -U "truefoundry"
- |
# Function to process dry-run changes in a folder
process_folder_dryrun() {
local folder_name=$1
# Get changed files
changed_files=$(git diff --name-status $BITBUCKET_PR_DESTINATION_COMMIT $BITBUCKET_COMMIT | grep "^[AM].*\.yaml$" | grep "^[AM].*${folder_name}/" | awk '{print $2}')
# Sort files by nesting level (number of slashes)
sorted_files=$(echo "$changed_files" | awk -F'/' '{print NF, $0}' | sort -n | cut -d' ' -f2-)
# Process files
for file in $sorted_files; do
if [ -f "$file" ]; then
echo "Running dry-run apply for file: $file"
tfy apply -f "$file" --dry-run
fi
done
}
process_folder_dryrun "teams"
process_folder_dryrun "integrations"
process_folder_dryrun "clusters"
process_folder_dryrun "virtualaccounts"
branches:
main:
- step: *validate-yaml
- step:
name: Apply TFY Changes
script:
- apt-get update && apt-get install -y git
- pip install -U "truefoundry"
- |
# Function to process changes in a folder
process_folder_changes() {
local folder_name=$1
# Get all changed files
added_modified=$(git diff --name-status HEAD^ HEAD | grep "^[AM].*\.yaml$" | grep "^[AM].*${folder_name}/" | awk '{print $2}')
deleted=$(git diff --name-status HEAD^ HEAD | grep "^D.*\.yaml$" | grep "^D.*${folder_name}/" | awk '{print $2}')
# Sort files by nesting level (number of slashes) in decreasing order
sorted_deleted_files=$(echo "$deleted" | awk -F'/' '{print NF, $0}' | sort -rn | cut -d' ' -f2-)
# Process deleted files first
for file in $sorted_deleted_files; do
echo "Deleting configuration for $file"
# Uncomment the following line when ready to delete
# tfy delete -f "$file"
done
# Sort files by nesting level (number of slashes)
sorted_added_modified_files=$(echo "$added_modified" | awk -F'/' '{print NF, $0}' | sort -n | cut -d' ' -f2-)
# Process added/modified files
for file in $sorted_added_modified_files; do
if [ -f "$file" ]; then
echo "Applying configuration for $file"
tfy apply -f "$file"
fi
done
}
process_folder_changes "teams"
process_folder_changes "integrations"
process_folder_changes "clusters"
process_folder_changes "virtualaccounts"