-
Notifications
You must be signed in to change notification settings - Fork 3
/
docker-rebuild
executable file
·85 lines (67 loc) · 2.57 KB
/
docker-rebuild
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
#!/bin/bash -e
# uncomment to get bash tracing
# set -x
if [ $# != 3 -a $# != 2 ]; then
echo missing arguments: $0 app_folder stage message
exit 34
fi
app_folder=$1
stage=$2
message=$3
# Loading app specific vars
source $app_folder/.deploy_vars
app=$DOCKER_TAG_PREFIX
# Secret password to decrypt GPG encryped file containing app-stage specific vars
gpg_secret_var_name="${GPG_ENV_PREFIX}_${stage^^}_GPG_SECRET"
[ "$GPG_DEBUG" = "true" ] && echo "$gpg_secret_var_name ==> ${!gpg_secret_var_name}"
builder_gitsha=$(git show -s --oneline | awk {'print $1'})
# Building the app
cd $app_folder
gitsha=$(git show -s --oneline | awk {'print $1'})
if [ "$message" = "" ];then
message=$(git show -s --pretty='format:%s, %ad')
fi
time sudo docker buildx build \
--build-arg env_secret_passphrase=${!gpg_secret_var_name} \
--build-arg deploy_stage=$stage \
--build-arg app_gitsha=$gitsha \
--build-arg app_message="$message" \
--build-arg builder_gitsha=$builder_gitsha \
-t $app-$stage-tmp .
cd -
remote_repo=$DOCKER_REMOTE
timestamp=$(date --rfc-3339=seconds | sed 's/[ :+-]//g')
# Setting up 2nd stage Dockerfile with build_label
build_label=$app-$stage:$gitsha-$timestamp
echo -e "FROM $remote_repo/$build_label\nEXPOSE 80" > Dockerfile
git add -f Dockerfile
# Auto gen EB config to the application
./gen_eb_config $app_folder $stage $build_label
git add -f .elasticbeanstalk/config.yml .ebextensions/*.config
# Tagging and pushing to remote
sudo docker tag $app-$stage-tmp $remote_repo/$build_label
sudo docker tag $app-$stage-tmp $remote_repo/$app-$stage:latest
if [[ $(aws --version | grep aws-cli/1) ]]; then
echo "using aws-cli v1 with ecr get-login"
sudo $(aws ecr get-login --region $AWS_REGION --profile $AWS_CRED_PROFILE --no-include-email)
elif [[ $(aws --version | grep aws-cli/2) ]]; then
echo "using aws-cli v2 with ecr get-login-password"
aws_login_pass=$(aws ecr get-login-password --region $AWS_REGION --profile $AWS_CRED_PROFILE)
echo $aws_login_pass | docker login --username AWS --password-stdin $remote_repo
else
echo "unsupported version of aws-cli"
exit 77
fi
sudo docker push $remote_repo/$build_label
sudo docker push $remote_repo/$app-$stage:latest
echo "Ready to deploy? [Enter for yes, CTRL-C to cancel]"
read answer
env_list_var="EB_${stage^^}_LIST"
env_watch_var="EB_${stage^^}_WATCH"
git rm --cached README.md docker-rebuild gpg_config gen_eb_config lib/gpg_encrypted_config_on_s3.rb || echo
for envname in ${!env_list_var}
do
eb deploy --staged --nohang --message "$message" --label "$build_label" $envname
done
git reset
eb events -f ${!env_watch_var}