forked from codefresh-contrib/terraform-sample-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codefresh.yml
78 lines (78 loc) · 2.96 KB
/
codefresh.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
version: '1.0'
stages:
- checkout
- prepare
- plan
- deploy
- destroy
steps:
main_clone:
title: Cloning main repository...
stage: checkout
type: git-clone
repo: 'sandrogattuso/terraform-sample-app'
revision: master
git: github
Setup:
image: 'google/cloud-sdk'
title: Setting up Google cloud auth and Variables
stage: prepare
shell: bash
commands:
- STACK_ARRAY=($STACKS)
- echo "$ACCOUNT_JSON_CONTENT" | base64 -d > /codefresh/volume/account.json
- cf_export GOOGLE_CLOUD_KEYFILE_JSON=/codefresh/volume/account.json
- cf_export GOOGLE_APPLICATION_CREDENTIALS=/codefresh/volume/account.json
- echo gcp_project = \"$GCLOUD_PROJECT\" > vars.auto.tfvars
- gcloud auth activate-service-account --key-file=/codefresh/volume/account.json
- gsutil cp "gs://$STORAGE_BUCKET/codefresh/$STACK_FILE" $STACK_FILE || echo "0" > $STACK_FILE
- export CURRENT_STACK=$(cat $STACK_FILE)
- export OLD_STACK="$(( ($CURRENT_STACK+1) %2 ))"
- export CURRENT_STACK_NAME="${STACK_ARRAY[$CURRENT_STACK]}"
- cf_export CURRENT_STACK OLD_STACK CURRENT_STACK_NAME
TerraformPlan:
image: hashicorp/terraform:0.12.0
title: Terraform plan
stage: plan
commands:
- echo 'yes' | terraform init -force-copy -backend-config="bucket=$STORAGE_BUCKET"
- terraform workspace select $CURRENT_STACK_NAME || terraform workspace new $CURRENT_STACK_NAME
- export BLUE_GREEN=false
- terraform plan -detailed-exitcode || if [[ $? == 2 ]]; then export BLUE_GREEN=true; export OLD_STACK=$CURRENT_STACK; export CURRENT_STACK=$(( ($CURRENT_STACK+1) %2 )); else echo "Error excuting plan"; exit 1; fi
- cf_export BLUE_GREEN OLD_STACK CURRENT_STACK
SetCurrentStackName:
image: 'google/cloud-sdk'
title: Setting Stack Name
stage: plan
shell: bash
commands:
- export STACK_ARRAY=($STACKS)
- export OLD_STACK_NAME="${STACK_ARRAY[$OLD_STACK]}"
- export CURRENT_STACK_NAME="${STACK_ARRAY[$CURRENT_STACK]}"
- cf_export OLD_STACK_NAME CURRENT_STACK_NAME
TerraformApply:
image: hashicorp/terraform:0.12.0
title: Terraform apply
stage: deploy
commands:
- terraform workspace select $CURRENT_STACK_NAME || terraform workspace new $CURRENT_STACK_NAME
- terraform apply -auto-approve
UpdateLiveStack:
image: 'google/cloud-sdk'
title: Update Live stack information
stage: deploy
commands:
- echo "$CURRENT_STACK" > $STACK_FILE
- gcloud auth activate-service-account --key-file=/codefresh/volume/account.json
- gsutil cp $STACK_FILE "gs://$STORAGE_BUCKET/codefresh/$STACK_FILE"
TerraformDestroy:
image: hashicorp/terraform:0.12.0
title: Terraform destroy
stage: destroy
commands:
- terraform workspace select $OLD_STACK_NAME || terraform workspace new $OLD_STACK_NAME
- terraform destroy -auto-approve
when:
condition:
all:
executedBlueGreen: '${{BLUE_GREEN}}'