Repository navigation
Expand file tree
/
Copy pathdocker-entrypoint
More file actions
115 lines (97 loc) · 3.8 KB
/
Copy pathdocker-entrypoint
File metadata and controls
115 lines (97 loc) · 3.8 KB
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
#!/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"
# Workflow logs are public in public repositories. The AWS CLI echoes ARNs and
# bucket names, both of which embed the account ID, so ask the runner to mask it
# wherever it appears (including in error messages). Best effort: a key without
# sts:GetCallerIdentity should still be able to deploy.
if account="$(aws sts get-caller-identity --query Account --output text 2>/dev/null)"; then
echo "::add-mask::$account"
fi
# 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. Only errors are printed, so the bucket name stays out of the log.
yellow "Uploading app version..."
aws s3 cp "$ZIP_NAME" "s3://$INPUT_BUCKET/$INPUT_APP_NAME/" --only-show-errors
# Create new application version
yellow "Creating a new app version..."
# Elastic Beanstalk limits the description to 200 characters, so use only the commit
# subject line and truncate defensively
description="$(git log -1 --pretty=%s) (${GITHUB_SHA:0:7})"
description="${description:0:200}"
# The full response includes the version's ARN and source bucket; log only its status.
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" \
--query 'ApplicationVersion.Status' --output text
# Deploy application version
yellow "Updating environment..."
# The full response includes the environment ID, load balancer hostname and
# CNAME; log only its status.
aws elasticbeanstalk update-environment \
--application-name "$INPUT_APP_NAME" \
--environment-name "$INPUT_ENV_NAME" \
--version-label "$INPUT_VERSION_LABEL" \
--query 'Status' --output text
# 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"