-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpipeline.sh
executable file
·144 lines (123 loc) · 3.79 KB
/
pipeline.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
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
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
#
# My fake deploy pipeline!
#
set -e
export AWS_PROFILE="default"
ENVIRONMENT="develop"
PROJECT_HOME=$(pwd)
PROJECT_COMMIT_SHA=$(git rev-parse --short HEAD)
TIMESTAMP=$(date +"%Y%m%d")
export VAULT_ADDR=http://vault-test.demo.net:8200
function pipeline_apply {
cd ${PROJECT_HOME}/${1}
echo
echo "=========== APPLYING ${1} ==========="
terraform init -backend-config=${ENVIRONMENT}.tfbackend
terraform apply -var-file=${ENVIRONMENT}.tfvars
cd ${PROJECT_HOME}
}
function pipeline_apply_ecs-cluster {
pipeline_apply terraform-ecs-cluster
}
function pipeline_apply_ecs-service {
set_ecr_variables
# pipeline_container_deploy
cd ${PROJECT_HOME}/terraform-ecs-cluster
export TF_VAR_elb_arn=$(terraform output elb_arn)
pipeline_apply terraform-ecs-service
}
function pipeline_apply_ecr {
pipeline_apply terraform-ecr
}
function pipeline_terraform-apply_all {
pipeline_apply_ecr
pipeline_apply_ecs-cluster
pipeline_apply_ecs-service
}
function pipeline_destroy {
cd ${PROJECT_HOME}/${1}
echo
echo "=========== DESTROYING ${1} ==========="
terraform destroy -var-file=${ENVIRONMENT}.tfvars -auto-approve
cd ${PROJECT_HOME}
}
function pipeline_destroy_ecs-cluster {
cd ${PROJECT_HOME}/terraform-ecs-cluster
echo
echo "=========== DESTROY terraform-ecs-cluster ==========="
export asg_name=$(terraform output autoscaling_group_name)
# Terraform bug - must destroy ASG manually first. https://github.com/terraform-providers/terraform-provider-aws/issues/11409
aws autoscaling delete-auto-scaling-group --auto-scaling-group-name ${asg_name} --force-delete
terraform destroy -var-file=${ENVIRONMENT}.tfvars -auto-approve
cd ${PROJECT_HOME}
}
function pipeline_destroy_ecs-service {
set_ecr_variables
pipeline_destroy terraform-ecs-service
}
function pipeline_destroy_ecr {
pipeline_destroy terraform-ecr
}
function pipeline_destroy_all {
pipeline_destroy_ecs-service
pipeline_destroy_ecs-cluster
}
function get_commit_sha {
git rev-parse --short HEAD
}
function set_ecr_variables {
cd ${PROJECT_HOME}/terraform-ecr
export container_name=$(terraform output service_name)
export repo_url=$(terraform output repo_url)
cd ${PROJECT_HOME}
export commit_sha=$(get_commit_sha)
export docker_tag="${TIMESTAMP}-${commit_sha}"
export image_name="${container_name}:${TIMESTAMP}-${commit_sha}"
export TF_VAR_image="${repo_url}:${docker_tag}"
}
function pipeline_container_build {
set_ecr_variables
cd ${PROJECT_HOME}
if [ ! -f "Dockerfile" ]; then
echo
echo "ERROR: NO DOCKERFILE: No file 'Dockerfile' found."
echo " Unable to run this command without it, quitting."
echo
exit 1
fi
echo
echo "=========== BUILDING CONTAINER ==========="
docker build -t ${image_name} .
}
function pipeline_container_push {
echo
echo "=========== PUSHING CONTAINER TO ECR ==========="
aws ecr get-login-password | docker login --username AWS --password-stdin ${repo_url}
docker tag "${image_name}" "${repo_url}:${docker_tag}"
docker push "${repo_url}:${docker_tag}"
}
function pipeline_container_deploy {
pipeline_container_build
pipeline_container_push
}
function pipeline_deploy_all {
echo "=========== DEPLOYING ENTIRE PROJECT ==========="
pipeline_apply_ecr
pipeline_apply_ecs-cluster
pipeline_container_deploy
pipeline_apply_ecs-service
sleep 10
pipeline_vault_init
}
function pipeline_vault_init {
echo
echo "=========== VAULT INIT ==========="
ssh bastion-host "vault operator init -address=${VAULT_ADDR} -recovery-shares=1 -recovery-threshold=1"
}
function main {
command_type="${1}"
command_target="${2}"
"pipeline_${command_type}_${command_target}"
}
main $@