Skip to content

Commit b375683

Browse files
authored
Build packages of specified remote git and ref (#4)
Provide the functionality to be able to build packages of any provided git remote and ref without needing to clone the repo first. * Remote git repository builder on Ubuntu 16.04 * Remote git repository builder on Ubuntu 14.04 * Update README for building remote repository Fixes #1
1 parent 2b3e441 commit b375683

3 files changed

Lines changed: 384 additions & 37 deletions

File tree

README.md

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ This will give portable, immutable and reproducable mechanism to build packages
99
- [Supported tags and respective `Dockerfile` links](#supported-tags-and-respective-dockerfile-links)
1010
- [Packges installed in conatiner](#packges-installed-in-conatiner)
1111
- [Build DEB packages](#build-deb-packages)
12-
- [Clone Apache CloudStack source code](#clone-apache-cloudstack-source-code)
1312
- [Pull Docker images](#pull-docker-images)
14-
- [Build packages](#build-packages)
13+
- [Build local repository](#build-local-repository)
14+
- [Clone Apache CloudStack source code](#clone-apache-cloudstack-source-code)
15+
- [Build packages of local repository](#build-packages-of-local-repository)
16+
- [Build remote repository](#build-remote-repository)
17+
- [Build packages of remote repository](#build-packages-of-remote-repository)
1518
- [Building tips](#building-tips)
1619
- [Maven cache](#maven-cache)
1720
- [Adjust host owner permission](#adjust-host-owner-permission)
@@ -45,15 +48,6 @@ List of available packages inside the container:
4548

4649
Building DEB packages with the Docker container is rather easy, a few steps are required:
4750

48-
### Clone Apache CloudStack source code
49-
50-
The first step required is to clone the CloudStack source code somewhere on the filesystem, in `/tmp` for example:
51-
52-
cd /tmp
53-
git clone https://github.com/apache/cloudstack.git cloudstack
54-
55-
Now that you have done so we can continue.
56-
5751
### Pull Docker images
5852

5953
Let's assume we want to build packages for Ubuntu 16.04 (Xenial). We pull that image first:
@@ -62,9 +56,21 @@ Let's assume we want to build packages for Ubuntu 16.04 (Xenial). We pull that i
6256

6357
You can replace `ubuntu1604` tag by `ubuntu1404` or `latest` if you want.
6458

65-
### Build packages
59+
### Build local repository
60+
61+
You can clone the CloudStack source code from repository locally on your machine and build packages against that.
62+
63+
#### Clone Apache CloudStack source code
64+
65+
The first step required is to clone the CloudStack source code somewhere on the filesystem, in `/tmp` for example:
66+
67+
git clone https://github.com/apache/cloudstack.git /tmp/cloudstack
68+
69+
Now that you have done so we can continue.
70+
71+
#### Build packages of local repository
6672

67-
Now that we have the Docker images we can build packages by mapping `/tmp` into `/mnt/build` in the container. (Note that the container always expects the `cloudstack` code exists in `/mnt/build` path.)
73+
Now that we have cloned the CloudStack source code locally, we can build packages by mapping `/tmp` into `/mnt/build` in the container. (Note that the container always expects the `cloudstack` code exists in `/mnt/build` path.)
6874

6975
docker run \
7076
-v /tmp:/mnt/build \
@@ -78,6 +84,31 @@ Or if your local cloudstack folder has other name, you need to map it to `/mnt/b
7884

7985
After the build has finished the *.deb* packages are available in */tmp/cloudstack/dist/debbuild/DEBS* on the host system.
8086

87+
### Build remote repository
88+
89+
Also you can build RPM packages of any remote repository without the need to manually clone it first. You only need to specify git remote and git ref you intend to build from.
90+
91+
#### Build packages of remote repository
92+
93+
Now let's assume we want to build packages of `HEAD` of `master` branch from https://github.com/apache/cloudstack repository, we build packages by mapping `/tmp` into `/mnt/build` in the container. The container will clone the repository (defined by `--git-remote` flag) and check out the REF (defined by `--git-ref` flag) in `/mnt/build/cloudstack` inside the container and can be accessed from `/tmp/cloudstack` from the host machine.
94+
95+
docker run \
96+
-v /tmp:/mnt/build \
97+
khos2ow/cloudstack-deb-builder:ubuntu1604 \
98+
--git-remote https://github.com/apache/cloudstack.git \
99+
--git-ref master \
100+
[ARGS...]
101+
102+
Note that any valid git Refspec is acceptable, such as:
103+
104+
- `refs/heads/<BRANCH>` to build specified Branch
105+
- `<BRANCH>` short version of build specified Branch
106+
- `refs/pull/<NUMBER>/head` to build specified GitHub Pull Request
107+
- `refs/merge-requests/<NUMBER>/head` to build specified GitLab Merge Request
108+
- `refs/tags/<NAME>` to build specified Tag
109+
110+
After the build has finished the *.deb* packages are available in */tmp/cloudstack/dist/debbuilds/DEBS* on the host system.
111+
81112
## Building tips
82113

83114
Check the following tips when using the builder:

ubuntu1404/docker-entrypoint.sh

Lines changed: 170 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,174 @@
1818

1919
set -e
2020

21+
# Flag to show help text
22+
show_help=false
23+
24+
# Using remote git repository variables
25+
use_remote=false
26+
git_remote=""
27+
git_ref=""
28+
remove_first=false
29+
30+
# packaging flags to be sent to script
31+
PKG_ARGS=""
32+
HELP_ARG=""
33+
34+
while [ -n "$1" ]; do
35+
case "$1" in
36+
--git-remote)
37+
if [ -n "$git_remote" ]; then
38+
echo "Error: you have already entered value for --git-remote"
39+
exit 1
40+
else
41+
git_remote=$2
42+
use_remote=true
43+
shift 2
44+
fi
45+
;;
46+
47+
--git-ref)
48+
if [ -n "$git_ref" ]; then
49+
echo "Error: you have already entered value for --git-ref"
50+
exit 1
51+
else
52+
git_ref=$2
53+
use_remote=true
54+
shift 2
55+
fi
56+
;;
57+
58+
--remove-first)
59+
if [ $remove_first = true ]; then
60+
echo "Error: you have already entered --remove_first"
61+
exit 1
62+
else
63+
remove_first=true
64+
shift 1
65+
fi
66+
;;
67+
68+
-h | --help)
69+
if [ $show_help = true ]; then
70+
echo "Error: you have already entered -h, --help"
71+
exit 1
72+
else
73+
show_help=true
74+
HELP_ARG="$1"
75+
shift 1
76+
fi
77+
;;
78+
79+
-* | --* | *)
80+
PKG_ARGS="$PKG_ARGS $1"
81+
shift 1
82+
;;
83+
esac
84+
done
85+
86+
set -- $PKG_ARGS
87+
88+
# Both of --git-remote AND --git-ref must be specified at the same time
89+
if [ $use_remote = true ]; then
90+
if [ -z "$git_remote" -o -z "$git_ref" ]; then
91+
echo "Error: you must specify --git-remote and --git-ref at the same time"
92+
exit 1
93+
fi
94+
fi
95+
96+
# Check if cloudstack directory exists or not. Options are either:
97+
#
98+
# 1) cloudstack directory is provided through the host's volume
99+
# 2) cloudstack directory is NOT provided and git remote and ref are provided
100+
#
101+
# Any combination of the above situations is invalid.
102+
if [ -d "/mnt/build/cloudstack" ]; then
103+
if [ $use_remote = true ]; then
104+
if [ $remove_first = false ]; then
105+
echo "Error: Could not clone remote git repository, '/mnt/build/cloudstack' exists"
106+
exit 1
107+
else
108+
echo "Removing /mnt/build/cloudstack ..."
109+
rm -rf /mnt/build/cloudstack
110+
fi
111+
fi
112+
else
113+
if [ $use_remote = false ]; then
114+
echo "Could not find '/mnt/build/cloudstack'"
115+
exit 1
116+
fi
117+
fi
118+
119+
# Clone the remote provided git repo and ref
120+
if [ $use_remote = true ]; then
121+
echo "Cloning $git_remote ..."
122+
git clone --quiet --depth=50 $git_remote /mnt/build/cloudstack
123+
124+
cd /mnt/build/cloudstack
125+
126+
echo "Fetching $git_ref ..."
127+
git fetch --quiet origin +$git_ref:
128+
129+
echo "Checking out $git_ref ..."
130+
git checkout --quiet --force FETCH_HEAD
131+
132+
echo -e "\n--------\n"
133+
fi
134+
135+
# Make sure build-deb.sh script exists before going any further
136+
if [ ! -f "/mnt/build/cloudstack/packaging/build-deb.sh" ]; then
137+
echo "Could not find '/mnt/build/cloudstack/packaging/build-deb.sh'"
138+
exit 1
139+
fi
140+
141+
# convert LONG flags to SHORT flags for anything prior 4.12.x.x
142+
echo "Detecting CloudStack version ..."
143+
pom_version=$(cd /mnt/build/cloudstack; mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec)
144+
major_version=$(echo ${pom_version} | cut -d. -f1)
145+
minor_version=$(echo ${pom_version} | cut -d. -f2)
146+
echo -e "\n--------\n"
147+
148+
if [ $major_version -lt 4 ] || [ $major_version -eq 4 -a $minor_version -lt 12 ]; then
149+
if [ $show_help = true ]; then
150+
HELP_ARG=""
151+
fi
152+
fi
153+
154+
# Show help, both from docker-entrypoint.sh and /mnt/build/cloudstack/packaging/build-deb.sh
155+
if [ $show_help = true ]; then
156+
if [ -n "$HELP_ARG" ]; then
157+
help=$(cd /mnt/build/cloudstack/packaging; bash -x ./build-deb.sh $HELP_ARG)
158+
else
159+
help=""
160+
fi
161+
cat << USAGE
162+
Usage: docker run ... khos2ow/cloudstack-deb-builder [DOCKER_OPTIONS] ... [PACKAGING_OPTIONS]...
163+
CloudStack DEB builder which acts as a wrapper for CloudStack package script. Optionally
164+
you can specify remote git repository and ref to be cloned and checked out and run the
165+
packaging script on in.
166+
167+
Optional arguments:
168+
--git-remote string Set the git remote repository to clone (must be set together with \`--git-ref\`) (default: none)
169+
--git-ref string Set the ref from remote repository to check out (must be set together with \`--git-remote\`) (default: none)
170+
--remove-first Remove existing \`/mnt/build/cloudstack\` directory before cloning (default: false)
171+
172+
Other arguments:
173+
-h, --help Display this help message and exit
174+
175+
Examples:
176+
docker run ... khos2ow/cloudstack-deb-builder [PACKAGING_OPTIONS] ...
177+
docker run ... khos2ow/cloudstack-deb-builder --git-remote https://path.to.repo/cloudstack.git --git-ref foo-branch [PACKAGING_OPTIONS] ...
178+
179+
--------
180+
181+
$help
182+
183+
USAGE
184+
exit 0
185+
fi
186+
21187
# Adjust user and group provided by host
22-
adjust_owner() {
188+
function adjust_owner() {
23189
# if both set then change the owner
24190
if [ -n "${USER_ID}" -a -z "${USER_GID}" ]; then
25191
chown -R ${USER_ID} /mnt/build/cloudstack
@@ -28,19 +194,11 @@ adjust_owner() {
28194
fi
29195
}
30196

31-
if [ ! -d "/mnt/build/cloudstack" ]; then
32-
echo "Could not find directory 'cloudstack'"
33-
exit 1
34-
fi
35-
36-
if [ ! -f "/mnt/build/cloudstack/packaging/build-deb.sh" ]; then
37-
echo "Could not find file 'cloudstack/packaging/build-deb.sh'"
38-
exit 1
39-
fi
40-
41197
{
198+
cd /mnt/build/cloudstack/packaging
199+
42200
# do the packaging
43-
bash -x /mnt/build/cloudstack/packaging/build-deb.sh $@ && {
201+
bash -x ./build-deb.sh $@ && {
44202
mkdir -p /mnt/build/cloudstack/dist/debbuild/DEBS
45203

46204
cp /mnt/build/cloudstack-*.deb /mnt/build/cloudstack/dist/debbuild/DEBS

0 commit comments

Comments
 (0)