-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_job.sh
executable file
·60 lines (53 loc) · 1.2 KB
/
generate_job.sh
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
#!/bin/bash
generate_status=${2}
echo "stages:
- plan
- apply
default:
image:
name: alpine/terragrunt
tags:
- terraform-runner
before_script:
- export GOOGLE_APPLICATION_CREDENTIALS=${ACCOUNT_KEY}
" >job.yml
generate_plan_job() {
PROJECT_URL="$1"
PARENT_PIPELINE_ID="$2"
echo "plan:${PROJECT_URL}:
stage: plan
script:
- cd projects/${PROJECT_URL}
- terragrunt validate
- terragrunt refresh
- terragrunt plan -out=tfplan
artifacts:
paths:
- projects/${PROJECT_URL}
expire_in: \"3600\"
rules:
- if: '\$CI_PIPELINE_SOURCE == \"parent_pipeline\"'
" >>job.yml
}
generate_apply_job() {
PROJECT_URL="$1"
PARENT_PIPELINE_ID="$2"
echo "apply:${PROJECT_URL}:
stage: apply
script:
- cd projects/${PROJECT_URL}
- echo 'y' | terragrunt apply tfplan
dependencies:
- plan:${PROJECT_URL}
when: manual
" >>job.yml
}
IFS=',' read -ra PROJECT_URLS <<<"$1"
for PROJECT_URL in "${PROJECT_URLS[@]}"; do
if [ "${generate_status}" == "plan" ]; then
generate_plan_job "$PROJECT_URL" "$PARENT_PIPELINE_ID"
else
generate_plan_job "$PROJECT_URL" "$PARENT_PIPELINE_ID"
generate_apply_job "$PROJECT_URL" "$PARENT_PIPELINE_ID"
fi
done