-
Notifications
You must be signed in to change notification settings - Fork 30
/
build
executable file
·82 lines (71 loc) · 3.22 KB
/
build
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
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
export DEPLOY_ROOT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
source "$DEPLOY_ROOT_DIR/src/common.bash"
if [ "x$1" != "x" ]; then
export DOCKERFILE=$1
else
export DOCKERFILE=Dockerfile
fi
# If BUILDARGS_FROM exists, change SECRET/STAGE_ prefixes to BUILDARG_ for later consumption
buildargs_from
# Set buildargs
set_buildargs
if [[ -n "$CI_REGISTRY_USER" ]]; then
echo "Logging to GitLab Container Registry with CI credentials..."
docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
echo ""
fi
echo "Building application..."
if [ "x$1" != "x" ]; then
echo "Building Dockerfile-based application using the Dockerfile at $1"
# Look for KDH_INSERT_ARGS and edit Dockerfile
insert_args
export SHORT=$(echo "$1" | cut -d'-' -f2-)
if [ "x$KDH_SKIP_LATEST" == "xtrue" ]; then
echo "KDH_SKIP_LATEST is true, not building from latest"
dockerbuild="docker build --pull -t \"$CI_REGISTRY_IMAGE:$SHORT-$CI_REGISTRY_TAG\" $buildargs -f $1 ."
eval $dockerbuild
else
dockerbuild="docker build --pull --cache-from \"$CI_REGISTRY_IMAGE:$SHORT-latest\" -t \"$CI_REGISTRY_IMAGE:$SHORT-$CI_REGISTRY_TAG\" $buildargs -f $1 ."
eval $dockerbuild
fi
echo "Pushing to GitLab Container Registry..."
docker push "$CI_REGISTRY_IMAGE:$SHORT-$CI_REGISTRY_TAG"
elif [[ -f Dockerfile ]]; then
echo "Building Dockerfile-based application..."
# Look for KDH_INSERT_ARGS and edit Dockerfile
insert_args
if [ "x$KDH_SKIP_LATEST" == "xtrue" ]; then
echo "KDH_SKIP_LATEST is true, not building from latest"
dockerbuild="docker build --pull -t \"$CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG\" $buildargs ."
eval $dockerbuild
else
docker pull $CI_REGISTRY_IMAGE:latest || true
dockerbuild="docker build --pull --cache-from \"$CI_REGISTRY_IMAGE:latest\" -t \"$CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG\" $buildargs ."
eval $dockerbuild
fi
echo "Pushing to GitLab Container Registry..."
docker push "$CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG"
else
export DOCKER_HOST='tcp://docker:2375'
echo "No Dockerfile at root of repo. Using herokuish build packs."
echo "Building env file"
build_env
echo "Starting herokuish"
dockerrun="docker run -i --name=\"$CI_CONTAINER_NAME\" -v \"$(pwd):/tmp/app:ro\" gliderlabs/herokuish /bin/bash -c \"/bin/herokuish buildpack build && (rm /app/.env || true)\""
eval $dockerrun
docker commit "$CI_CONTAINER_NAME" "$CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG"
docker rm "$CI_CONTAINER_NAME"
echo "Configuring $CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG docker image..."
docker create --expose 5000 --env PORT=5000 --name="$CI_CONTAINER_NAME" "$CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG" /bin/herokuish procfile start web
docker commit "$CI_CONTAINER_NAME" "$CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG"
docker rm "$CI_CONTAINER_NAME" >/dev/null
echo "Pushing to GitLab Container Registry"
docker push "$CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG"
fi