forked from DataBiosphere/consent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·122 lines (107 loc) · 3.5 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·122 lines (107 loc) · 3.5 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
116
117
118
119
120
121
122
#!/usr/bin/env bash
HELP_TEXT="$(cat <<EOF
Build docker images.
-d | --docker : (default: no action) provide either "build" or "push" to
build or push a docker image. "push" will also perform build.
-g | --gcr-registry: If this flag is set, will push to the specified GCR repository.
-k | --service-account-key-file: (optional) path to a service account key json
file. If set, the script will call "gcloud auth activate-service-account".
Otherwise, the script will not authenticate with gcloud.
-h | --help: print help text.
Examples:
Jenkins build job should run with all options, for example,
./docker/build.sh compile -d push
To build the compile, the image, and push it to a gcr repository.
./docker/build.sh compile -d build -r gcr --project "my-awesome-project"
\t
EOF
)"
# Enable strict evaluation semantics
set -e
# Set default variables
DOCKER_CMD=
BRANCH=${BRANCH:-$(git rev-parse --abbrev-ref HEAD)} # default to current branch
DOCKERHUB_REGISTRY=${DOCKERHUB_REGISTRY:-broadinstitute/$PROJECT}
GCR_REGISTRY=""
ENV=${ENV:-""}
SERVICE_ACCT_KEY_FILE=""
RUN_DOCKER=false
PRINT_HELP=false
if [ -z "$1" ]; then
echo "No argument supplied!"
echo "run '${0} -h' to see available arguments."
exit 1
fi
while [ "$1" != "" ]; do
case $1 in
-d | --docker)
shift
echo "docker command = $1"
DOCKER_CMD=$1
RUN_DOCKER=true
;;
-g | --gcr-registry)
shift
echo "gcr registry = $1"
GCR_REGISTRY=$1
;;
-k | --service-account-key-file)
shift
echo "service-account-key-file = $1"
SERVICE_ACCT_KEY_FILE=$1
;;
-h | --help)
PRINT_HELP=true
;;
*)
echo "Urecognized argument '${1}'."
echo "run '${0} -h' to see available arguments."
exit 1
;;
esac
shift
done
if $PRINT_HELP; then
echo -e "${HELP_TEXT}"
exit 0
fi
# Run gcloud auth if a service account key file was specified.
if [[ -n $SERVICE_ACCT_KEY_FILE ]]; then
TMP_DIR=$(mktemp -d tmp-XXXXXX)
export CLOUDSDK_CONFIG=$(pwd)/${TMP_DIR}
gcloud auth activate-service-account --key-file="${SERVICE_ACCT_KEY_FILE}"
fi
function docker_cmd()
{
if [ $DOCKER_CMD = "build" ] || [ $DOCKER_CMD = "push" ]; then
GIT_SHA=$(git rev-parse origin/${BRANCH})
echo GIT_SHA=$GIT_SHA > env.properties
HASH_TAG=${GIT_SHA:0:12}
echo "Building $DOCKERHUB_REGISTRY:$HASH_TAG"
docker build -t $DOCKERHUB_REGISTRY:${HASH_TAG} --file Dockerfile .
if [ $DOCKER_CMD = "push" ]; then
echo "pushing $PROJECT docker image..."
docker push $DOCKERHUB_REGISTRY:${HASH_TAG}
docker tag $DOCKERHUB_REGISTRY:${HASH_TAG} $DOCKERHUB_REGISTRY:${BRANCH}
docker push $DOCKERHUB_REGISTRY:${BRANCH}
if [[ -n $GCR_REGISTRY ]]; then
docker tag $DOCKERHUB_REGISTRY:${HASH_TAG} $GCR_REGISTRY:${HASH_TAG}
gcloud docker -- push $GCR_REGISTRY:${HASH_TAG}
fi
fi
else
echo "Not a valid docker option! Choose either build or push (which includes build)"
fi
}
function cleanup()
{
echo "cleaning up..."
if [[ -n $SERVICE_ACCT_KEY_FILE ]]; then
gcloud auth revoke && echo 'Token revoke succeeded' || echo 'Token revoke failed -- skipping'
rm -rf ${CLOUDSDK_CONFIG}
fi
}
if $RUN_DOCKER; then
docker_cmd
fi
cleanup