Start work on unit tests of CI
[ci.git] / ci.sh
blob671caea7766fa5cf064181a4e34623e69cafec44
1 #!/bin/sh
4 # Copyright (c) 2017 Vojtech Horky
5 # All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
11 # - Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # - Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # - The name of the author may not be used to endorse or promote products
17 # derived from this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 CI_HOME=`which -- "$0" 2>/dev/null`
32 # Maybe, we are running Bash
33 [ -z "$CI_HOME" ] && CI_HOME=`which -- "$BASH_SOURCE" 2>/dev/null`
34 CI_HOME=`dirname -- "$CI_HOME"`
39 list_build_numbers() {
41 cd "$1"
42 find -maxdepth 1 -mindepth 1 -type d -name 'build-*'
43 ) \
44 | cut '-d-' -f 2 \
45 | sort -nr
54 # Defaults
57 CI_WEB_ROOT="$PWD/web-ci"
58 CI_HISTORY_LENGTH=10
59 CI_BUILD_DIR="$PWD/tmp-ci"
60 CI_EXTRA_OPTS=""
62 # Load user configuration
63 if [ -e "ci.rc" ]; then
64 . ./ci.rc
69 # Start the build
72 mkdir -p "$CI_BUILD_DIR"
73 ( cd "$CI_BUILD_DIR"; rm -rf * )
76 # Ensure we are the only ones running
77 LOCK_DIR="$CI_BUILD_DIR/lock-dir"
79 if ! mkdir "$LOCK_DIR"; then
80 echo "Error: another build in progress, aborting." >&2
81 echo "Note: if no other build process is running, try removing $LOCK_DIR." >&2
82 exit 2
86 # Run the rest of the script in subshell (shall ensure that we remove the
87 # lock directory upon termination).
91 mkdir -p "$CI_WEB_ROOT"
93 # Get build number
94 BUILD_NUMBER=`list_build_numbers "$CI_WEB_ROOT" | head -n 1`
95 if [ -z "$BUILD_NUMBER" ]; then
96 BUILD_NUMBER=1
97 else
98 BUILD_NUMBER=$(( $BUILD_NUMBER + 1 ))
101 # Our directory with HTML report
102 WEB_DIR="$CI_WEB_ROOT/build-$BUILD_NUMBER"
103 WEB_DIR_HIDDEN="$CI_WEB_ROOT/.build-$BUILD_NUMBER"
105 $CI_HOME/build.py \
106 "--build-id=$BUILD_NUMBER" \
107 "--build-directory=$CI_BUILD_DIR" \
108 "--artefact-directory=$WEB_DIR_HIDDEN" \
109 "--rss-url=../rss.xml" \
110 "--resource-path=../" \
111 $CI_EXTRA_OPTS
113 if ! [ -e "$WEB_DIR_HIDDEN/report.xml" ]; then
114 echo "$WEB_DIR_HIDDEN/report.xml not found, aborting!" >&2
115 exit 1
118 # Switch the new pages
119 mv "$WEB_DIR_HIDDEN" "$WEB_DIR"
121 # Ensure stylesheet and scripts are available
122 for i in main.css jquery-2.1.4.min.js; do
123 cp -n "$CI_HOME/hbuild/web/$i" "$CI_WEB_ROOT/$i"
124 done
126 # New-enough builds
127 KEPT_BUILDS=`list_build_numbers "$CI_WEB_ROOT" | head -n $CI_HISTORY_LENGTH | grep -v "^$BUILD_NUMBER\$" | paste '-sd '`
129 # Recreate the index page
130 xsltproc \
131 --stringparam LAST_BUILD $BUILD_NUMBER \
132 --stringparam PREVIOUS_BUILDS "$KEPT_BUILDS" \
133 "$CI_HOME/hbuild/web/index.xsl" "$WEB_DIR/report.xml" \
134 >"$CI_WEB_ROOT/index.html"
136 # Recreate RSS
137 xsltproc \
138 --stringparam WEB_ROOT_ABSOLUTE_FILE_PATH "$CI_WEB_ROOT" \
139 --stringparam PREVIOUS_BUILDS "$KEPT_BUILDS" \
140 "$CI_HOME/hbuild/web/rss.xsl" "$WEB_DIR/report.xml" \
141 >"$CI_WEB_ROOT/rss.xml"
143 # Recreate diff RSS
144 KEPT_REPORTS=""
145 for ID in $KEPT_BUILDS; do
146 KEPT_REPORTS="$KEPT_REPORTS $CI_WEB_ROOT/build-$ID/report.xml"
147 done
148 xsltproc \
149 --stringparam PREVIOUS_REPORTS "$KEPT_REPORTS" \
150 "$CI_HOME/hbuild/web/diff-rss.xsl" "$WEB_DIR/report.xml" \
151 >"$CI_WEB_ROOT/diff-rss.xml"
153 # Remove older builds
154 for i in `list_build_numbers "$CI_WEB_ROOT" | tail -n +$(( $CI_HISTORY_LENGTH + 1 ))`; do
155 rm -rf "$CI_WEB_ROOT/build-$i"
156 done
158 # Keep latest/ pointing to the latest build
160 cd "$CI_WEB_ROOT"
161 ln -sTf "build-$BUILD_NUMBER" latest
167 rmdir "$LOCK_DIR"