-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·103 lines (86 loc) · 3.61 KB
/
build.sh
File metadata and controls
executable file
·103 lines (86 loc) · 3.61 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
#!/bin/sh
# vim: ft=sh et sw=4
set -e
if [ -n "$DEBUG$PLUGIN_DEBUG" ]; then
set -x
fi
# ANSI colour escape sequences
RED='\033[0;31m'
RESET='\033[0m'
error() { >&2 printf "${RED}Error: %s${RESET}" "$@"; exit 1; }
# $PLUGIN_REPO tag to this repo/repo to push to
# $PLUGIN_PATH specify the build directory (or URL)
# $PLUGIN_CWD cd before calling docker build
# $PLUGIN_DOCKERFILE override Dockerfile location
# $PLUGIN_BUILD_ARGS comma/space separated build arguments
# $PLUGIN_USE_CACHE override to disable --no-cache
# $PLUGIN_NO_LABELS disable automatic image labelling
# $PLUGIN_ARGUMENTS optional extra arguments to pass to `docker build`
# $PLUGIN_RM a flag to immediately `docker rm` the built image
# $PLUGIN_SQUASH builds with --squash
# $PLUGIN_MAKE provides makeflags concurrent of nproc
if [ -z "$PLUGIN_REPO" ]; then
if [ -n "$PLUGIN_RM" ]; then
PLUGIN_REPO="$DRONE_REPO"
elif [ -n "$DRONE_BUILD_NUMBER" ]; then
if [ -n "$DOCKER_IMAGE_TOKEN" ]; then
PLUGIN_REPO="drone/$DRONE_REPO/$DRONE_BUILD_NUMBER/$DOCKER_IMAGE_TOKEN:$DRONE_STAGE_OS-$DRONE_STAGE_ARCH"
else
PLUGIN_REPO="drone/$DRONE_REPO/$DRONE_BUILD_NUMBER:$DRONE_STAGE_OS-$DRONE_STAGE_ARCH"
fi
else
error "Missing 'repo' argument required for building"
fi
fi
# Always specify pull so images are pulled, and intermediate containers removed
ARGS="--pull\0--force-rm"
# Override Dockerfile if specified
[ -n "$PLUGIN_DOCKERFILE" ] && ARGS="$ARGS\0--file=$PLUGIN_DOCKERFILE"
# Squash image if requested
[ -n "$PLUGIN_SQUASH" ] && ARGS="$ARGS\0--squash"
# Specify MAKEFLAGS job concurrency flag
[ -n "$PLUGIN_MAKE" ] && ARGS="$ARGS\0--build-arg\0MAKEFLAGS=-j$(nproc)"
# Specify --no-cache unless caching is requested
[ -z "$PLUGIN_USE_CACHE" ] && ARGS="$ARGS\0--no-cache"
while read -r arg; do
# If arg is '%file: <filename>' then .parse and read file
if echo "$arg" | grep -q "%file\\s*:\\s*"; then
value=$(cat "$(echo ${arg#*:} | xargs)")
name="$(basename "$(echo ${arg//*:} | xargs)" | tr a-z A-Z)"
arg="$name=$value"
fi
if [ -n "${arg// }" ]; then
# Only add arguments if they're not empty
# this prevents the '"docker build" requires exactly 1 argument.' error
ARGS="$ARGS\0--build-arg\0${arg}"
fi
done << EOA
$(echo "$PLUGIN_BUILD_ARGS" | tr ',' '\n')
EOA
export VCS_REF="$DRONE_COMMIT_SHA"
export VCS_URL="$DRONE_REPO_LINK"
export VCS_BRANCH="$DRONE_COMMIT_BRANCH"
if [ -n "$DRONE_JOB_STARTED" ]; then
export BUILD_DATE="$(date -Isec -d "@$DRONE_JOB_STARTED")"
else
export BUILD_DATE="$(date -Isec --utc)"
fi
ARGS="$ARGS\0--build-arg\0VCS_REF=$VCS_REF"
ARGS="$ARGS\0--build-arg\0VCS_URL=$VCS_URL"
ARGS="$ARGS\0--build-arg\0VCS_BRANCH=$VCS_BRANCH"
ARGS="$ARGS\0--build-arg\0BUILD_DATE=$BUILD_DATE"
if [ -z "$PLUGIN_NO_LABELS" ]; then
ARGS="$ARGS\0--label\0org.opencontainers.image.revision=${VCS_REF:0:7}"
ARGS="$ARGS\0--label\0org.opencontainers.image.source=$VCS_URL"
ARGS="$ARGS\0--label\0org.opencontainers.image.branch=$VCS_BRANCH"
ARGS="$ARGS\0--label\0org.opencontainers.image.created=$BUILD_DATE"
fi
>&2 echo "+ docker build ${ARGS//\\0/ } $PLUGIN_ARGUMENTS --tag=$(echo $PLUGIN_REPO | awk '{print tolower($0)}') ${PLUGIN_PATH:-.}"
if [ -n "$PLUGIN_CWD" ]; then
cd "${PLUGIN_CWD}"
fi
# Un-escape the NULL characters to fix arguments with spaces in
printf "$ARGS${PLUGIN_ARGUMENTS//,/\0}\0--tag=$(echo $PLUGIN_REPO | awk '{print tolower($0)}')\0${PLUGIN_PATH:-.}" | xargs -0 docker build
if [ -n "$PLUGIN_RM" ]; then
docker image rm "$PLUGIN_REPO"
fi