-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbitbucket-pipelines.yml
128 lines (126 loc) · 5.52 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
image: google/cloud-sdk
pipelines:
default:
- step:
name: setup terraform
script:
- apt-get update && apt-get install unzip wget -y
- wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip || echo fine...
- unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip
- yes | cp terraform /usr/bin/
- ls /usr/bin
artifacts:
- terraform
- step:
name: terraform init
oidc: true
script:
- cp terraform /usr/bin/
- ls . /usr/bin/
- ./get_oidctoken.sh
- export GOOGLE_APPLICATION_CREDENTIALS=`pwd`/.gcp_temp_cred.json
- /usr/bin/terraform init -backend-config="bucket=${STATE_BUCKET}" -backend-config="prefix=${BITBUCKET_REPO_FULL_NAME}"
- /usr/bin/terraform plan
branches:
feature/*:
- step:
name: setup terraform
script:
- apt-get update && apt-get install unzip wget -y
- wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip || echo fine...
- unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip
- yes | cp terraform /usr/bin/
- ls /usr/bin
artifacts:
- terraform
- step:
name: main branch execution tf-apply
oidc: true
script:
- cp terraform /usr/bin/
- ls . /usr/bin/
- export SERVICE_ACCOUNT_EMAIL="${GCP_SERVICE_ACCOUNT}"
- ./get_oidctoken.sh
- export GOOGLE_APPLICATION_CREDENTIALS=`pwd`/.gcp_temp_cred.json
- /usr/bin/terraform init -backend-config="bucket=${STATE_BUCKET}" -backend-config="prefix=${BITBUCKET_REPO_FULL_NAME}"
- /usr/bin/terraform plan
- /usr/bin/terraform apply -auto-approve
dev:
- step:
name: setup terraform
script:
- apt-get update && apt-get install unzip wget -y
- wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip || echo fine...
- unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip
- yes | cp terraform /usr/bin/
- ls /usr/bin
artifacts:
- terraform
- step:
name: dev branch execution tf-apply
oidc: true
script:
- cp terraform /usr/bin/
- ls . /usr/bin/
- export SERVICE_ACCOUNT_EMAIL="${GCP_SERVICE_ACCOUNT}"
- ./get_oidctoken.sh
- export GOOGLE_APPLICATION_CREDENTIALS=`pwd`/.gcp_temp_cred.json
- /usr/bin/terraform init -backend-config="bucket=${STATE_BUCKET}" -backend-config="prefix=${BITBUCKET_REPO_FULL_NAME}"
- /usr/bin/terraform plan
- /usr/bin/terraform apply -auto-approve
main:
- step:
name: setup terraform
script:
- apt-get update && apt-get install unzip wget -y
- wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip || echo fine...
- unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip
- yes | cp terraform /usr/bin/
- ls /usr/bin
artifacts:
- terraform
- step:
name: tf-policy validate
oidc: true
script:
- if [[ "$TERRAFORM_POLICY_VALIDATE" == "true" ]]; then
- cp terraform /usr/bin/
- export TF_PLAN_NAME=plan.out
- export TF_ROOT=tf_root
- mkdir -p $TF_ROOT
- ls . /usr/bin/
- export SERVICE_ACCOUNT_EMAIL="${GCP_SERVICE_ACCOUNT}"
- ./get_oidctoken.sh
- export GOOGLE_APPLICATION_CREDENTIALS=`pwd`/.gcp_temp_cred.json
- apt-get install google-cloud-sdk-terraform-tools -y
- git clone $POLICY_LIBRARY_REPO $TF_ROOT/policy-repo
- /usr/bin/terraform init -backend-config="bucket=${STATE_BUCKET}" -backend-config="prefix=${BITBUCKET_REPO_FULL_NAME}"
- /usr/bin/terraform plan -out $TF_PLAN_NAME
- /usr/bin/terraform show --json $TF_PLAN_NAME > $TF_ROOT/tfplan.json
- violations=$(gcloud beta terraform vet $TF_ROOT/tfplan.json --policy-library=$TF_ROOT/policy-repo --format=json)
- ret_val=$?
- if [ $ret_val -eq 2 ]; then
- if [ "$violations" != "[]" ] ; then
- echo "$violations"
- echo "Violations found, not proceeding with terraform apply"
- exit 1
- fi
- elif [ $ret_val -ne 0 ]; then
- echo "Error during gcloud beta terraform vet; not proceeding with terraform apply"
- exit 1
- else
- echo "No policy violations detected; proceeding with terraform apply"
- fi
- fi
- step:
name: prod branch execution tf-apply
oidc: true
script:
- cp terraform /usr/bin/
- ls . /usr/bin/
- export SERVICE_ACCOUNT_EMAIL="${GCP_SERVICE_ACCOUNT}"
- ./get_oidctoken.sh
- export GOOGLE_APPLICATION_CREDENTIALS=`pwd`/.gcp_temp_cred.json
- /usr/bin/terraform init -backend-config="bucket=${STATE_BUCKET}" -backend-config="prefix=${BITBUCKET_REPO_FULL_NAME}"
- /usr/bin/terraform plan
- /usr/bin/terraform apply -auto-approve