-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker-entrypoint
99 lines (82 loc) · 2.87 KB
/
docker-entrypoint
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
#!/bin/bash
set -e -o pipefail
# Configure git
git config --global --add safe.directory /github/workspace
# Renamed input parameters
export AWS_ACCESS_KEY_ID="$INPUT_AWS_ACCESS_KEY_ID"
export AWS_SECRET_ACCESS_KEY="$INPUT_AWS_SECRET_ACCESS_KEY"
export AWS_DEFAULT_REGION="$INPUT_AWS_DEFAULT_REGION"
# Default inputs from env
INPUT_ENV_NAME="${INPUT_ENV_NAME:=$INPUT_APP_NAME}"
UUID="$(python -c 'import uuid; print(uuid.uuid4().hex)')"
INPUT_VERSION_LABEL="${INPUT_VERSION_LABEL:=${UUID//-}}"
ZIP_NAME="$INPUT_APP_NAME-$INPUT_VERSION_LABEL.zip"
yellow() {
echo -e "\033[33m$1\033[0m"
}
red() {
echo -e "\033[31m$1\033[0m"
}
yellow "App bundle name: $ZIP_NAME"
yellow "Creating app bundle..."
zip -r "$ZIP_NAME" . -x 'docker-compose*' '.git*'
# Add docker-compose.prod.yaml to the ZIP file if it exists
if [ -f docker-compose.prod.yaml ]; then
yellow "Adding docker-compose.prod.yaml to the app bundle as docker-compose.yaml..."
mv docker-compose.prod.yaml docker-compose.yaml
zip -g "$ZIP_NAME" docker-compose.yaml
else
yellow "docker-compose.prod.yaml not found, skipping addition to the app bundle..."
fi
env_ready() {
aws elasticbeanstalk describe-environment-health \
--environment-name "$INPUT_ENV_NAME" \
--attribute-names Status | grep -q '"Status": "Ready"'
}
# Ensure environment is ready
yellow "Ensuring environment is ready..."
max_retries=30
curr_retry=1
until env_ready; do
if [[ $curr_retry -gt $max_retries ]]; then
red "Timed out before environment $INPUT_ENV_NAME was ready"
exit 1
fi
sleep 60 # wait for 1 minute
curr_retry=$(( $curr_retry + 1 ))
done
# Upload zip to S3
yellow "Uploading app version..."
aws s3 cp "$ZIP_NAME" "s3://$INPUT_BUCKET/$INPUT_APP_NAME/"
# Create new application version
yellow "Creating a new app version..."
description="$(git log -1 --pretty=%B) (${GITHUB_SHA:0:7})"
aws elasticbeanstalk create-application-version \
--application-name "$INPUT_APP_NAME" \
--description "$description" \
--version-label "$INPUT_VERSION_LABEL" \
--source-bundle S3Bucket="$INPUT_BUCKET",S3Key="$INPUT_APP_NAME/$ZIP_NAME"
# Deploy application version
yellow "Updating environment..."
aws elasticbeanstalk update-environment \
--application-name "$INPUT_APP_NAME" \
--environment-name "$INPUT_ENV_NAME" \
--version-label "$INPUT_VERSION_LABEL"
# Wait until environment is ready
yellow "Waiting until update is complete..."
sleep 60
max_retries=30
curr_retry=1
until env_ready; do
if [[ $curr_retry -gt $max_retries ]]; then
red "Timed out before environment $INPUT_ENV_NAME was ready"
exit 1
fi
sleep 60 # wait for 1 minute
curr_retry=$(( $curr_retry + 1 ))
done
# Check if deployment succeeded
yellow "Checking if deployment succeeded..."
env_ready && aws elasticbeanstalk describe-environments \
--application-name "$INPUT_APP_NAME" \
--environment-names "$INPUT_ENV_NAME" | grep -q "$INPUT_VERSION_LABEL"