-
Notifications
You must be signed in to change notification settings - Fork 7
/
deploy.sh
executable file
·144 lines (126 loc) · 3.9 KB
/
deploy.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
#! /usr/bin/env bash
# bash boilerplate
set -euo pipefail # strict mode
readonly SCRIPT_NAME="$(basename "$0")"
readonly SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
function log { # Log a message to the terminal.
echo -e "[$SCRIPT_NAME]" "${@:-}"
}
# Config
readonly COMPOSE_FILE=docker-compose.azure.yml
# derived
readonly LOCAL_COMPOSE=(docker-compose --file "$COMPOSE_FILE") # note hypen
readonly CLOUD_COMPOSE=(docker compose --file "$COMPOSE_FILE") # no hyphen
## Low level functions
function verify_docker_context_is_azure(){
docker context use "$AZURE_DOCKER_CONTEXT" || {
log "Failed: docker context use $AZURE_DOCKER_CONTEXT"
return 11
}
docker context inspect | grep "Type" | grep "aci" || {
log 'Error: `docker context inspect` does not report "Type":"aci".'
log 'FYI, [docker context ls] shows:'
docker context ls
return 11
}
}
function verify_docker_is_installed(){
docker version | head -1
}
function verify_compose_cli_is_installed(){
docker version | grep 'Cloud integration'
}
function verify_prereqs_are_met(){
verify_docker_is_installed || {
log 'Error: Docker is not found. To install it, see https://docs.docker.com/get-docker/'
return 11
}
verify_compose_cli_is_installed || {
log 'Error: Docker is not found. To install it, see https://docs.docker.com/get-docker/'
return 11
}
verify_docker_context_is_azure || {
log 'Error: Docker context in use is not Azure.'
log 'Please complete the following tasks. Guidance is linked below.'
log '- Install Docker'
log '- Install "compose-cli", a special cloud version of docker-compose.'
log '- Activate the context with: $ docker context use NAME'
log 'Guidance through "Run a container" here: https://docs.docker.com/cloud/aci-integration/'
return 11
}
}
function build_docker_images() {
docker context use default
"${LOCAL_COMPOSE[@]}" build
}
function push_docker_images() {
docker context use default
"${LOCAL_COMPOSE[@]}" push
}
function prepare_to_deploy(){
build_docker_images || {
log 'Error: Could not build docker image(s)'
log ' Are you missing files in the .dockerignore whitelist?'
return 11
}
push_docker_images || {
log 'Error: Could not push docker image(s) to registry. Do you need to `docker login --username NAME`?'
return 11
}
}
function deploy(){
docker context use "$AZURE_DOCKER_CONTEXT"
"${CLOUD_COMPOSE[@]}" up || {
log 'Error: Docker Compose CLI failed'
return 11
}
}
function source_env_file() {
local retcode=0
log "Sourcing environment from $1"
set -o allexport
set +o errexit
source "$1"
retcode=$?
set -o errexit
set +o allexport
return $retcode
}
function load_env_file() {
source_env_file "$1" || log "Info: Coud not load $1 environment file."
}
function add_date_to_docker_image_env() {
export SPARROW_SITE_DOCKER_TAG="${SPARROW_SITE_DOCKER_IMAGE}:$(date +%Y-%m-%d-%H%M)"
}
function configure_environment() {
load_env_file '.env'
load_env_file '.env.production.local'
add_date_to_docker_image_env
}
function main() {
cd "$SCRIPT_DIR"
log "Deploying Sparrow Reference Web App to the Cloud."
configure_environment || {
log "Error: Could not configure environment."
return 11
}
verify_prereqs_are_met || {
log "Error: Prerequisites are not met."
return 11
}
prepare_to_deploy || {
log "Error: Could not prepare to deploy."
return 11
}
deploy || {
log "Error: Could not deploy."
return 11
}
log '🚀 Successful deployment.'
log '🔃 To deploy new changes, simply run this script again.'
log '🚮 To delete the deployment or see cloud details, visit the Azure Portal:'\
'https://portal.azure.com/#blade/HubsExtension/BrowseResource/resourceType/Microsoft.ContainerInstance%2FcontainerGroups'
log '⏰ In a few minutes the site should be visible here:'
log "🔜 https://$STARTER_SITE_DNS"
}
main