]> git.ipfire.org Git - people/ms/nightly-builds.git/blobdiff - build.sh
build.sh: Delete successfully uploaded files
[people/ms/nightly-builds.git] / build.sh
index e87111407d04049c7b540ba292224918334e23db..fcb7699c5f07302458b3476f0852a97f74c19e90 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -4,10 +4,8 @@ BASEDIR="/build/nightly"
 LOCKFILE="/tmp/.nightly-builds.lock"
 
 UPLOAD_DIR="${BASEDIR}/upload"
-UPLOAD_TO="pakfire@git.ipfire.org:/pub/nightly"
-RSYNC_ARGS=""
-
-export MAKETUNING="-j$(getconf _NPROCESSORS_ONLN)"
+UPLOAD_TO="pakfire@fs01.haj.ipfire.org:/pub/nightly"
+RSYNC_ARGS=( "-avH" "--progress" "--delay-updates" )
 
 extract_installer_from_iso() {
        local dir="${1}"
@@ -36,6 +34,63 @@ extract_installer_from_iso() {
        rm -rf "${tmpdir}"
 }
 
+uriencode() {
+       local path="${1}"
+
+       local IFS="/"
+
+       local s
+       local segments=()
+       for s in ${path}; do
+               s="${s//'%'/%25}"
+               s="${s//' '/%20}"
+               s="${s//'"'/%22}"
+               s="${s//'#'/%23}"
+               s="${s//'$'/%24}"
+               s="${s//'&'/%26}"
+               s="${s//'+'/%2B}"
+               s="${s//','/%2C}"
+               s="${s//'/'/%2F}"
+               s="${s//':'/%3A}"
+               s="${s//';'/%3B}"
+               s="${s//'='/%3D}"
+               s="${s//'?'/%3F}"
+               s="${s//'@'/%40}"
+               s="${s//'['/%5B}"
+               s="${s//']'/%5D}"
+
+               segments+=( "${s}" )
+       done
+
+       echo "${segments[*]}"
+}
+
+send_email() {
+       local status="${1}"
+       local target="${2}"
+       local branch="${3}"
+       local commit="${4}"
+       local build="${5}"
+
+       sendmail -ti <<END
+From: IPFire Nightly Builder <nightly-builds@ipfire.org>
+To: Nightly Builds List <nightly-builds@lists.ipfire.org>
+Subject: [${status^^}] Nightly Build of ${branch} (${commit:0:7}) for ${target} on ${HOSTNAME}
+Date: $(date --rfc-2822)
+MIME-Version: 1.0
+Content-Type: text/plain; charset="us-ascii"
+Content-Transfer-Encoding: 7bit
+
+https://nightly.ipfire.org$(uriencode "${build:${#UPLOAD_DIR}}")
+
+$(git log -1 "${commit}")
+
+https://git.ipfire.org/?p=ipfire-2.x.git;a=shortlog;h=${commit}
+
+$(<"${build}/build.log")
+END
+}
+
 build() {
        local dir="${1}"
        shift
@@ -60,11 +115,15 @@ build() {
        local commit_new="$(git rev-parse origin/${branch})"
        [ "${commit_old}" = "${commit_new}" ] && return 2
 
+       local current_branch="$(git rev-parse --abbrev-ref HEAD)"
+       if [ "${current_branch}" != "${branch}" ]; then
+               git checkout -b "${branch}" "${commit_new}"
+       fi
+
        # Checkout the latest commit
        git reset --hard "${commit_new}"
 
-       #local now="$(date -u +"%Y-%m-%d-%H:%M")"
-       local now="$(git log --format="%cI" -1 "${commit_new}")"
+       local now="$(git log --format="%ci" -1 "${commit_new}")"
 
        local targets="$(git config build.targets)"
        [ -z "${targets}" ] && targets="i586"
@@ -73,6 +132,7 @@ build() {
        for target in ${targets}; do
                local build_path="${UPLOAD_DIR}/${branch}/${now}-${commit_new:0:8}"
                local build="${build_path}/${target}"
+               local status="failed"
 
                # Ready for build
                ./make.sh --target="${target}" clean
@@ -93,22 +153,23 @@ build() {
 
                # Save the result
                if [ "${ret}" = "0" ]; then
-                       mv -v *.iso *.img.gz *.tar.bz2 *.md5 packages/ "${build}"
-                       extract_installer_from_iso "${build}"
+                       status="success"
                        touch "${build}/.success"
+
+                       # Copy images
+                       mv -v *.iso *.img.gz *.img.xz *.tar.bz2 *.md5 packages/ "${build}"
+                       extract_installer_from_iso "${build}"
                fi
                mv -v log/ "${build}"
 
+               # Send an email notification
+               send_email "${status}" "${target}" "${branch}" "${commit_new}" "${build}"
+
                # Cleanup the build environment
                ./make.sh --target="${target}" clean
 
                # Upload the result
-               # If that failed, we will keep the result and it will
-               # be retried with the next build. If that succeeded, the
-               # build will be removed from disk.
-               if sync; then
-                       rm -rf "${build_path}"
-               fi
+               sync
        done
 
        popd
@@ -117,8 +178,12 @@ build() {
 sync() {
        mkdir -p "${UPLOAD_DIR}"
 
-       rsync -avHz --progress ${RSYNC_ARGS} \
-               "${UPLOAD_DIR}/" "${UPLOAD_TO}"
+       # Acquire a Kerberos ticket for authentication
+       kinit -k -t /etc/krb5.keytab "host/${HOSTNAME}"
+
+       if rsync "${RSYNC_ARGS[@]}" "${UPLOAD_DIR}/" "${UPLOAD_TO}"; then
+               rm -rf "${UPLOAD_DIR}"
+       fi
 }
 
 is_locked() {
@@ -133,7 +198,6 @@ unlock() {
        rm -f "${LOCKFILE}"
 }
 
-
 # Don't start again if the script is already running
 # or if an other build script is running
 if is_locked || pgrep make.sh >/dev/null; then
@@ -150,4 +214,7 @@ for repo in $(find ${BASEDIR} -maxdepth 3 -type d -name ".git"); do
        build "$(dirname ${repo})"
 done
 
+# Try to sync even nothing was built for retrying failed uploads
+sync
+
 exit 0