forked from perfsonar/debian-docker-buildmachines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-in-docker
executable file
·162 lines (142 loc) · 5.93 KB
/
build-in-docker
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env bash
# Usage info
show_help() {
cat << EOF
Usage: ${0##*/} -BDjk [args_as_for_ps-source-builder] [gitrepo]
This script will build docker images and launch a git repo build in a container
Requirements:
- Needs to be in a directory at the same level as the gitrepo to be built
- The source branch to build from need to be checked out
- Functionnal docker buildx setup
Arguments understood:
-B don't rebuild source package but use previously build one
-D debug build, will get you into a shell instead of doing the build
-j build in Jenkins, use -T switch when calling docker exec
-k: use and keep locally built packages (can be useful to solve dependencies)
- all other args are directly passed to the ps-source-builder script
but they need to be separated with a blank space ' '
- gitrepo: name of an already checked out git repository to build
The OSimage and the pS repo used to build the package are coming from what
is defined in the debian/gbp.conf file, respectively in the builder and
debian-branch values.
Some examples:
./build-in-docker -t debian/stretch/4.4.0-1.b1-1 toolkit
package=pscheduler-archiver-rabbitmq ./build-in-docker pscheduler
EOF
}
# By default, the setup is to build packages for the AMD64 architecture
# If you want to build for different ones, you should call the scripts with the ARCHES
# variable set similarily to (here for all valid and supported architectures):
# ARCHES='linux/amd64 linux/arm64 linux/arm/v7 linux/ppc64le' ./build-in-docker
if [ -z "$ARCHES" ]; then
declare -a ARCHES=("linux/amd64")
fi
# Defaults
source_and_binary=true
need_shift=false
jenkins_build=false
keep_builds=false
OTHEROPT=1
# Parsing options
while getopts "BDhjb:t:ksu" OPT; do
case $OPT in
B) source_and_binary=false ; need_shift=true ;;
D) debug_build="bash" ; need_shift=true ;;
j) jenkins_build=true ; need_shift=true ;;
k) keep_builds=true ; need_shift=true ;;
# The other options will get passed to ps-source-builder so we must not shift them
b|t) ((OTHEROPT+=2)) ;;
s|u) ((OTHEROPT+=1)) ;;
h)
show_help >&2
exit 1 ;;
esac
done
shift $((OPTIND-OTHEROPT))
# Get the gitrepo to build
GITREPO=`echo $@ | awk '{print $NF}'`
if [[ "$GITREPO" =~ ^(pscheduler|minor-packages)$ && -z "$package" ]]; then
echo "For pscheduler and minor-packages repo, the package variable needs to be set to the package to build!"
exit 1
elif [ -z "$package" ]; then
package="."
fi
# Check if directory contains a Debian package to build
GBPCONF=`find ../$GITREPO/$package -path */debian/gbp.conf -not -path */shared/debian/gbp.conf | head -1`
if [ $? -ne 0 ]; then
echo "This doesn't seem to be a package I can build."
echo "There is no debian/gbp.conf file in ../$GITREPO/$package"
exit 1
fi
# Which pS repo are we building for?
# TODO: when a tag is given, we should build from staging
REPO=`awk '/debian-branch / {gsub(".[0-9]+$","",$3); print "perfsonar-"$3"-snapshot"}' $GBPCONF`
# Which OS are we building on?
OSimage=`awk '/ DIST=/ {gsub("DIST=","",$3); print $3}' $GBPCONF`
case $OSimage in
bionic) OSimage=ubuntu:$OSimage ;;
buster) OSimage=debian:$OSimage ;;
focal) OSimage=ubuntu:$OSimage ;;
stretch) OSimage=debian:$OSimage ;;
esac
export OSimage ARCH REPO useproxy
if [ -n "$proxy" ]; then
# If $proxy is set, then we will use it
useproxy=with
else
useproxy=without
fi
# Are we running in Jenkins?
if $jenkins_build ; then
docker_exec_param="-T"
echo "We're building in Jenkins with docker exec parameters: $docker_exec_param"
else
docker_exec_param=""
fi
# Do we keep previous builds in local repo?
if $keep_builds ; then
ps_build_param="-k"
else
ps_build_param=""
fi
# Initialise
LOGS_PREFIX="logs/binary_build"
mkdir -p ${LOGS_PREFIX%%/*}
rm -f ${LOGS_PREFIX}_*.log
# We will launch only the upper declared containers ARCH
CONTAINERS=""
for ARCH in ${ARCHES[@]}; do
LARCH=${ARCH#*\/}
CONTAINERS+=${LARCH/\/}"_build "
done
docker compose down
echo $REPO
# Launch all containers, with deb_build running on amd64
docker compose up -d deb_build $CONTAINERS
if $source_and_binary ; then
# Build source package and feed all the parameters to ps-builder
echo -e "\n\033[1m===== Building source package in source-build container =====\033[0m"
docker compose exec $docker_exec_param --user psbuild -e package=$package deb_build /usr/local/bin/ps-source-builder $ps_build_param $*
# TODO: how to actually stop?
[ $? -eq 0 ] || exit 1
else
echo -e "\n\033[1;33m -B : I'm not building the source package, but will use the one already existing in the local ../build_source directory.\033[0m\n"
fi
# Build binary package for all architectures
# TODO: Need to improve on that and run containers in parallel, like for the Docker build
# If not possible, then need to launch all containers and check when each build finishes to collect result code
for ARCH in ${ARCHES[@]}; do
LARCH=${ARCH#*\/}
LARCH=${LARCH/\/}
echo -e "\n===== Building \033[1mbinary package\033[0m on \033[1m$ARCH.$OSimage\033[0m in \033[1m$LARCH-build\033[0m container ====="
[ -n "$debug_build" ] && echo -e "\033[1;33m -D : We will run a debug binary build, i.e. call $debug_build.\033[0m\n"
# TODO: can we run all builds in parallel?
docker compose exec $docker_exec_param -e package=$package ${LARCH}_build /usr/local/bin/ps-binary-builder $ps_build_param $GITREPO $debug_build
done
# Shuting down
echo -e "\nNow stopping all containers."
ARCH=linux/amd64 docker compose down
### Debugging
# ARCH=linux/amd64 OSimage=debian:stretch REPO=perfsonar-4.4-snapshot docker compose up -d deb_build amd64_build
### Running again a binary build with same source package
# ARCH=amd64 OSimage=debian:stretch REPO=perfsonar-4.4-snapshot docker compose exec --user psbuild amd64_build /usr/local/bin/ps-binary-builder $GITREPO