-
Notifications
You must be signed in to change notification settings - Fork 14
/
.gitlab-ci.yml
75 lines (68 loc) · 2.42 KB
/
.gitlab-ci.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
image: bash
stages:
- test
- plan
- deploy
variables:
TERRAFORM_VERSION: 0.14.6
VAULT_VERSION: 1.6.2
TF_VAR_vault_role: web-pipeline
TF_VAR_vault_backend: web-aws
before_script:
- apk --update add curl unzip bash
- cd /usr/local/bin/
- curl https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_386.zip --output terraform.zip
- unzip terraform.zip
- curl https://releases.hashicorp.com/vault/${VAULT_VERSION}/vault_${VAULT_VERSION}_linux_386.zip --output vault.zip
- unzip vault.zip
- cd -
- terraform version
- vault version
vault_auth:
stage: test
script:
- export VAULT_TOKEN="$(vault write -field=token auth/jwt/login role=$TF_VAR_vault_role jwt=$CI_JOB_JWT)"
- vault token lookup
plan:
stage: plan
artifacts:
paths:
- terraform/project/
expire_in: 1 day
script:
# Check job's ref name
- echo $CI_COMMIT_REF_NAME
# and is this ref protected
- echo $CI_COMMIT_REF_PROTECTED
# Authenticate and get token. Token expiry time and other properties can be configured
# when configuring JWT Auth - https://www.vaultproject.io/api/auth/jwt#parameters-1
- export VAULT_TOKEN="$(vault write -field=token auth/jwt/login role=$TF_VAR_vault_role jwt=$CI_JOB_JWT)"
# Now use the VAULT_TOKEN to provide child token and execute Terraform in AWS env
- cd terraform/project
- export TF_VAR_vault_addr=$VAULT_ADDR
- export TF_VAR_vault_agent_version=$VAULT_VERSION
- terraform init
- terraform plan
apply:
stage: deploy
when: manual
script:
- export VAULT_TOKEN="$(vault write -field=token auth/jwt/login role=$TF_VAR_vault_role jwt=$CI_JOB_JWT)"
# Now use the VAULT_TOKEN to provide child token and execute Terraform in AWS env
- cd terraform/project
- export TF_VAR_vault_addr=$VAULT_ADDR
- export TF_VAR_vault_agent_version=$VAULT_VERSION
- terraform init
- terraform apply -auto-approve
# - vault write -force $(terraform output -raw vault_path_db_rotate)
destroy:
stage: deploy
when: manual
script:
- export VAULT_TOKEN="$(vault write -field=token auth/jwt/login role=$TF_VAR_vault_role jwt=$CI_JOB_JWT)"
# Now use the VAULT_TOKEN to provide child token and execute Terraform in AWS env
- cd terraform/project
- export TF_VAR_vault_addr=$VAULT_ADDR
- export TF_VAR_vault_agent_version=$VAULT_VERSION
- terraform init
- terraform destroy -auto-approve