]> git.ipfire.org Git - people/ms/nightly-builds.git/blob - build.sh
3b49b1442c6385b225e27dbe54d3939ea713e732
[people/ms/nightly-builds.git] / build.sh
1 #!/bin/bash -l
2
3 BASEDIR="/build/nightly"
4 LOCKFILE="/tmp/.nightly-builds.lock"
5
6 UPLOAD_DIR="${BASEDIR}/upload"
7 UPLOAD_TO="pakfire@git.ipfire.org:/pub/nightly"
8 RSYNC_ARGS=""
9
10 extract_installer_from_iso() {
11 local dir="${1}"
12
13 local tmpdir="$(mktemp -d)"
14 mkdir -p "${dir}/images"
15
16 local file
17 for file in ${dir}/*.iso; do
18 if mount -o loop,ro "${file}" "${tmpdir}"; then
19 local f
20 for f in vmlinuz instroot; do
21 cp -f "${tmpdir}/boot/isolinux/${f}" "${dir}/images"
22 done
23 umount "${tmpdir}"
24
25 local ext
26 for ext in "" ".md5"; do
27 ln -s --relative "${file}${ext}" \
28 "${dir}/images/installer.iso${ext}"
29 done
30 break
31 fi
32 done 2>/dev/null
33
34 rm -rf "${tmpdir}"
35 }
36
37 build() {
38 local dir="${1}"
39 shift
40
41 # Check if this actually source of IPFire 2
42 [ -x "${dir}/make.sh" ] || return 1
43
44 pushd "${dir}"
45
46 local branch="$(git config build.branch)"
47 [ -z "${branch}" ] && branch="master"
48
49 local commit_old="$(git rev-parse HEAD)"
50 git fetch
51
52 # Search for the latest core branch
53 if [ "${branch}" = "core" ]; then
54 branch=$(git branch -r | awk -F/ '{ print $NF }' | grep ^core | sort --version-sort | tail -n1)
55 fi
56
57 # If the branch was not updated, we won't build
58 local commit_new="$(git rev-parse origin/${branch})"
59 [ "${commit_old}" = "${commit_new}" ] && return 2
60
61 # Checkout the latest commit
62 git reset --hard "${commit_new}"
63
64 #local now="$(date -u +"%Y-%m-%d-%H:%M")"
65 local now="$(git log --format="%cI" -1 "${commit_new}")"
66
67 local targets="$(git config build.targets)"
68 [ -z "${targets}" ] && targets="i586"
69
70 local target
71 for target in ${targets}; do
72 local build_path="${UPLOAD_DIR}/${branch}/${now}-${commit_new:0:8}"
73 local build="${build_path}/${target}"
74
75 # Ready for build
76 ./make.sh --target="${target}" clean
77
78 # Download the toolchain if required
79 ./make.sh --target="${target}" gettoolchain || continue
80
81 # Download all sources
82 ./make.sh --target="${target}" downloadsrc || continue
83
84 # Execute the build
85 mkdir -p "${build}"
86 ./make.sh --target="${target}" build | tee "${build}/build.log"
87 local ret=${PIPESTATUS[0]}
88
89 # Save the changelog
90 git log -50 > "${build}/changelog.txt"
91
92 # Save the result
93 if [ "${ret}" = "0" ]; then
94 mv -v *.iso *.img.gz *.tar.bz2 *.md5 packages/ "${build}"
95 extract_installer_from_iso "${build}"
96 touch "${build}/.success"
97 fi
98 mv -v log/ "${build}"
99
100 # Cleanup the build environment
101 ./make.sh --target="${target}" clean
102
103 # Upload the result
104 # If that failed, we will keep the result and it will
105 # be retried with the next build. If that succeeded, the
106 # build will be removed from disk.
107 if sync; then
108 rm -rf "${build_path}"
109 fi
110 done
111
112 popd
113 }
114
115 sync() {
116 mkdir -p "${UPLOAD_DIR}"
117
118 rsync -avHz --progress ${RSYNC_ARGS} \
119 "${UPLOAD_DIR}/" "${UPLOAD_TO}"
120 }
121
122 is_locked() {
123 [ -e "${LOCKFILE}" ]
124 }
125
126 lock() {
127 touch "${LOCKFILE}"
128 }
129
130 unlock() {
131 rm -f "${LOCKFILE}"
132 }
133
134
135 # Don't start again if the script is already running
136 # or if an other build script is running
137 if is_locked || pgrep make.sh >/dev/null; then
138 exit 0
139 fi
140
141 # Lock
142 trap unlock EXIT
143 lock
144
145 for repo in $(find ${BASEDIR} -maxdepth 3 -type d -name ".git"); do
146 [ -d "${repo}" ] || continue
147
148 build "$(dirname ${repo})"
149 done
150
151 exit 0