scripts/release_helper.sh: remove files from grml-live.build-area on autobuilds
[grml-live.git] / scripts / release_helper.sh
1 #!/bin/bash
2 # Filename:      scripts/release_helper.sh
3 # Purpose:       helper script to build grml-live Debian packages
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2 or any later version.
7 ################################################################################
8 ## How to set up autobuild (for automatically updating grml-live.git and
9 ## build packages out of git tree and install them, e.g. for usage on
10 ## daily.grml.org):
11 # echo "deb file:/home/grml-live-git/grml-live.build-area/ ./" >> /etc/apt/sources.list.d/grml-live.list
12 # adduser --disabled-login --disabled-password grml-live-git
13 # visudo -> add "grml-live-git ALL=NOPASSWD: /usr/bin/apt-get"
14 # su - grml-live-git
15 # mkdir /home/grml-live-git/grml-live.build-area
16 # git clone git://git.grml.org/grml-live.git
17 # git config --global user.name "Grml-Live Git Autobuild"
18 # git config --global user.email "grml-live-git@$(hostname)"
19 #
20 # Finally install a cron job (as user grml-live-git) like:
21 # 30 00 * * * cd /home/grml-live-git/grml-live.git/ && env AUTOBUILD=1 scripts/release_helper.sh >/home/grml-live-git/grml-live-build.log
22 ################################################################################
23
24 set -e
25 set -u
26
27 export LC_ALL=C
28 export LANG=C
29
30 debian_version=''
31 script_version=''
32
33 autobuild_branch="autobuild_$(date +%Y%m%d_%H%M%S)"
34
35 if [ -n "${AUTOBUILD:-}" ] ; then
36   git checkout master
37   git pull
38   git checkout -b "$autobuild_branch"
39 else
40   if git status --porcelain | grep -q '^?? '; then
41     printf "Uncommited changes in current working tree. Please commit/clean up.\n"
42     exit 1
43   fi
44 fi
45
46 printf "Building debian/changelog: "
47 if [ -n "${AUTOBUILD:-}" ] ; then
48   # since=$(git show -s --pretty="tformat:%h")
49   eval $(grep '^GRML_LIVE_VERSION=' grml-live)
50   DATE=$(date -R)
51   UNIXTIME=$(date +%s)
52
53   cat > debian/changelog << EOF
54 grml-live (${GRML_LIVE_VERSION}~autobuild${UNIXTIME}) UNRELEASED; urgency=low
55
56   * Automatically built package based on the state of
57     git repository at http://git.grml.org/?p=grml-live.git
58     on $DATE
59
60  -- grml-live Auto Build <mika@grml.org>  $DATE
61
62 EOF
63   git add debian/changelog
64   git commit -m "Releasing ${GRML_LIVE_VERSION}-~autobuild${UNIXTIME} (auto build)"
65 else
66   since=v$(dpkg-parsechangelog | awk '/^Version:/ {print $2}')
67   git-dch --ignore-branch --since=$since \
68           --id-length=7 --meta --multimaint-merge -S
69   printf "OK\n"
70 fi
71
72 if [ -z "${AUTOBUILD:-}" ] ; then
73   if ! $EDITOR debian/changelog ; then
74     printf "Exiting as editing debian/changelog returned an error." >&2
75     exit 1
76   fi
77 fi
78
79 debian_version="$(dpkg-parsechangelog | awk '/^Version:/ {print $2}')"
80
81 dorelease="true"
82 if echo "$debian_version" | grep -q '\.gbp' ; then
83   printf "Building snapshot version, not releasing.\n"
84   dorelease="false"
85 fi
86
87 printf "Updating GRML_LIVE_VERSION string in grml-live script: "
88 sed -i "s/^GRML_LIVE_VERSION=.*/GRML_LIVE_VERSION='$debian_version'/" grml-live
89 printf "OK\n"
90
91 printf "Comparing versions of debian/changelog with grml-live version string: "
92 script_version="$(awk -F= '/^GRML_LIVE_VERSION=/ {print $2}' grml-live | sed "s/'//g")"
93 debian_version="$(dpkg-parsechangelog | awk '/^Version:/ {print $2}')"
94
95 if [[ "$script_version" == "$debian_version" ]] ; then
96   printf "OK\n"
97 else
98   printf "FAILED\n."
99   printf "Debian package version ($debian_version) does not match script version ($script_version).\n"
100   exit 1
101 fi
102
103 if $dorelease || [ -n "${AUTOBUILD:-}" ] ; then
104   git add debian/changelog grml-live
105   git commit -s -m "Release new version ${debian_version}."
106 fi
107
108 printf "Building debian packages:\n"
109 if [ -n "${AUTOBUILD:-}" ] ; then
110   [ -d ../grml-live.build-area ] || mkdir ../grml-live.build-area
111   rm -f ../grml-live.build-area/grml-live* # otherwise we're keeping files forever...
112   git-buildpackage --git-ignore-branch --git-ignore-new --git-export-dir=../grml-live.build-area -us -uc
113 else
114   git-buildpackage --git-ignore-branch --git-ignore-new $*
115   printf "Finished execution of $(basename $0). Do not forget to tag release ${debian_version}\n"
116 fi
117
118 if [ -n "${AUTOBUILD:-}" ] ; then
119    (
120      cd ../grml-live.build-area
121      dpkg-scanpackages . /dev/null > Packages
122    )
123    git checkout master
124    git branch -D ${autobuild_branch} || true
125    sudo apt-get update
126    PACKAGES=$(dpkg --list grml-live\* | awk '/^ii/ {print $2}')
127    sudo apt-get -y --allow-unauthenticated install $PACKAGES
128 fi
129
130 ## END OF FILE #################################################################