/
build.sh
executable file
·99 lines (80 loc) · 3.24 KB
/
build.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
#!/bin/sh
# exit when any command fails
set -e
p() {
printf "\e[33m$1\e[0m\n"
}
function _build() {
# 1. Define variables for use in the script
team_name=peoplefinder
ecr_repo_name=peoplefinder-ecr
component=peoplefinder
region='eu-west-2'
aws_profile='ecr-live-peoplefinder'
git_remote_url="https://github.com/ministryofjustice/peoplefinder.git";
docker_endpoint=754256621582.dkr.ecr.eu-west-2.amazonaws.com
docker_registry=${docker_endpoint}/${team_name}/${ecr_repo_name}
current_branch=$(git branch | grep \* | cut -d ' ' -f2)
current_version=$(git rev-parse $current_branch)
short_version=$(git rev-parse --short $current_branch)
docker_build_tag=pf-${current_branch}-${short_version}
export BUILD_TAG=${docker_build_tag}
docker_registry_tag=${docker_registry}:${docker_build_tag}
# 2. Display status message - include warning if the working copy is not clean
p "------------------------------------------------------------------------"
p "Building Peoplefinder image for deployment"
p "Git repository: $git_remote_url"
p "Build tag: $docker_build_tag"
p "Branch: $current_branch"
p "Registry tag: $docker_registry_tag"
if [ -z "$(git status --porcelain)" ]; then
p "Deploying from a clean working directory..."
else
p "\e[31mWarning! Working directory contains new or modified files...\e[0m\n"
git status --porcelain
fi
p "------------------------------------------------------------------------"
# 3. Get a logged in context so we can push images to the ECR
p "Docker login to registry (ECR)..."
docker login -u AWS -p $(aws ecr get-login-password --profile "$aws_profile" --region "$region") $docker_endpoint
# 4. Compose the URL for the remote git object we'll use as the Docker build context
p "Using git repository as Docker context"
git_fetch_url=$git_remote_url#$current_version
# 5. Ensure the current checked out commit is the head of the current branch unless circleci
if [ $(git rev-parse HEAD) == $(git rev-parse @{u}) ] || [ $1 == 'circleci' ]
then
p "Building app container image from git using $short_version"
else
p "\e[31mFatal error: Local git branch is out of sync with origin\e[0m"
p "\e[31mExiting... run git push to sync changes\e[0m\n"
return 0;
fi
# 6. Build the image using the application's docker file and the git build context above
docker build \
--build-arg VERSION_NUMBER=$docker_registry_tag \
--build-arg BUILD_DATE=$(date +%Y-%m-%dT%H:%M:%S%z) \
--build-arg COMMIT_ID=$current_version \
--build-arg BUILD_TAG=$docker_build_tag \
--pull \
--tag ${docker_registry_tag} \
--file ./Dockerfile \
$git_fetch_url
# 7. Tag and push the image to the ECR
case $current_branch in
main)
latest_tag=${docker_registry}:${component}-latest
;;
*)
latest_tag=${docker_registry}:${component}-${current_branch}-latest
;;
esac
docker tag $docker_registry_tag $latest_tag
p "Beginning push to ECR..."
docker push $latest_tag
docker push ${docker_registry_tag}
p "...push to ECR complete"
# 8. Display the tag to use for deployment
p "Pushed to ${latest_tag}"
p "Image created with unique tag: \e[32m$docker_build_tag\e[0m\n"
}
_build $@