forked from kudulab/docker-openjdk-dojo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasks
executable file
·112 lines (96 loc) · 3.83 KB
/
tasks
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
#!/bin/bash
set -Eeuo pipefail
AWS_REGION="eu-west-2"
DOCKER_OPS_VERSION="2.0.0"
DOCKER_OPS_FILE="ops/docker-ops-${DOCKER_OPS_VERSION}"
mkdir -p ops
if [[ ! -f $DOCKER_OPS_FILE ]];then
wget --quiet -O $DOCKER_OPS_FILE https://github.com/kudulab/docker-ops/releases/download/${DOCKER_OPS_VERSION}/docker-ops
fi
source $DOCKER_OPS_FILE
image_name="nhsdev/openjdk-dojo"
image_registry="dockerhub"
image_dir="./image"
image_platform="linux/arm64/v8,linux/amd64"
function get_aws_ssm_secret {
secret_id=$1
json=$(dojo "aws ssm get-parameter --with-decryption --region $AWS_REGION --name $secret_id")
if [ $? != 0 ]; then
>&2 echo "Failed to obtain AWS secret from SSM: $secret_id"
exit 5
fi
echo $json | jq -r ".Parameter.Value"
}
function docker_login {
if [ -z "$DOCKERHUB_USERNAME" ]; then
echo "DOCKERHUB_USERNAME must be your dockerhub username or an API token"
exit 5;
fi
if [ -z "$DOCKERHUB_PASSWORD" ]; then
echo "DOCKERHUB_PASSWORD must be your dockerhub password or an API token"
exit 5;
fi
echo "$DOCKERHUB_PASSWORD" | docker login --username $DOCKERHUB_USERNAME --password-stdin
}
function create_docker_builder() {
local builder_name=arch_builder
docker buildx &> /dev/null || { echo 'Please install and enable buildx - https://docs.docker.com/buildx/working-with-buildx/ ' ; exit 1; }
# Allowing linux machines to build arm64
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes || true
if docker buildx inspect arch_builder --bootstrap &> /dev/null
then
echo "Builder already exists removing previous builder"
docker buildx rm ${builder_name}
fi
echo "Creating new multi arch builder"
docker buildx create --name ${builder_name} --platform linux/amd64,linux/arm64 --driver docker-container --use
docker buildx inspect --bootstrap
docker buildx ls
}
function build_multi_arch() {
local image_dir=${1?image_dir not set}
local image_short_name=${2?image_short_name not set}
local image_tag=${3:-}
local image_registry=${4:-dockerhub}
local image_platform=${5:-linux/arm64/v8,linux/amd64}
create_docker_builder
docker_ops::log_info "image_dir set to: ${image_dir}"
docker_ops::log_info "image_short_name set to: ${image_short_name}"
docker_ops::log_info "imagerc_filename set to: ${imagerc_filename}"
docker_ops::create_imagerc "${image_dir}" "${imagerc_filename}" "${image_short_name}" "${image_tag}" "${image_registry}"
docker_ops::source_imagerc ${image_dir} ${imagerc_filename}
initial_dir="$(pwd)"
cd "${image_dir}"
docker_ops::log_info "image_tag set to: ${image_tag}"
set -x -e
docker buildx build --platform "${image_platform}" -t "${KUDU_DOCKER_IMAGE_URL}" --push .
}
command="$1"
set +u
case "${command}" in
build_local)
image_tag=$(git rev-parse HEAD)
# build image and push to a test registry
docker_ops::docker_build "${image_dir}" "${imagerc_filename}" "${image_name}" "${image_tag}" "${image_registry}"
;;
build_multi_arch)
DOCKERHUB_USERNAME=$(get_aws_ssm_secret "/repo/prod/user-input/prm-team-dockerhub-username")
DOCKERHUB_PASSWORD=$(get_aws_ssm_secret "/repo/prod/user-input/prm-team-dockerhub-password")
docker_login
image_tag=$(git rev-parse HEAD)
# build image and push to registry
build_multi_arch ${image_dir} "${image_name}" "${image_tag}" "${image_registry}" "${image_platform}"
;;
build)
DOCKERHUB_USERNAME=$(get_aws_ssm_secret "/repo/prod/user-input/prm-team-dockerhub-username")
DOCKERHUB_PASSWORD=$(get_aws_ssm_secret "/repo/prod/user-input/prm-team-dockerhub-password")
docker_login
./tasks build_local
docker_ops::push "${image_dir}" "${imagerc_filename}"
;;
*)
echo "Invalid command: '${command}'"
exit 1
;;
esac
set +e